Skip to content

Commit 7648129

Browse files
committed
edit
1 parent d7fe046 commit 7648129

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
*.class
1+
*.class
2+
.vscode/*
3+
!.vscode/settings.json

2. Classes in Java/InnerClass.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// Tiếp tục về lớp chồng nhau (nested class, inner class).
2+
/*
3+
*
4+
*/
5+
16
public class InnerClass {
27

38
}

2. Classes in Java/NestedClass.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,29 @@
66
// Lớp chồng nhau (nested class) là một lớp được định nghĩa bên trong một lớp khác.
77

88
// Ví dụ:
9+
910
// ContactBook là danh bạ điện thoại, bao gồm danh sách các liên hệ và các nhóm.
1011
class ContactBook {
1112
public List<Contact> contacts = new ArrayList<>();
1213
public Map<String, Group> groups = new HashMap<>();
1314

1415
// Lớp Contact đại diện cho một liên hệ trong danh bạ, gồm tên và số điện thoại.
16+
/*
17+
* Tương tác với lớp bên ngoài:
18+
* - Các inner class có thể truy cập đầy đủ tới tất cả các phương thức và biến
19+
* của lớp chứa nó.
20+
* - Inner class thường không thể truy cập một cách trực tiếp từ bên ngoài lớp
21+
* chứa nó nếu không thông qua một instance của lớp chứa.
22+
* - Ví dụ, để tạo một instance của lớp Contact, ta cần thông qua một instance
23+
* của lớp ContactBook như sau
24+
* ContactBook myBook = new ContactBook();
25+
* ContactBook.Contact contact = myBook.new Contact("Alice", "1234567890");
26+
*/
1527
class Contact {
1628
private String name;
1729
private String phone;
1830

19-
// Là một lớp, Contact cũng có hàm khởi tạo.
31+
// Là một lớp, Contact cũng có hàm tạo.
2032
// Dùng this để tham chiếu đến biến của lớp Contact.
2133
public Contact(String name, String phone) {
2234
this.name = name;
@@ -30,29 +42,28 @@ public void displayContact() {
3042

3143
// Lớp Group đại diện cho một nhóm trong danh bạ, bao gồm tên nhóm và danh sách
3244
// các thành viên.
33-
static class Group {
45+
class Group {
3446
String name;
35-
List<String> members = new ArrayList<>();
47+
List<Contact> members = new ArrayList<>();
3648

3749
public Group(String name) {
3850
this.name = name;
3951
}
4052

41-
public void addMember(String contactName) {
53+
public void addMember(Contact contactName) {
4254
members.add(contactName);
4355
}
4456

4557
public void displayGroup() {
4658
System.out.println("Group: " + name);
4759
System.out.println("Members:");
48-
for (String member : members) {
49-
System.out.println(member);
60+
for (Contact member : members) {
61+
System.out.println(member.name);
5062
}
5163
}
5264
}
5365

54-
public void addContact(String name, String phone) {
55-
Contact contact = new Contact(name, phone);
66+
public void addContact(Contact contact) {
5667
contacts.add(contact);
5768
}
5869

@@ -62,20 +73,27 @@ public void addGroup(String name) {
6273
}
6374
}
6475

76+
// Lớp NestedClass chứa hàm main để chạy chương trình.
6577
public class NestedClass {
6678
public static void main(String[] args) {
6779
ContactBook myBook = new ContactBook();
68-
myBook.addContact("Alice", "1234567890");
69-
myBook.addContact("Bob", "0987654321");
80+
81+
ContactBook.Contact contact1 = myBook.new Contact("Alice", "1234567890");
82+
ContactBook.Contact contact2 = myBook.new Contact("Bob", "0987654321");
83+
84+
myBook.addContact(contact1);
85+
myBook.addContact(contact2);
7086

7187
myBook.addGroup("Friends");
72-
myBook.groups.get("Friends").addMember("Alice");
73-
myBook.groups.get("Friends").addMember("Bob");
88+
myBook.groups.get("Friends").addMember(contact1);
89+
myBook.groups.get("Friends").addMember(contact2);
7490

91+
System.out.println("_____________DISPLAY CONTACTS_____________");
7592
for (ContactBook.Contact contact : myBook.contacts) {
7693
contact.displayContact();
7794
}
7895

96+
System.out.println("_____________DISPLAY GROUP_____________");
7997
myBook.groups.get("Friends").displayGroup();
8098
}
8199
}

0 commit comments

Comments
 (0)