LẬP TRÌNH HƯỚNG đối TƯỢNG BAAIF 4b NESTED CLASS

2 100 0
LẬP TRÌNH HƯỚNG đối TƯỢNG   BAAIF 4b NESTED CLASS

Đang tải... (xem toàn văn)

Thông tin tài liệu

8/24/2011 Khái niệm Java cho phép đị nh nghĩa class class khác Gọi nested class Bộ môn Công nghệ Phần mềm Viện CNTT & TT Trường Đại học Bách Khoa Hà Nội Ví dụ: class OuterClass { class NestedClass { } } k o?sqìmg?g ︰mf?I。h?s ︸mf Bài 4B Nested Class Phân loại Tại sửdụng nested class? Nested class chia làm loại: Ví dụ: class OuterClass { static class StaticNestedClass { } class InnerClass { } } 3.1 Static nested classes 3.1 Static nested classes (2) public class Outside { public static class Skinside { public Skinside() { System.out.println("Demo static"); } } Được truy cập từtên class bao Đểtạo đối tượng static nested class: Chỉđược truy cập thành viên static class bao public class Inside { } public static void main(String[] arg) { Outside.Skinside example = new Outside.Skinside(); } } 8/24/2011 3.2 Inner Class 3.2 Inner Class (2) Inner class có thểtruy cập tới member outer class Inner class thành phần static thểhiện (instance) inner class chỉtồn thểhiện outer class public class Outer { private int id; private class Inner { private static int defaultId; //Error public Inner() { id = 00001; //Truy cậ p đư ợ c id } } public class DataStructure { private final static int SIZE = 15; private int[] arrayOfInts = new int[SIZE]; public DataStructure() {//fill the array with ascending integer values for (int i = 0; i < SIZE; i++) { arrayOfInts[i] = i; } } public void printEven() {//In số lẻ mả ng InnerEvenIterator iterator = this.new InnerEvenIterator(); while (iterator.hasNext()) { System.out.println(iterator.getNext() + " "); } } private class InnerEvenIterator { //inner class implements the Iterator pattern //start stepping through the array from the beginning private int next = 0; public boolean hasNext() { return (next ... 3.2 Inner Class 3.2 Inner Class (2) Inner class có thểtruy cập tới member outer class Inner class thành phần static thểhiện (instance) inner class chỉtồn thểhiện outer class public class Outer... DataStructure(); ds.printEven(); } } 3.2 Inner Class (3) Inner Class lại chia làm loại con: 10 a local inner class b anonymous inner classes interface Counter { int next(); } class Outer { int outer_x = 100;... (iterator.hasNext()) { System.out.println(iterator.getNext() + " "); } } private class InnerEvenIterator { //inner class implements the Iterator pattern //start stepping through the array from the

Ngày đăng: 11/11/2015, 16:31

Tài liệu cùng người dùng

Tài liệu liên quan