|
| 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 | + |
1 | 72 | 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; |
2 | 89 |
|
| 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 | + } |
3 | 96 | }
|
0 commit comments