Bài giảng Lập trình hướng đối tượng - Bài 4B: Nested Class
9/18/17
1. Khái niệm
n Java cho phép định nghĩa 1 class trong class
khácàGọi là 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
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,
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
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
Inner class: ngược lại
chỉ ở 1 nơi
n
Ví dụ:
class OuterClass {
...
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
static class StaticNestedClass {
n Tăng tính đóng gói
...
n
Giả sử có class A và B
}
class InnerClass {
...
}
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ì
3
4
1
9/18/17
3.1. Static nested classes
3.1. Static nested classes (2)
public class Outside {
n
n
Được truy cập từ tên của class bao nó
public static class Skinside {
n
Ví dụ: OuterClass.StaticNestedClass
public Skinside()
{
Để tạo 1 đối tượng của static nested class:
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
{
private int localId;
public class Inside {
}
public Inner()
{
localId=0000;
id = 0; //Error
public static void main(String[] arg) {
Outside.Skinside example = new Outside.Skinside();
}
}
}
}
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
public class Outer {
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
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
private int[] arrayOfInts new int[SIZE];
public DataStructure() {//fill the array with ascending integer values
{
=
15;
=
3.2. Inner Class (3)
for (int
i
=
0;
i
<
SIZE; i++)
i;
{
arrayOfInts[i]
=
}
}
public void printEven() {//In chỉ số lẻ trong mảng
n Inner Class lại chia làm 2 loại con:
InnerEvenIterator iterator
while (iterator.hasNext())
=
{
this.new InnerEvenIterator();
System.out.println(iterator.getNext()
+ "
");
n
local inner class: Khai báo 1 inner class trong 1
method
anonymous inner classes: Khai báo 1 inner Class
trong 1 method nhưng không đặt tên
}
}
private class InnerEvenIterator
//start stepping through the array from the beginning
private int next 0;
{ //inner class implements the Iterator pattern
n
=
public boolean hasNext()
return (next <= SIZE
}
{
-
1); //check if current element is the last in the array
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
ds.printEven();
= new DataStructure();
}
9
10
a. local inner class
b. anonymous inner classes
interface Counter
{
class Outer
{
int next();
}
int outer_x
=
100;
void test()
{
public class Outer{
private int count
Counter getCounter(final String name)
for(int i=0; i<10; i++)
class Inner
void display()
System.out.println("display: outer_x
{
=
0;
{
{
{
return new Counter()
{
{
=
" + outer_x);
System.out.println("Constructor Counter()");
}
}
}
public int next()
System.out.print(name); // Access local final
System.out.println(count);
{
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
Inner inner
inner.display();
= new Inner();
Anonymous inner class cannot have
a named constructor, only an
instance initializer
}
return count++;
}
};
}
}
}
}
class InnerClassDemo
{
public static void main(String[] args)
{
public static void main(String args[])
Outer outer new Outer();
outer.test();
{
Outer out
Counter c1
c1.next();
c1.next();
=
=
new Outer();
out.getCounter("Local inner ");
Constructor Counter()
Local inner 0
Local inner 1
=
}
}
11
}
12
3
Bạn đang xem tài liệu "Bài giảng Lập trình hướng đối tượng - Bài 4B: Nested Class", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_lap_trinh_huong_doi_tuong_bai_4b_nested_class.pdf

