多态的记录
作者:互联网
1 public abstract class Car { 2 3 public abstract String getCarName(); 4 5 }
1 public class BYD extends Car { 2 3 @Override 4 public String getCarName() { 5 return "比亚迪"; 6 } 7 8 }
public class JL extends Car { @Override public String getCarName() { return "吉利"; } }
1 public class CarFactory { 2 3 public static Car getCarName(String name) { 4 Car car = null; 5 switch (name) { 6 case "BYD": 7 car = new BYD(); 8 break; 9 case "JL": 10 car = new JL(); 11 break; 12 default: 13 break; 14 } 15 return car; 16 } 17 }
1 public class CarTest { 2 3 public static void main(String[] args) { 4 Car byd = CarFactory.getCarName("BYD"); 5 System.out.println(byd.getCarName()); 6 Car jl = CarFactory.getCarName("JL"); 7 System.out.println(jl.getCarName()); 8 } 9 }
输出信息:
比亚迪
吉利
标签:String,记录,getCarName,多态,class,JL,Car,public 来源: https://www.cnblogs.com/dnn179/p/12085374.html