Skip to content

Commit 7c18bb7

Browse files
committed
update
1 parent 5c1886f commit 7c18bb7

4 files changed

Lines changed: 68 additions & 12 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ Java核心技术学习代码兼测试案例<br>
2020
- [x] ├── [org.javacore.collection.util](src/main/java/org/javacore/collection/util)) // Java集合工具类(**)
2121
- [x] ├── [org.javacore.initAndCleanup](src/main/java/org/javacore/initAndCleanup)) // Java初始化及清理(*)
2222
- [x] ├── [org.javacore.proxy](src/main/java/org/javacore/proxy) // Java 代理
23-
- [ ] ├── [org.javacore.io](src/main/java/org/javacore/io) // Java IO
24-
- [ ] ├── [org.javacore.io.byteoper](src/main/java/org/javacore/io/byteoper) // Java IO 字节操作
25-
- [ ] ├── [org.javacore.io.zip](src/main/java/org/javacore/io/zip) // Java IO ZIP压缩解压
23+
- [x] ├── [org.javacore.io](src/main/java/org/javacore/io) // Java IO
24+
- [x] ├── [org.javacore.io.byteoper](src/main/java/org/javacore/io/byteoper) // Java IO 字节操作
25+
- [x] ├── [org.javacore.io.zip](src/main/java/org/javacore/io/zip) // Java IO ZIP压缩解压
2626
- [ ] ├── [org.javacore.nio](src/main/java/org/javacore/nio) // Java NIO
27-
- [ ] ├── [org.javacore.reflection](src/main/java/org/javacore/reflection) // Java 反射
28-
- [ ] ├── [org.javacore.rtti](src/main/java/org/javacore/rtti) // Java RTTI
27+
- [x] ├── [org.javacore.reflection](src/main/java/org/javacore/reflection) // Java 反射
28+
- [x] ├── [org.javacore.rtti](src/main/java/org/javacore/rtti) // Java RTTI
2929
- [ ] ├── [org.javacore.thread](src/main/java/org/javacore/thread) // Java 线程
3030

3131
=================实战JAVA 8=================

‎src/main/java/org/javacore/reflection/ReflectionTest.java‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@
2222
*/
2323

2424
/**
25-
* @author Jeff Lee
25+
* @author Jeff
2626
* @since 2015-11-9 10:45:19
27+
* getFields():获得某个类的所有的公共(public)的字段,包括父类中的字段。
28+
* getDeclaredFields():获得某个类的所有声明的字段,
29+
* 即包括public、private和proteced,但是不包括父类的申明字段。
30+
* 同样类似的还有
31+
* getConstructors()和getDeclaredConstructors()、
32+
* getMethods()和getDeclaredMethods(),
33+
* 这两者分别表示获取某个类的方法、构造函数。
2734
* 反射对象构造函数、方法及字段
2835
*/
2936
public class ReflectionTest {
3037
public final int AGE = 1;
3138
public static void main(String[] args) {
3239
Class cl = null;
3340
try {
34-
cl = Class.forName("org.javacore.reflection.ReflectionTest");
41+
// cl = Class.forName("org.javacore.reflection.ReflectionTest");
42+
cl = Class.forName("org.javacore.reflection.User");
3543
System.out.println("打印析构函数:");
3644
printConstructors(cl);
3745
System.out.println("打印方法:");
@@ -51,8 +59,9 @@ public static void printConstructors(Class cl){
5159
// 返回类所有的析构方法
5260
Constructor[] constructors = cl.getDeclaredConstructors();
5361
for (Constructor c : constructors){
54-
// 返回析构方法名称
62+
// 返回析构方法名称,即是类名 org.javacore.reflection.User
5563
String name = c.getName();
64+
System.out.print("name " + name);
5665
System.out.print(" ");
5766
// 获取Java语言的修饰符
5867
// 修饰符由 Java 虚拟机的 public、protected、private、
@@ -83,8 +92,9 @@ public static void printMethods(Class cl){
8392
for (Method m : methods) {
8493
// 获取方法返回对象
8594
Class retType = m.getReturnType();
95+
// 获取方法名
8696
String name = m.getName();
87-
97+
System.out.print("name " + name);
8898
System.out.print(" ");
8999
// 获取Java语言的修饰符
90100
// 修饰符由 Java 虚拟机的 public、protected、private、
@@ -105,9 +115,10 @@ public static void printMethods(Class cl){
105115
}
106116
}
107117

108-
public static void printFields(Class clazz){
118+
public static void printFields(Class cl){
109119
// 获取字段Field对象数组
110-
Field[] fields = clazz.getFields();
120+
Field[] fields = cl.getFields();
121+
// Field[] fields = cl.getDeclaredFields();
111122
for (Field field : fields){
112123
// 获取字段声明类型对象
113124
Class type = field.getType();
@@ -121,7 +132,7 @@ public static void printFields(Class clazz){
121132
String modifiers = Modifier.toString(field.getModifiers());
122133
if (modifiers.length() > 0)
123134
System.out.print(modifiers + " ");
124-
System.out.print(type.getName() + " " + name);
135+
System.out.println(type.getName() + " " + name);
125136
}
126137
}
127138
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.javacore.rtti;
2+
3+
/**
4+
* 通过静态初始化块->初始化age
5+
* 通过初始化块->  初始化sex
6+
* 通过构造方法-> 初始化name
7+
*/
8+
public class LearnStatic {
9+
String name;
10+
String sex;
11+
static int age;
12+
13+
// 构造方法
14+
public LearnStatic(){
15+
System.out.println("constructor part");
16+
name = "wu";
17+
}
18+
19+
// 初始化块
20+
{
21+
System.out.println("initial part");
22+
sex = "male";
23+
}
24+
25+
// static initial part
26+
static {
27+
System.out.println("static initial part");
28+
age = 18;
29+
}
30+
31+
public void print(){
32+
System.out.println("name:"+ name);
33+
System.out.println("sex:"+ sex);
34+
System.out.println("age:"+ age);
35+
}
36+
37+
public static void main(String[] args){
38+
LearnStatic hello = new LearnStatic();
39+
LearnStatic hello2 = new LearnStatic();
40+
// hello.print();
41+
}
42+
}

‎src/main/java/org/javacore/rtti/ShowMethods.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
* @author Jeff Lee
2323
* @since 2015-11-2 16:43:41
2424
* 获取Class方法案例
25+
* toGenericString是Constructor,Method,Field这三个类专有的方法,
26+
* 如Constructor类,给出的是对应的构造函数的细节,
27+
* 比如修饰符,构造函数名和参数列表等
2528
*/
2629
public class ShowMethods {
2730
public static void main(String[] args) {

0 commit comments

Comments
 (0)