Skip to content

Commit 1e966df

Browse files
committed
反射:使用构造方法创建对象实例
1 parent 77dd512 commit 1e966df

3 files changed

Lines changed: 99 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
#Java中级
2222
##反射
23-
* [获取方法、字段和注解](src/cn/aofeng/demo/java/lang/reflect/ClassAnalyze.java)
23+
* [获取构造方法、普通方法、字段和注解](src/cn/aofeng/demo/java/lang/reflect/ClassAnalyze.java)
24+
* [使用构造方法创建对象实例](src/cn/aofeng/demo/java/lang/reflect/CreateInstance.java)
2425

2526
##序列化与反序列化
2627
###Json
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.aofeng.demo.java.lang.reflect;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.InvocationTargetException;
5+
6+
import cn.aofeng.demo.util.LogUtil;
7+
8+
/**
9+
* 通过反射使用构造方法创建对象实例。
10+
*
11+
* @author <a href="mailto:[email protected]">聂勇</a>
12+
*/
13+
public class CreateInstance {
14+
15+
public static void main(String[] args) throws InstantiationException, IllegalAccessException,
16+
NoSuchMethodException, SecurityException,
17+
IllegalArgumentException, InvocationTargetException {
18+
Class<Man> claz = Man.class;
19+
20+
// 调用默认的public构造方法
21+
Man man = claz.newInstance();
22+
LogUtil.log(man.toString());
23+
24+
// 调用带参数的protected构造方法
25+
Constructor<Man> manC = claz.getDeclaredConstructor(String.class);
26+
man = manC.newInstance("aofeng");
27+
LogUtil.log(man.toString());
28+
29+
// 调用带参数的private构造方法
30+
manC = claz.getDeclaredConstructor(String.class, int.class);
31+
manC.setAccessible(true);
32+
man = manC.newInstance("NieYong", 32);
33+
LogUtil.log(man.toString());
34+
}
35+
36+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cn.aofeng.demo.java.lang.reflect;
2+
3+
import cn.aofeng.demo.json.gson.Person;
4+
import cn.aofeng.demo.util.LogUtil;
5+
6+
/**
7+
* 男人。
8+
*
9+
* @author <a href="mailto:[email protected]">聂勇</a>
10+
*/
11+
public class Man extends Person {
12+
13+
private boolean marry;
14+
15+
private int power;
16+
17+
public Man() {
18+
super();
19+
LogUtil.log("%s的默认构造方法被调用", Man.class.getName());
20+
}
21+
22+
protected Man(String name) {
23+
super(name, 0);
24+
LogUtil.log("%s带name参数的构造方法被调用", Man.class.getName());
25+
}
26+
27+
@SuppressWarnings("unused")
28+
private Man(String name, int age) {
29+
super(name, age);
30+
LogUtil.log("%s带name和age参数的构造方法被调用", Man.class.getName());
31+
}
32+
33+
public static void fight(Man a, Man b) {
34+
String win = "unkown";
35+
if (a.power > b.power) {
36+
win = a.getName();
37+
} else if (b.power > a.power) {
38+
win = a.getName();
39+
}
40+
41+
LogUtil.log("%s vs %s, fight result:%s", a.getName(), b.getName(), win);
42+
}
43+
44+
public boolean isMarry() {
45+
return marry;
46+
}
47+
48+
public void setMarry(boolean marry) {
49+
this.marry = marry;
50+
}
51+
52+
public int getPower() {
53+
return power;
54+
}
55+
56+
@SuppressWarnings("unused")
57+
private void setPower(int power) {
58+
this.power = power;
59+
}
60+
61+
}

0 commit comments

Comments
 (0)