简单工厂实例(singlefactory)实例


1.首先要在包下创建properties文件

简单工厂实例(singlefactory)实例_第1张图片

简单工厂实例(singlefactory)实例_第2张图片

源码如下

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 constructor=a1.getDeclaredConstructor(new Class[]{});
	 fruit fruit=constructor.newInstance(new Object[]{});
	 fruit.a();
}
}

你可能感兴趣的:(java)