1.首先要在包下创建properties文件
源码如下
package s123;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
interface fruit
{
public void a();
}
class apple implements fruit
{
public void a()
{
System.out.println("苹果");
}
}
class orange implements fruit
{
public void a()
{
System.out.println("橘子");
}
}
public class IO1 {
public static void main(String[]args) throws ClassNotFoundException, IOException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Properties prop=new Properties();
prop.load(IO1.class.getResourceAsStream("/s123/a.properties"));
String string=prop.getProperty("aa");
Class a1=Class.forName("s123."+string);
Constructor<fruit> constructor=a1.getDeclaredConstructor(new Class[]{});
fruit fruit=constructor.newInstance(new Object[]{});
fruit.a();
}
}