자바 반사 로 인터페이스 프로 그래 밍 실현

1049 단어
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

좋은 웹페이지 즐겨찾기