1. Trang chủ
  2. » Công Nghệ Thông Tin

Bài giảng Lập trình hướng đối tượng: Chương 4.2 - ĐH Bách Khoa Hà Nội

3 13 0

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

THÔNG TIN TÀI LIỆU

Bài giảng Lập trình hướng đối tượng: Chương 4.2 Nested Class cung cấp cho người học những kiến thức như: Khái niệm; Tại sao sử dụng nested class? Phân loại; Static nested classes; Inner Class. Mời các bạn cùng tham khảo!

9/18/17 Khái niệm n 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 Java cho phép định nghĩa class class khácàGọi nested class n n n Nested class thành viên lớp bao Các loại từ định truy cập: public, private, protected, khơng có Ví dụ: class OuterClass { class NestedClass { } } LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG Bài 4B Nested Class Phân loại Tại sử dụng nested class? n Nhóm cách logic class sử dụng nơi n n n Nếu class hữu ích cho class khác logic nhúng class vào class n n B cần truy cập tới thành phần private A Đặt B nested Class A n B bị ẩn với bên ngồi (private, …) Giúp dễ đọc code dễ bảo trì Static nested class: Nếu nested class khai báo static Inner class: ngược lại Ví dụ: class OuterClass { static class StaticNestedClass { } class InnerClass { } } Giả sử có class A B n Nested class chia làm loại: static non-static n Tăng tính đóng gói n n n 9/18/17 3.1 Static nested classes n Được truy cập từ tên class bao n n public class Outside { public static class Skinside { public Skinside() { System.out.println("Demo static"); } } Ví dụ: OuterClass.StaticNestedClass Để tạo đối tượng static nested class: n n 3.1 Static nested classes (2) OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); Chỉ truy cập thành viên static class bao public class Outer { private int id; public static class Inner { private int localId; public Inner() { localId=0000; id = 0; //Error } } public class Inside { } public static void main(String[] arg) { Outside.Skinside example = new Outside.Skinside(); } } 3.2 Inner Class n 3.2 Inner Class (2) thể (instance) inner class tồn thể outer class n n n Để khởi tạo đối tượng cho inner class, phải khởi tạo đối tượng outer class trước n Inner class truy cập tới member outer class Inner class thành phần static public class Outer { private int id; private class Inner { private static int defaultId; //Error public Inner() { id = 00001; //Truy cập id } } OuterClass.InnerClass innerObject = outerObject.new InnerClass(); 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 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

Ngày đăng: 20/06/2021, 09:06

Xem thêm:

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN