-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToyTest.java
More file actions
56 lines (48 loc) · 1.44 KB
/
Copy pathToyTest.java
File metadata and controls
56 lines (48 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ClassObject;
interface HasBatteries{}
interface Waterproof{}
interface Shoots{}
class Toy{
Toy(){}
Toy(int i) {
}
}
class FancyToy extends Toy implements HasBatteries, Waterproof, Shoots {
FancyToy() {
super(1);
}
}
public class ToyTest {
static void printInfo(Class clazz) {
System.out.println("Class name: " + clazz.getName() + " is interface: " + clazz.isInterface() + " ] ");
System.out.println("Simple name: " + clazz.getSimpleName());
System.out.println("Canonical name : " + clazz.getCanonicalName());
}
public static void main(String[] args) {
Class<?> c = null;
try {
c = Class.forName("ClassObject.FancyToy");
} catch (Exception e) {
System.out.println("Can't find FancyToy");
System.exit(1);
}
printInfo(c);
//print all interface
for (Class<?> aClass : c.getInterfaces()) {
printInfo(aClass);
}
Class<?> up = c.getSuperclass();
Object obj = null;
try {
//使用newInstance必须有默认构造器
obj = up.newInstance();
} catch (InstantiationException e) {
System.out.println("Cannot instantiate");
System.exit(1);
} catch (IllegalAccessException e) {
System.out.println("Cannot access");
System.exit(1);
}
printInfo(obj.getClass());
}
}