import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
public class Reflect {
public static void main(String[] args) throws Exception {
Jucicer jj=new Jucicer();
BufferedReader br= new BufferedReader(new FileReader("File.txt")) ;
//获取class对象
String bb=br.readLine();
Class clazz=Class.forName(bb);//获取了class文件
fruit f=(fruit)clazz.newInstance();//创建了对象
String Meth=br.readLine();
Method mm=Jucicer.class.getMethod(Meth,fruit.class);
mm.invoke(jj,f);
}
}
interface fruit
{
void squee();
void mrr();
}
class Jucicer
{
public void run(fruit ff)
{
ff.squee();
}
}
class Apple implements fruit
{
public void squee()
{
System.out.println("苹果汁");
}
public void mrr()
{
System.out.println("削皮");
}
}
class Orange implements fruit
{
public void squee()
{
System.out.println("橘子汁");
}
public void mrr()
{
System.out.println("zhajuzi");
}
}
另一个
File.txt
cn.itcast.day22.Exercise.Apple
squee
异常报告
Exception in thread "main" java.lang.NoSuchMethodException: cn.itcast.day22.Exercise.Jucicer.squee(cn.itcast.day22.Exercise.fruit)
at java.lang.Class.getMethod(Class.java:1605)
at cn.itcast.day22.Exercise.Reflect.main(Reflect.java:22)
帮忙看看吧 |
|