Contents
  1. 1. 通过一个对象获得完整的包名和类名
  2. 2. 实例化Class类对象的3种方法
  3. 3. 通过Class实例化类对象
  4. 4. 返回一个类实现的接口
  5. 5. 返回父类
  6. 6. 返回他的构造方法
  7. 7. 返回所有方法
  8. 8. 返回类的属性
  9. 9. 通过反射调用其他类中的方法
  10. 10. 通过反射操作属性

参考:http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html#!comments

通过一个对象获得完整的包名和类名

实例化Class类对象的3种方法

通过Class实例化类对象

返回一个类实现的接口

返回父类

返回他的构造方法

返回所有方法

返回类的属性

通过反射调用其他类中的方法

通过反射操作属性

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package yu;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

interface hehe{
public static final String intefaceParam = "interfaceName";
}

class human{

public String fatherName;

public void sayFather(){
System.out.println("我是父类");

}
}

class Student extends human implements hehe{

private String name;//私有字段

private int id;//私有字段

public String asd;//公有字段

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}

public int getId(){
return id;
}

public void setId(int id){
this.id = id;
}

public Student() {}

public Student(String name, int id){
this.name = name;
this.id = id;
}

@Override
public String toString(){
return "[" + this.name + ": " + this.id + "]";
}

public void say(){
System.out.println("我是本类里的方法");
}

public void say2(String a){
System.out.println("我是本类中的另一个方法");
}

}

public class Lian4 {

public static void main(String[] args) throws Exception
{


/**
* 通过一个对象获得完整的包名和类名
* */

Student stu1 = new Student();
System.out.println(stu1.getClass().getName());

//实例化Class类对象的3中方法
Class<?> demo1 = null;
Class<?> demo2 = null;
Class<?> demo3 = null;
try{
//一般尽量采用这种形式
demo1 = Class.forName("yu.Student");
}catch(Exception e){
e.printStackTrace();
}

demo2 = new Student().getClass();
demo3 = Student.class;
System.out.println("类名称 "+demo1.getName());
System.out.println("类名称 "+demo2.getName());
System.out.println("类名称 "+demo3.getName());


//通过Class实例化类对象
Class<?> studentDemo = null;
try{
studentDemo = Class.forName("yu.Student");//forName的返回值是Class<?>
}catch(Exception e){
e.printStackTrace();
}

Student stu2 = null;
try{
stu2 = (Student) studentDemo.newInstance();//要求类有无参构造函数注意forName 的返回值,要强制转换
}catch(InstantiationException e){
e.printStackTrace();
}catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stu2.setName("yu");
stu2.setId(18);
System.out.println(stu2);

//返回一个类实现的接口:
for(Class<?> inte : studentDemo.getInterfaces()){//注意studentDemo是类的Class对象
System.out.println(inte);
}

//返回父类
Class<?> up = studentDemo.getSuperclass();
System.out.println(up);

//返回他的构造方法:
Class<?> studentDemo2 = null;
try{
studentDemo2 = Class.forName("yu.Student");
}catch (Exception e) {
e.printStackTrace();
}

Constructor<?>cons[] = studentDemo2.getConstructors();
for(Constructor<?> con : cons){
System.out.println(con);
}
System.out.println();

//返回所有方法:
Method[] method = studentDemo2.getMethods();
for(int i = 0; i < method.length; i++){
Class<?> returnType = method[i].getReturnType();//方法的返回类型
Class<?>[] para = method[i].getParameterTypes();//方法的参数

int temp = method[i].getModifiers();//getModifiers返回此类或接口以整数编码的 Java 语言修饰符。
System.out.print(Modifier.toString(temp)+" ");//修饰符
System.out.print(returnType.getName()+" ");//返回类型
System.out.print(method[i].getName()+" ");//方法名称
System.out.print("(");
for(int j = 0; j < para.length; ++j){
System.out.print(para[j].getName() + " " + "arg" + j);//参数
if(j < para.length - 1){
System.out.print(",");
}
}
Class<?>[] exce = method[i].getExceptionTypes();//方法可能抛出的异常
if(exce.length > 0){
System.out.print(") throws ");
for(int k = 0; k < exce.length; ++k){
System.out.print(exce[k].getName() + " ");
if(k < exce.length - 1){
System.out.print(",");
}
}
}else{
System.out.print(")");
}
System.out.println();
}

//返回类的属性:
System.out.println("===============本类属性所有属性========================");
Field[] field = studentDemo2.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
// 权限修饰符
int mo = field[i].getModifiers();
String priv = Modifier.toString(mo);//和上面那句一起用
// 属性类型
Class<?> type = field[i].getType();
System.out.println(priv + " " + type.getName() + " "
+ field[i].getName() + ";");
}
System.out.println("===============实现的接口或者父类或本类的可访问属性========================");
// 取得实现的接口或者父类的属性
Field[] filed1 = studentDemo2.getFields();//getFields() 返回Field对象数组类包含类,父类或接口的所有可访问公共字段。
for (int j = 0; j < filed1.length; j++) {
// 权限修饰符
int mo = filed1[j].getModifiers();
String priv = Modifier.toString(mo);
// 属性类型
Class<?> type = filed1[j].getType();
System.out.println(priv + " " + type.getName() + " "
+ filed1[j].getName() + ";");
}


//通过反射调用其他类中的方法:
Class<?> studentDemo3 = null;
try{
studentDemo3 = Class.forName("yu.Student");
}catch(Exception e){
e.printStackTrace();
}

try{
Method method1 = studentDemo3.getMethod("say");
method1.invoke(studentDemo3.newInstance());

Method method2 = studentDemo3.getMethod("say2", String.class);//方法的名称和参数列表
method2.invoke(studentDemo3.newInstance(), "随便");
}catch(Exception e){
e.printStackTrace();
}

//通过反射操作属性
Object obj = studentDemo3.newInstance();

Field field1 = studentDemo3.getDeclaredField("name");
field1.setAccessible(true);
field1.set(obj, "name:修改后的名字");
System.out.println(field1.get(obj));

}
}


输出:

yu.Student
类名称 yu.Student
类名称 yu.Student
类名称 yu.Student
[yu: 18]
interface yu.hehe
class yu.human
public yu.Student(java.lang.String,int)
public yu.Student()

public java.lang.String toString ()
public java.lang.String getName ()
public int getId ()
public void setName (java.lang.String arg0)
public void setId (int arg0)
public void say2 (java.lang.String arg0)
public void say ()
public void sayFather ()
public final void wait () throws java.lang.InterruptedException
public final void wait (long arg0,int arg1) throws java.lang.InterruptedException
public final native void wait (long arg0) throws java.lang.InterruptedException
public boolean equals (java.lang.Object arg0)
public native int hashCode ()
public final native java.lang.Class getClass ()
public final native void notify ()
public final native void notifyAll ()
===============本类属性所有属性========================

private java.lang.String name;
private int id;
public java.lang.String asd;
===============实现的接口或者父类或本类的可访问属性========================
public java.lang.String asd;
public static final java.lang.String intefaceParam;
public java.lang.String fatherName;
我是本类里的方法
我是本类中的另一个方法
name:修改后的名字
Contents
  1. 1. 通过一个对象获得完整的包名和类名
  2. 2. 实例化Class类对象的3种方法
  3. 3. 通过Class实例化类对象
  4. 4. 返回一个类实现的接口
  5. 5. 返回父类
  6. 6. 返回他的构造方法
  7. 7. 返回所有方法
  8. 8. 返回类的属性
  9. 9. 通过反射调用其他类中的方法
  10. 10. 通过反射操作属性