package com.youyj.factory.factorydemo03;
interface Car
{
public void start();
public void stop();
}
class Benz implements Car
{
@Override
public void start()
{
System.out.println("Benz 车启动了...");
}
@Override
public void stop()
{
System.out.println("Benz 车停止了...");
}
}
class Fort implements Car
{
@Override
public void start()
{
System.out.println("Fort 车启动了。。。");
}
@Override
public void stop()
{
System.out.println("Fort 车停止了。。。");
}
}
class Factory
{
public static Car getInstance(String type)
{
Car c = null;
try
{
c = (Car) Class.forName("com.youyj.factory.factorydemo03." + type)
.newInstance();
}
catch (InstantiationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return c;
}
}
public class FactoryDemo03
{
/**
* @param args
*/
public static void main(String[] args)
{
Car car = Factory.getInstance("Fort");
if (null != car)
{
car.start();
car.stop();
}
else
{
System.out.println("该工厂造不成该汽车");
}
}
}