Bài giảng Lập trình hướng đối tượng - Chương 4, Phần 2: Nested Class
Tại sao sử dụng nested class?
n Nhóm 1 cách logic các class được sử dụng
chỉ ở 1 nơi
n Nếu 1 class hữu ích chỉ cho 1 class khác à sẽ
logic nếu nhúng class đó vào class kia
n Tăng tính đóng gói
n Giả sử có class A và B
n B cần truy cập tới các thành phần private của A
n à Đặt B là nested Class trong A
n B có thể bị ẩn với bên ngoài (private, )
n Giúp dễ đọc code và dễ bảo trì
Inner Class
n 1 thể hiện (instance) của inner class chỉ tồn
tại được trong 1 thể hiện của outer class
n Để khởi tạo đối tượng cho inner class, phải khởi
tạo đối tượng của outer class trước
n OuterClass.InnerClass innerObject =
outerObject.new InnerClass();

Trang 1

Trang 2

Trang 3
Bạn đang xem tài liệu "Bài giảng Lập trình hướng đối tượng - Chương 4, Phần 2: Nested Class", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
Tóm tắt nội dung tài liệu: Bài giảng Lập trình hướng đối tượng - Chương 4, Phần 2: Nested Class
9/18/17
1. Khái niệm
n Java cho phép định nghĩa 1 class trong class
Bộ môn Công nghệ Phần mềm khácàGọi là nested class
Viện CNTT & TT n Nested class là 1 thành viên của lớp bao nó
n Các loại từ chỉ định truy cập: public, private, protected,
Trường Đại học Bách Khoa Hà Nội không có gì
n Ví dụ:
class OuterClass {
LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ...
class NestedClass {
Bài 4B. Nested Class ...
}
}
2
2. Tại sao sử dụng nested class? 3. Phân loại
n Nested class chia làm 2 loại: static và non-static
n Nhóm 1 cách logic các class được sử dụng
n Static nested class: Nếu nested class được khai báo là static
chỉ ở 1 nơi n Inner class: ngược lại
n Nếu 1 class hữu ích chỉ cho 1 class khác à sẽ n Ví dụ:
logic nếu nhúng class đó vào class kia class OuterClass {
...
n Tăng tính đóng gói static class StaticNestedClass {
...
n Giả sử có class A và B }
n B cần truy cập tới các thành phần private của A class InnerClass {
...
n à Đặt B là nested Class trong A }
n B có thể bị ẩn với bên ngoài (private, ) }
n Giúp dễ đọc code và dễ bảo trì
3 4
1
9/18/17
3.1. Static nested classes 3.1. Static nested classes (2)
n Được truy cập từ tên của class bao nó public class Outside {
n Ví dụ: OuterClass.StaticNestedClass public static class Skinside {
n Để tạo 1 đối tượng của static nested class: public Skinside()
n OuterClass.StaticNestedClass nestedObject = new {
OuterClass.StaticNestedClass(); System.out.println("Demo static");
n Chỉ được truy cập các thành viên static của class bao nó }
public class Outer { }
private int id;
public static class Inner public class Inside {
{ }
private int localId;
public Inner() public static void main(String[] arg) {
{ Outside.Skinside example = new Outside.Skinside();
localId=0000; }
id = 0; //Error }
}
} 5 6
3.2. Inner Class 3.2. Inner Class (2)
n Inner class có thể truy cập tới 1 member bất kỳ của
n 1 thể hiện (instance) của inner class chỉ tồn
outer class
tại được trong 1 thể hiện của outer class n Inner class không được có thành phần static
n Để khởi tạo đối tượng cho inner class, phải khởi public class Outer {
tạo đối tượng của outer class trước private int id;
private class Inner
n OuterClass.InnerClass innerObject =
{
outerObject.new InnerClass();
private static int defaultId; //Error
public Inner()
{
id = 00001; //Truy cập được id ngoài
}
}
7 8
2
9/18/17
public class DataStructure {
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];
public DataStructure() {//fill the array with ascending integer values 3.2. Inner Class (3)
for (int i = 0; i < SIZE; i++) {
arrayOfInts[i] = i;
}
}
public void printEven() {//In chỉ số lẻ trong mảng
InnerEvenIterator iterator = this.new InnerEvenIterator(); n Inner Class lại chia làm 2 loại con:
while (iterator.hasNext()) {
System.out.println(iterator.getNext() + " "); n local inner class: Khai báo 1 inner class trong 1
}
} method
private class InnerEvenIterator { //inner class implements the Iterator pattern
//start stepping through the array from the beginning
private int next = 0; n anonymous inner classes: Khai báo 1 inner Class
public boolean hasNext() {
return (next <= SIZE - 1); //check if current element is the last in the array trong 1 method nhưng không đặt tên
}
public int getNext() {
int retValue = arrayOfInts[next];
next += 2; //get the next even element
return retValue;
}
}
public static void main(String s[]) {
//fill the array with integer values and print out only values of even indices
DataStructure ds = new DataStructure();
ds.printEven();
}
}
9 10
a. local inner class b. anonymous inner classes
class Outer { interface Counter {
int outer_x = 100; int next();
}
void test() {
public class Outer{
for(int i=0; i<10; i++) { private int count = 0;
class Inner { Counter getCounter(final String name) {
void display() { return new Counter() {
System.out.println("display: outer_x = " + outer_x); {
} System.out.println("Constructor Counter()");
} }
public int next() {
Inner inner = new Inner(); display: outer_x = 100
System.out.print(name); // Access local final
inner.display(); display: outer_x = 100 System.out.println(count); Anonymous inner class cannot have
} display: outer_x = 100 return count++;
a named constructor, only an
} display: outer_x = 100 }
} }; instance initializer
display: outer_x = 100 }
class InnerClassDemo { display: outer_x = 100
public static void main(String[] args) {
public static void main(String args[]) { display: outer_x = 100 Outer out = new Outer();
Outer outer = new Outer(); display: outer_x = 100 Counter c1 = out.getCounter("Local inner "); Constructor Counter()
Local inner 0
outer.test(); c1.next();
display: outer_x = 100 Local inner 1
} c1.next();
} display: outer_x = 100 }
11 } 12
3File đính kèm:
bai_giang_lap_trinh_huong_doi_tuong_chuong_4_phan_2_nested_c.pdf

