解析成员属性和成员方法

解析成员方法
1、利用Method执行方法
Method对象提供了如下方法,用于执行它所代表的方法:
public Object invoke(Object obj,Object... args)
使用Method可以执行无参、有参、多个参(带数组和基本数据类型)、静态、私有的 方法。
创建类的实例的两种方法:1、可以通过newInstance方法来创建
Class cls = Class.forName("cn.csdn.reflect.Student");
Student entity = (Student)cls.newInstance();

2、也可以通过Constructor的方法来创建
Class cls = Class.forName("cn.csdn.reflect.Student");
Constructor csr = cls.getConstructor(null);
Student entity = (Student)csr.newInstance(null);

3、这两种方法的区别:Class类中的newInstance方法只能在Student中有一个默认无参数构 造器时才能使用

例:
1、Get methods 是返回多有的方法:

@Test
public void test()throws Exception{
//1加载类
Class cls = Class.forName("cn.csdn.reflect.Student");
//使用cls的newInstance()方法的时候必须确保Student类中有一个无参数的构造器
Student entity = (Student)cls.newInstance();
Method mds[] = cls.getMethods();
for(Method m:mds){
System.out.println(m.toGenericString());
}

}


2、解析:public void study方法
//当解析没有返回值的方法时invoke方法的参数值为空
//有类的对象直接调用newInstance()必须确保该类中有一个无参构造器

@Test
public void test1()throws Exception{
//1加载类
Class cls = Class.forName("cn.csdn.reflect.Student");
Student entity = (Student)cls.newInstance();
//2解析方法
Method md = cls.getMethod("study", null);
//3、执行
Object obj = md.invoke(entity, null);//当没有返回值的时候 ,返回的是null
System.out.println(obj);
}

3、解析私有方法
@Test
public void test3()throws Exception{
//1、加载类
Class cls = Class.forName("cn.csdn.reflect.Student");
//2、创建类的实例
Student entity = (Student)cls.newInstance();
//3、解析方法
Method md = cls.getDeclaredMethod("display", String.class,int.class);
md.setAccessible(true);//私有的 所以需要强制执行
//执行
md.invoke(entity, "zhangsan",1200);

}
4、解析main()方法

@Test
public void test4()throws Exception{
//1、加载类
Class cls = Class.forName("cn.csdn.reflect.Student");
//2、通过Class类中的newInstance创建类的实例 ||或者可以根据Constructor
//的此方法创建
//区别:Class类中的newInstance方法只能在Student中有一个默认无参数的构
//造器时才能使用
Student entity = (Student)cls.newInstance();
//3、解析方法
Method md = cls.getMethod("main", String[].class);
//执行
//md.invoke(entity, new Object[]{new String[]{"12"}});
String src[]={};
md.invoke(entity, (Object)src);

}


解析成员属性
Field对象提供了如下方法,用于设置、获取对象属性的值:
public void set(Object obj,Object value)
public Object get(Object obj)

//返回所有的对象属性
例:
@Test
public void test()throws Exception{
//1、加载类
Class cls =Class.forName("cn.csdn.reflect.Student");
//2、创建类的实例
Student entity = (Student)cls.newInstance();
//3、解析属性
Field fds[] = cls.getDeclaredFields(); //字段
System.out.println("=========="+fds.length);
for(Field fd:fds){
fd.setAccessible(true);
System.out.println(fd.getName());
}
}


//获取private属性的值
@Test
public void test2()throws Exception{
//1、加载类
Class cls =Class.forName("cn.csdn.reflect.Student");
//2、创建类的实例
Student entity = (Student)cls.newInstance();
//获取字段的值 private
Field fd = cls.getDeclaredField("name");
//强制执行
fd.setAccessible(true);
//set 赋值(entity,"")
fd.set(entity, "redarmy");
System.out.println(entity.getName());
String value = (String)fd.get(entity);
System.out.println(value);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值