通过反射调用类中的方法

时间:2022-09-30 13:18:59
操作步骤:
1:实例化Class;
2:通过Class类的getMethod()方法取得一个Method的对象,并设置次方法操作时所需的参数类型;
3:使用invoke进行调用,并向方法中传递要设置的参数,但在使用invoke()方法时必须传入一个类的实例化对象

实例1:sayChina()方法中没有参数
package cn.itcast02;

import java.lang.reflect.Method;

//调用Person类中的sayChina()方法,没有参数

public class ClassDemo04 {
public static void main(String[] args) {
Class<?> c1 = null;
try {
c1 = Class. forName("cn.itcast02.Person");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//获得一个method对象
Method method = c1.getMethod( "sayChina");
//调用方法,必须传递对象实例
method.invoke(c1.newInstance());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

实例2:sayHello()方法中有参数

package cn.itcast02;

import java.lang.reflect.Method;


//调用Person类中的sayHello()方法,有参数

public class ClassDemo05 {
public static void main(String[] args) {
Class<?> c1 = null;

try {
c1 = Class. forName("cn.itcast02.Person");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Method method = c1.getMethod( "sayHello", String.class,int .class,char. class);
//调用方法,必须传递对象实例,同时传递三个参数值
Object invoke = method.invoke(c1.newInstance(),"侯勇" ,50,'男' );
System. out.println(invoke);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

 

实例3:调用getter和setter方法

package cn.itcast02;

import java.lang.reflect.Method;


//调用Person类中的sayChina()方法

public class ClassDemo04 {
public static void main(String[] args) {
Class<?> c1 = null;
Object obj = null;
try {
c1 = Class. forName("cn.itcast02.Person");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
obj = c1.newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Method method = obj.getClass().getMethod("setName" ,String.class);
Method method2 = obj.getClass().getMethod("setAge" ,int.class);
method.invoke(obj, "小红");
method2.invoke(obj, 30);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Method method = obj.getClass().getMethod("getName" );
Method method2 = obj.getClass().getMethod("getAge" );
System. out.println(method.invoke(obj));
System. out.println(method2.invoke(obj));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

  

Person类
package cn.itcast02;

public class Person implements China{
private String name;
private int age;
private char sex;

public Person() {
super();
}

public Person(String name, int age, char sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public char getSex() {
return sex;
}

public void setSex(char sex) {
this.sex = sex;
}
public void eat()
{
System.out.println("吃了");
}

@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}

@Override
public void sayChina() {
// TODO Auto-generated method stub
System.out.println("作者:"+ AUTHOR + "国籍:"+ NATIONAL);
}

@Override
public String sayHello(String name, int age, char sex) {
// TODO Auto-generated method stub
return "姓名:" + name +"年龄:"+ age +"性别:"+ sex;
}

}

  

China接口
package cn.itcast02;

public interface China {
public static final String NATIONAL = "China";
public static final String AUTHOR = "成龙";

public void sayChina();

public String sayHello(String name, int age, char sex);
}