获取Class对象有三种方式:
1.通过Object类的getClass()方法。例如:
Class c1 = new String("").getClass();
2.通过Class类的静态方法——forName()来实现:
Class c2 = Class.forName("MyObject");
3.如果T是一个已定义的类型的话,在java中,它的.class文件名:T.class就代表了与其匹配的Class对象,例如:
Class c3 = Manager.class;
Class c4 = int.class;
Class c5 = Double[].class;
Class类中存在以下几个重要的方法:
1.getName()
一个Class对象描述了一个特定类的特定属性,而这个方法就是返回String形式的该类的简要描述。由于历史原因,对数组的Class对象
调用该方法会产生奇怪的结果。
2.newInstance()
该方法可以根据某个Class对象产生其对应类的实例。需要强调的是,它调用的是此类的默认构造方法。例如:
MyObject x = new MyObject();
MyObject y = x.getClass().newInstance();
3.getClassLoader()
返回该Class对象对应的类的类加载器。
4.getComponentType()
该方法针对数组对象的Class对象,可以得到该数组的组成元素所对应对象的Class对象。例如:
int[] ints = new int[]{1,2,3};
Class class1 = ints.getClass();
Class class2 = class1.getComponentType();
而这里得到的class2对象所对应的就应该是int这个基本类型的Class对象。
5.getSuperClass()
返回某子类所对应的直接父类所对应的Class对象。
6.isArray()
判定此Class对象所对应的是否是一个数组对象。
反射是松耦合,灵活性高,动态加载的;引用是紧耦合
import
java.lang.reflect.Field;
import
java.lang.reflect.Method;
import
java.lang.reflect.Modifier;
public
class
ReflectionTest {
public
static
void
main(String[] args) {
Class c=
null
;
try
{
c=Class.forName(
"java.lang.String"
);
System.out.println(
"package "
+c.getPackage().getName()+
";"
);
System.out.print(Modifier.toString(c.getModifiers())+
" "
);
System.out.print(
"class "
+c.getSimpleName()+
" "
);
if
(c.getSuperclass()!=Object.
class
) {
System.out.print(
"extends "
+ c.getSuperclass().getSimpleName());
}
Class[] inters=c.getInterfaces();
if
(inters.length>
0
){
System.out.print(
"implements "
);
for
(
int
i=
0
;i<inters.length;i++){
System.out.print(inters[i].getSimpleName());
if
(i<inters.length-
1
){
System.out.print(
","
);
}
}
}
System.out.println(
"{"
);
printFields(c);
printMethods(c);
System.out.println(
"}"
);
}
catch
(ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public
static
void
printFields(Class c){
Field[] field=c.getDeclaredFields();
if
(field.length>
0
){
for
(
int
i=
0
;i<field.length;i++){
System.out.println(Modifier.toString(field[i].getModifiers())+
" "
+field[i].getType().getSimpleName()+
" "
+field[i].getName()+
";"
);
}
}
}
public
static
void
printMethods(Class c){
Method[] method=c.getDeclaredMethods();
if
(method.length>
0
){
for
(
int
i=
0
;i<method.length;i++){
Class[] parameter=method[i].getParameterTypes();
System.out.print(Modifier.toString(method[i].getModifiers())+
" "
+method[i].getReturnType().getSimpleName()+
" "
+method[i].getName()+
"("
);
for
(
int
j=
0
;j<parameter.length;j++){
System.out.print(parameter[j].getSimpleName()+
" args"
);
if
(j!=parameter.length-
1
){
System.out.print(
","
);
}
}
System.out.print(
") "
);
Class exception[]=method[i].getExceptionTypes();
if
(exception.length>
0
) {
System.out.print(
"throws "
);
for
(
int
j =
0
; j < exception.length; j++) {
System.out.print(exception[j].getSimpleName());
}
}
System.out.println(
"{"
);
System.out.println(
"\t... ..."
);
System.out.println(
"}"
);
}
}
}
}
// 反射动态执行方法
public
class
ReflectTest {
public
int
Add(
int
x,
int
y){
System.out.println(x + y);
return
x + y;
}
public
String echo(String str) {
System.out.println(
"Hello, "
+ str);
return
"Hello, "
+ str;
}
public
static
void
main(String[] args)
throws
NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> classType = ReflectTest.
class
;
Object obj = classType.newInstance();
Method method = classType.getMethod(
"Add"
,
new
Class[]{
int
.
class
,
int
.
class
});
method.invoke(obj,
new
Object[] {
new
Integer(
100
),
new
Integer(
200
)} );
Method method2 = classType.getMethod(
"echo"
,
new
Class[] {String.
class
});
method2.invoke(obj,
new
Object[] {
"world"
});
}
}