标签:
interface Car { public void drive(); } class Benz implements Car { public void drive() { System.out.println("Benz"); } } class Bmw implements Car { public void drive() { System.out.println("drive Bmw"); } } class Factory { public static Car getInstance(String s) { switch (s) { case "Benze": return new Benz(); case "Bmw": return new Bmw(); default: return null; } } } public class DriveCar { public static void main(String[] args) { Car c = Factory.getInstance("Bmw"); c.drive(); } }
标签:
原文地址:http://my.oschina.net/u/2350638/blog/499251