java 反射简单运用

  
  
  
0基础
Method getDeclaredMethod(String name, Class… parameterTypes)
          返回一个 Method 对象,该对象反映此 Class 对象 所表示的 类或接口的指定已声明方法
(该类声明的所有方法的单个,name:方法名,parameterTypes:参数类型.Class) 
Method[] getDeclaredMethods() 
          返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。 
Method getMethod(String name, Class… parameterTypes) 
          返回一个 Method 对象,它反映此 Class 对象所表示的类或接口的指定 公共成员方法。 
Method[] getMethods() 
          返回一个包含某些 Method 对象的数组,这些对象反映此 Class 对象所表示的类或接口(包括那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共 member 方法。 
getDeclaredField(String name) 
          返回一个 Field 对象,该对象反映此 Class 对象所表示的类或接口的 指定已声明字段。
(该类声明所有属性,不包括父类) 
Field[] getDeclaredFields() 
          返回 Field 对象的一个数组,这些对象反映此 Class 对象所表示的类或接口所声明的所有字段,包括公共、保护、默认(包)访问和私有字段,但不包括继承的字段。
Field getField(String name)
返回一个Field对象,那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共 member 属性。
Field[] getFields()
返回一个Field对象数组,那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共 member 属性。

1.Bean类型
importandroid.util.Log;

/**
* Created by : Sclgxt
* Description:
* on : 2017/11/22 0022.
*/

public classBean {
public staticString INFO = "this is a bean";
privateString message;
privateString id;
publicString MESSAGE = "this is a message";

publicBean() {
}

publicBean(String message, String id) {
this. message= message;
this. id= id;
}

private voidoutMessage() {
Log. i( "Bean", "Bean getMessage");
}

privateString outMessage(String message) {
Log. i( "Bean", "Bean getMessage:" + message);
return "over";
}

publicString outMessage(String id, String message) {
Log. i( "Bean", "Bean getMessage:id is "+ id + ";message is " + message);
return "over";
}

publicString getMessage() {
return message;
}

public voidsetMessage(String message) {
this. message= message;
}

publicString getId() {
return id;
}

public voidsetId(String id) {
this. id= id;
}
2.运用
2.1反射构造方法构造对象
private voidrefeflectConstructorMethod(TextView textView) {
try{
Class c = Class. forName( "sclgxt.modeapplication.JavaReflect.Bean");
Constructor constructor = c.getConstructor(String. class, String. class);
Bean bean = (Bean) constructor.newInstance( "1234", "Bean two params");
Log. i( "Bean", "" + bean.getId() + "" + bean.getMessage());
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log. i( "Bean", "ClassNotFoundException");
} catch (NoSuchMethodException e) {
e.printStackTrace();
Log. i( "Bean", "NoSuchMethodException");
} catch (IllegalAccessException e) {
e.printStackTrace();
Log. i( "Bean", "IllegalAccessException");
} catch (InstantiationException e) {
e.printStackTrace();
Log. i( "Bean", "InstantiationException");
} catch (InvocationTargetException e) {
e.printStackTrace();
Log. i( "Bean", "InvocationTargetException");
}
}
2.2反射私有方法并调用
private voidreflectPrivateMethod() {
try{
Class c = Class. forName( "sclgxt.modeapplication.JavaReflect.Bean");
Object o = c.newInstance();
Method method = c.getDeclaredMethod( "outMessage");
method.setAccessible( true );//作用:让私有方法得以调用,如果是共有的则不必设置
method.invoke(o);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
2.3 反射共有方法并调用
private voidreflectPublicMethod() {
try{
Class c = Class. forName( "sclgxt.modeapplication.JavaReflect.Bean");
Object o = c.newInstance();
Method method = c.getDeclaredMethod( "outMessage", String. class, String. class);
String out = (String) method.invoke(o, "123", "this is 456");
Log. i( "Bean", "" + out);
} catch (Exception e) {

}
}
2.4 反射私有属性并调用
private voidreflectPrivateField() {
try{
Class c = Class. forName( "sclgxt.modeapplication.JavaReflect.Bean");
Field field = c.getDeclaredField( "id");
Object o = c.newInstance();
field.setAccessible( true );//让私有属性得以访问,如果是共有的则不必设置
field.set(o, "this is id");
Method method = c.getDeclaredMethod( "getId");
method.setAccessible( true );//让私有方法得以访问,如果是共有的则不必设置
String id = (String) method.invoke(o);
Log. i( "Bean", "reflectField: " + id);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值