Skip to content

Latest commit

 

History

History
50 lines (42 loc) · 848 Bytes

工厂模式.md

File metadata and controls

50 lines (42 loc) · 848 Bytes
interface car{
    void driver();
}
interface yxl{
    void love();
}
class yb implements yxl{

    @Override
    public void love() {
        System.out.println("love");
    }
}
class BMW implements car{

    @Override
    public void driver() {
        System.out.println("Driver BMW");
    }
}

 class Factory {
    public static void main(String[] args)
    {
        car car=MyFactory.getInstance("BMW");
        car.driver();
        yxl myxhh=MyFactory.getInstance("yb");
        myxhh.love();
    }

}
class MyFactory<T>{
    private MyFactory(){}
    @SuppressWarnings("unchecked")
    public static <T> T getInstance(String className){
        T t=null;
        try {
            t=(T)Class.forName(className).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
}