Skip to content

Commit 5281734

Browse files
committed
up
1 parent a493e07 commit 5281734

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

2. Classes in Java/Reflection.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,96 @@
1+
// Trong phần này, chúng ta sẽ tìm hiểu về Java Reflection API, hỗ trợ bởi gói java.lang.reflect.
2+
/*
3+
* - Reflection là khả năng của một lớp hoặc đối tượng để kiểm tra chính nó.
4+
* - Reflection cho phép mã Java nhìn vào đối tượng (hay chính xác hơn là lớp của đối tượng) và xác định cấu trúc của nó.
5+
* - Các phương thức trong class Class:
6+
* + Field[] getFields(): Lấy ra tất cả các biến public của class và các biến được kế thừa từ superclass.
7+
* + Field getField(String name): Lấy ra biến public có tên name của class hoặc được kế thừa từ superclass.
8+
* + Field[] getDeclaredFields(): Lấy ra tất cả các biến của class, bao gồm cả private, protected, default và public, nhưng không bao gồm biến của superclass.
9+
* + Field getDeclaredField(String name): Lấy ra biến có tên name của class, bao gồm cả private, protected, default và public, nhưng không bao gồm biến của superclass.
10+
* + Method[] getMethods(): Lấy ra tất cả các phương thức public của class và được kế thừa từ superclass.
11+
* + Method getMethod(String name, Class<?>... parameterTypes): Lấy ra phương thức public có tên name và kiểu tham số parameterTypes của class hoặc được kế thừa từ superclass.
12+
* + Method[] getDeclaredMethods(): Lấy ra tất cả các phương thức của class, bao gồm cả private, protected, default và public, nhưng không bao gồm phương thức của superclass.
13+
* + Method getDeclaredMethod(String name, Class<?>... parameterTypes): Lấy ra phương thức có tên name và kiểu tham số parameterTypes của class, bao gồm cả private, protected, default và public, nhưng không bao gồm phương thức của superclass.
14+
* + Constructor<?>[] getConstructors(): Lấy ra tất cả các constructor public của class.
15+
* + Constructor<?> getConstructor(Class<?>... parameterTypes): Lấy ra constructor public với kiểu tham số parameterTypes của class.
16+
* + Constructor<?>[] getDeclaredConstructors(): Lấy ra tất cả các constructor của class, bao gồm cả private, protected, default và public, nhưng không bao gồm constructor của superclass.
17+
* + Constructor<?> getDeclaredConstructor(Class<?>... parameterTypes): Lấy ra constructor với kiểu tham số parameterTypes của class, bao gồm cả private, protected, default và public, nhưng không bao gồm constructor của superclass.
18+
* + Class<?>[] getInterfaces(): Lấy ra tất cả các interface mà class này triển khai.
19+
* + Class[] getDeclaredClasses(): Lấy ra tất cả các inner class được khai báo bên trong class này.
20+
*/
21+
22+
import java.lang.reflect.Field;
23+
import java.lang.reflect.Method;
24+
25+
@SuppressWarnings("all")
26+
class Dinosaur {
27+
28+
public String species;
29+
30+
Dinosaur(String species) {
31+
this.species = species;
32+
}
33+
34+
public void roar() {
35+
System.out.println("Roar");
36+
}
37+
38+
private void eat() {
39+
System.out.println("Eat");
40+
}
41+
42+
protected void sleep() {
43+
System.out.println("Sleep");
44+
}
45+
}
46+
47+
@SuppressWarnings("all")
48+
class BabyDinosaur extends Dinosaur {
49+
50+
private int weight;
51+
private int age;
52+
53+
BabyDinosaur(String species, int weight, int age) {
54+
super(species);
55+
this.weight = weight;
56+
this.age = age;
57+
}
58+
59+
public void play() {
60+
System.out.println("Play");
61+
}
62+
63+
private void crawl() {
64+
System.out.println("Crawl");
65+
}
66+
67+
protected void run() {
68+
System.out.println("Run");
69+
}
70+
}
71+
172
public class Reflection {
73+
public static void main(String[] args) {
74+
Class<?> dinosaur = Dinosaur.class;
75+
76+
System.out.println("--Methods of Dinosaur class");
77+
Method[] methods = dinosaur.getMethods();
78+
for (Method method : methods) {
79+
System.out.println(method.getName());
80+
}
81+
82+
System.out.println("--Fields of Dinosaur class");
83+
Field[] fields = dinosaur.getFields();
84+
for (Field field : fields) {
85+
System.out.println(field.getName());
86+
}
87+
88+
Class<?> babyDinosaur = BabyDinosaur.class;
289

90+
System.out.println("--Fields of BabyDinosaur class");
91+
Field[] subfields = babyDinosaur.getFields();
92+
for (Field field : subfields) {
93+
System.out.println(field.getName());
94+
}
95+
}
396
}

2. Classes in Java/TheClassClass.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
// Trong Java, mỗi class tại thời điểm chạy đều được biểu diễn bởi 1 instance của class java.lang.Class.
2+
/*
3+
* - Class này cung cấp các phương thức để truy cập thông tin về class như tên class, các fields, methods, constructors, ...
4+
* - Mỗi loại đối tượng bạn sử dụng có một đối tượng Class tương ứng, và đối tượng này có trách nhiệm tạo ra cácinstance của loại đó.
5+
*/
6+
7+
// Ví dụ sau:
8+
9+
@SuppressWarnings("rawtypes")
110
public class TheClassClass {
2-
11+
public static void main(String[] args) {
12+
// Một đối tượng của class String
13+
String str = "Hello";
14+
15+
// Lấy ra class của đối tượng str
16+
Class strClass = str.getClass();
17+
18+
// Cung có thể tham chiếu trực tiếp tới class thông qua tên class
19+
Class strClass2 = String.class;
20+
21+
// In ra tên class của đối tượng str
22+
System.out.println(strClass.getName()); // In ra: java.lang.String
23+
System.out.println(strClass2.getName()); // In ra: java.lang.String
24+
25+
// Phương thức forName() của class Class dùng để tạo một đối tượng Class từ tên
26+
// class
27+
try {
28+
Class strClass3 = Class.forName("java.lang.String");
29+
System.out.println(strClass3.getName()); // In ra: java.lang.String
30+
} catch (ClassNotFoundException e) {
31+
e.printStackTrace();
32+
}
33+
}
334
}

bin/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)