黑暗之魂2王城大门:java反射机制method用法
来源:百度文库 编辑:九乡新闻网 时间:2024/05/06 11:10:04
import java.lang.reflect.Method;
public class MethodTest {
static void staticMethod()
{
System.out.println("执行staticMethod()方法");
}
public int publicMethod(int i)
{
System.out.println("执行publicMethod()方法");
return i*20;
}
protected int protectedMethod(String s,int i) throws NumberFormatException{
System.out.println("执行protectedMethod()方法");
return Integer.valueOf(s)+i;
}
private String privateMethod(String...strings){
System.out.println("执行privateMethod()方法");
StringBuffer stringBuffer = new StringBuffer();
for(int i=0;i
stringBuffer.append(strings[i]);
}
return stringBuffer.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//实例化一个对象:
MethodTest methodTest = new MethodTest();
Method[] declaredMethods = MethodTest.class.getDeclaredMethods(); //获得所有的方法
for(int i=0;i
Method method = declaredMethods[i];
System.out.println("名称:"+method.getName()); //方法名称
System.out.println("是否允许带有可变参数变量:"+method.isVarArgs());//
System.out.println("入口参数类型依次为:");
Class[]parameterTypes = method.getParameterTypes();//获得方法所有的参数类型
for(int j=0;j
System.out.println("parameterTypes[" + j + "]" + parameterTypes[j]);
}
System.out.println("返回值类型:"+method.getReturnType()); //获得方法返回值类型
System.out.println("可能抛出异常类型有:");
Class []exceptionTypes = method.getExceptionTypes(); //获得可能抛出的所有异常类型
for(int j=0;j
}
boolean isTurn = true;
while(isTurn) //调用类中的方法
{
try
{
isTurn = false;
if(i==0) //请注意生成的顺序,可以先把这些语句注释掉,看下顺序再另行执行。
{
method.invoke(methodTest);
} else if(i==1)
{
System.out.println("返回值:" + method.invoke(methodTest,168));
}else if(i==2)
{
System.out.println("返回值:" + method.invoke(methodTest,"7",5));
}else if(i==3)
{
Object[] parameters = new Object[]{new String[]{"M","W","Q"}};
System.out.println("返回值:"+method.invoke(methodTest, parameters));
}
}catch(Exception e)
{
System.out.println("在执行方法时抛出异常,执行setAccessible()方法");
method.setAccessible(true);
isTurn = true;
}
}
System.out.println("****************");
}
}
}
- Invoke用法说明
- import java.lang.reflect.Method;
- public class InvokeTester {
- public int add(int param1, int param2) {
- return param1 + param2;
- }
- public String echo(String mesg) {
- return "echo" + mesg;
- }
- public static void main(String[] args) throws Exception {
- Class classType = InvokeTester.class;
- Object invokertester = classType.newInstance(); //下面介绍与new 的区别
- Method addMethod = classType.getMethod("add", new Class[] { int.class,
- int.class });
- //Method类的invoke(Object obj,Object args[])方法接收的参数必须为对象,
- //如果参数为基本类型数据,必须转换为相应的包装类型的对象。invoke()方法的返回值总是对象,
- //如果实际被调用的方法的返回类型是基本类型数据,那么invoke()方法会把它转换为相应的包装类型的对象,
- //再将其返回
- Object result = addMethod.invoke(invokertester, new Object[] {
- new Integer(100), new Integer(200) });
- //在jdk5.0中有了装箱 拆箱机制 new Integer(100)可以用100来代替,系统会自动在int 和integer之间转换
- System.out.println(result);
- Method echoMethod = classType.getMethod("echo",
- new Class[] { String.class });
- result = echoMethod.invoke(invokertester, new Object[] { "hello" });
- System.out.println(result);
- }
- }
//==================================
newInstance()方法和new关键字除了一个是方法,一个是关键字外,最主要有什么区别?它们的区别在于创建对象的方式不一样,前者是使用类加载机制,后者是创建一个新类。那么为什么会有两种创建对象方式?这主要考虑到软件的可伸缩、可扩展和可重用等软件设计思想。
我们使用关键字new创建一个类的时候,这个类可以没有被加载。但是使用newInstance()方法的时候,就必须保证:1、这个类已经加载;2、这个类已经连接了。
newInstance()实际上是把new这个方式分解为两步,即首先调用Class加载方法加载某个类,然后实例化。 这样分步的好处是显而易见的。我们可以在调用class的静态加载方法forName时获得更好的灵活性,提供给了一种降耦的手段。
最后用最简单的描述来区分new关键字和newInstance()方法的区别:
newInstance: 弱类型。低效率。只能调用无参构造。
new: 强类型。相对高效。能调用任何public构造。