Test.java
package reflect;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test1 {
public static void main(String args[]) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException{
BufferedReader fs = new BufferedReader(new InputStreamReader( new FileInputStream("E:/o.txt")));
Object name = fs.readLine();
System.out.println(name.toString());
Class<?> forName_ = Class.forName(name.toString());
Interf newInstance1 = (Interf)forName_.newInstance();
newInstance1.sayHello();
}
}
Interface.java
package reflect;
public interface Interf {
public void sayHello();
}
InterfaceInpl.java
package reflect;
public interface Interf {
public void sayHello();
}
a.txt
reflect.InterfaceImpl
返回的结果为:
reflect.InterfaceImpl
hello world