Test 类
在这里插入代码片package test_1106;
import java.util.Scanner;
/**
*
* @author Condi
* @Date 2020年11月6日
*/
public class Test_Phone {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
System.out.println("请输入类型名字价格,类型(智能或者旧)");
String a = scanner.next();
String b = scanner.next();
double c = scanner.nextDouble();
Factory factory = new Factory();
Phone p1 = factory.Factory(a);
p1.setName(b);
p1.setType(a);
p1.setPrice(c);
System.out.println(p1.toString());
}
}
工厂类
package test_1106;
public class Factory {
public static Phone Factory(String op) {
Phone Phone = null;
switch (op) {
case "智能":
Phone = new Zhi();
break;
case "旧":
Phone = new Old();
break;
default:
break;
}
return Phone;
}
}