Java反射机制

来源:互联网 发布:用友软件分类 编辑:程序博客网 时间:2024/06/06 05:17

Java反射机制

import java.lang.reflect.Method;



public class MethodTest {


public static void main(String[] args) {
try{
System.out.println("调用Math类的静态方法sin()");
Method sin=Math.class.getDeclaredMethod("sin",Double.TYPE);
Double sin1=(Double)sin.invoke(null,new Integer(1));
System.out.println("1的正弦值是"+sin1);
System.out.println("调用string类的非静态方法equals()");
Method equals=String.class.getDeclaredMethod("equals",Object.class);
Boolean mrsoft=(Boolean)equals.invoke(new String("明日科技"),"明日科技");
System.out.println("字符串是否是明日科技: "+mrsoft);
}catch(Exception e){
e.printStackTrace();
}


}


}

import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
 


public class ClassDeclarationViewer {


public static void main(String[] args) throws ClassNotFoundException{
Class<?> clazz=Class.forName("java.util.ArrayList");//获得类的对象
System.out.println("类的标准名称 "+clazz.getCanonicalName());
System.out.println("类的修饰符"+Modifier.toString(clazz.getModifiers()));
//输出类的泛型参数
TypeVariable<?>[] typeVariables=clazz.getTypeParameters();
System.out.print("类的泛型参数:");
if(typeVariables.length!=0){
for(TypeVariable<?> typeVariable:typeVariables){
System.out.println(typeVariable+"\t");
}
}else{
System.out.println("空");
}
//输出类所实现的所有接口
Type[] interfaces=clazz.getGenericInterfaces();
System.out.println("类所实现的接口");
if(interfaces.length!=0){
for(Type type:interfaces){
System.out.println("\t"+type);
}
}else{
System.out.println("\t"+"空");
}
//输出类的直接继承类,如果是继承Object则返回空
Type superClass=clazz.getGenericSuperclass();
System.out.print("类的直接继承类");
if(superClass!=null){
System.out.println(superClass);
}else{
System.out.println("空");
}
//输出类的所有注释信息,有些注释是不鞥用反射获得的
Annotation[] annotations=clazz.getAnnotations();
System.out.print("类的注解");
if(annotations.length!=0){
for(Annotation annotation:annotations){
System.out.println("\t"+annotation);
}
}else{
System.out.println("空");
}


}


}

import java.lang.reflect.Field;


public class MoreFields {
int i;
public float f;
protected boolean b;
private String s;
public static void main(String[] args) {
MoreFields example=new MoreFields();
Class exampleC=example.getClass();
//Class<MoreFields>exampleC=MoreFields.class;
Field[] declaredFields=exampleC.getDeclaredFields();//获得所有成员变量
for(int i=0;i<declaredFields.length;i++){
Field field=declaredFields[i];//遍历成员遍历
System.out.println("名称为: "+field.getName());
Class fieldType=field.getType();//获得成员变量类型
System.out.println("类型为 "+fieldType);
boolean isTurn=true;
while(isTurn){
try{
//如果该成员变量的访问权限private,则抛出异常,几不允许访问
isTurn=false;
System.out.println("修改前的值为: "+field.get(example));
//判断成员变量的类型是否为int行
if(fieldType.equals(int.class)){
System.out.println("利用方法setInt()修改成员变量的值");
field.setInt(example,168);
//判断成员变量的类型的是否float行
}else if(fieldType.equals(float.class)){
System.out.println("利用方法setFloat()修改成员变量的值");
field.setFloat(example,99.9F);
//判断成员变量的值是否是boolean行
}else if(fieldType.equals(boolean.class)){
System.out.println("利用方法setFloat()修改成员变量的值");
field.setBoolean(example,true);
}else{
System.out.println("利用方法set()方法修改成员变量的值");
field.set(example,"mingri");
}
System.out.println("修改后的值为"+field.get(example));
}catch(Exception e){
System.out.println("在设置成员变量是抛出异常,下面执行方法");
field.setAccessible(true);
isTurn=true;
}
}
System.out.println();
}



}


}

0 0
原创粉丝点击