java一個最簡單的反射的例子
阿新 • • 發佈:2018-12-23
最基本的反射 通過一個類的根目錄 String生成(newInstance)一個物件
介面
package org.fashe.test;
public interface FansheInterface {
public void shuchu();
}
實現
package org.fashe.test;
public class FansheImpl implements FansheInterface {
public void shuchu() {
System.out.println("反射成功!!");
}
}
main方法測試
package org.fashe.test;
public class Testfashe {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class forName = Class.forName("org.fashe.test.FansheImpl");
FansheInterface fasheobj = (FansheInterface) forName.newInstance();
fasheobj.shuchu();
}
}