-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
84 lines (80 loc) · 2.28 KB
/
Copy pathMain.java
File metadata and controls
84 lines (80 loc) · 2.28 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.lang.reflect.*;
import java.util.ArrayList;
////Foo 也是对象是Class类的对象
////这个对象我们成为类类型
//class Foo {
// public static void print() {
// System.out.println("this is foo ... ");
// }
//}
//
//public class Main {
// public static void main (String[] args){
// Class c1 = Foo.class;
// Foo foo1 = new Foo() ;
// Class c2 = foo1.getClass();
// Class c3;
// try{
// c3 = Class.forName("Foo");
// //
// }catch (ClassNotFoundException e){
// e.printStackTrace();
// }
//
// //通过类类型创建对象实例---> newInstance()
// //需要有无参数的构造方法
// try {
// Foo foo2 = (Foo) c1.newInstance();
// foo2.print();
// }catch (InstantiationException e){
// e.printStackTrace();
// }catch (IllegalAccessException e){
// e.printStackTrace();
// }
//
// }
//}
//class Calc {
// public static void Add(int num1 , int num2 ){
// System.out.print(num1 + "+" + num2 + "=" + (num1+num2) );
// }
//}
//
//public class Main {
// public static void main(String[] args) {
// Calc clc = new Calc();
// Class cls = clc.getClass();
// try{
// Method m1 = cls.getMethod("Add",int.class,int.class);
// //method reflect operation
// m1.invoke(clc,new Object[]{10,20});
// }catch(Exception e){
// e.printStackTrace();
// }
// }
//}
public class Main {
public static void main(String[] args) {
ArrayList list = new ArrayList();
ArrayList<String> list1 = new ArrayList<String>();
list1.add("Hello, World");
Class c1 = list.getClass();
Class c2 = list1.getClass();
// if(c1 == c2) {
// System.out.println("去擦除");
// }
// else{
// System.out.println("not");
// }
try {
Method m1 = c2.getMethod("add",Object.class);
m1.invoke(list1, 100);
System.out.println(list1.size());
for(int i=0; i<list1.size() ; i++) {
System.out.println(list1.get(i));
}
}catch (Exception e){
e.printStackTrace();
}
}
}