多态作业
一、选择题
1. | 关于Java中的多态,以下说法不正确的为( B )。(选择一项) | |
A | 多态不仅可以减少代码量,还可以提高代码的可扩展性和可维护性 | |
B. | 把子类转换为父类,称为向下转型,自动进行类型转换 | |
C. | 多态是指同一个实现接口,使用不同的实例而执行不同的操作 | |
D. | 继承是多态的基础,没有继承就没有多态 |
2. | 编译运行如下Java代码,输出结果是( D )。(选择一项) | |
class Base { public void method(){ System.out.print ("Base method"); } } class Child extends Base{ public void methodB(){ System.out.print ("Child methodB"); } } class Sample { public static void main(String[] args) { Base base= new Child(); base.methodB(); } } | ||
A | Base method | |
B. | Child methodB | |
C. | Base method Child methodB | |
D. | 编译错误 |
4. | 给定如下Java程序,Test类中的四个输出语句输出结果依次是( C )。(选择一项) | |
class Person { String name="person"; public void shout(){ System.out.println(name); } } class Student extends Person{ String name="student"; String school="school"; } public class Test { public static void main(String[ ] args) { Person p=new Student(); //用来测试一个对象是否为一个类的实例 System.out.println(p instanceof Student); System.out.println(p instanceof Person); System.out.println(p instanceof Object); System.out.println(p instanceof System); } } | ||
A | true,false,true,false | |
B. | false,true,false,false | |
C. | true,true,true,编译错误 | |
D. | 编译错误 |
二、判断题
- 将子类对象赋给父类引用变量,称为向下转型,将无法访问子类特有的方法。( × )
- 继承是多态的基础,没有继承就没有多态。( √ )
三、简答题
1、多态的含义和作用
(1)含义:子类继承父类,子类对象对父类对象进行重写,父类引用指向子类对象这三个条件前 提下父类引用对子类方法中重写方法的调用,“一个接口,多种实现”。
(2)作用:
(1) 应用程序不必为每一个派生类编写功能调用,只需要对抽象基类进行处理即可。大大提高程序的可复用性。//继承
(2)派生类的功能可以被基类的方法或引用变量所调用,这叫向后兼容,可以提高可扩充性和可维护性。 //多态的真正作用,以前需要用switch实现
2、向上转型和向下转型是什么意思
(1)向上转型:通俗地讲即是将子类对象转为父类对象。此处父类对象可以是接口。
(2)向下转型:将父类对象指向子类对象.
四、编程题
1、 编写程序实现比萨制作。需求说明编写程序,接收用户输入的信息,选择需要制作的比萨。可供选择的比萨有:培根比萨和海鲜比萨。
实现思路及关键代码
1) 分析培根比萨和海鲜比萨
2) 定义比萨类
3) 属性:名称、价格、大小
4) 方法:展示
5) 定义培根比萨和海鲜比萨继承自比萨类
6) 定义比萨工厂类,根据输入信息产生具体的比萨对象
程序运行结果如图所示
abstract class Pizza {
private String name;
private double price;
private int size;
public Pizza() {
}
public Pizza(String name, double price, int size) {
this.name = name;
this.price = price;
this.size = size;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
class BaconPizza extends Pizza {
private int number;
public BaconPizza() {
this.setName("培根比萨");
}
public BaconPizza(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public BaconPizza(String name, double price, int size, int number) {
super(name, price, size);
this.number = number;
}
public void show() {
System.out.println("名称: " + this.getName() + "\n价格: " + this.getPrice() + "元\n大小: " +
this.getSize() + "\n培根克数: " + this.number);
}
}
class SeaPizza extends Pizza {
private String ingredient;
public String getIngredient() {
return ingredient;
}
public void setIngredient(String ingredient) {
this.ingredient = ingredient;
}
public SeaPizza(String ingredient) {
this.ingredient = ingredient;
}
public SeaPizza(String name, double price, int size, String ingredient) {
super(name, price, size);
this.ingredient = ingredient;
}
public SeaPizza() {
this.setName("海鲜比萨");
}
public void show() {
System.out.println("名称: " + this.getName() + "\n价格: " + this.getPrice() + "元\n大小: " +
this.getSize() + "寸\n配料: " + this.ingredient);
}
}
public class Test1 {
public static void main(String[] args) {
System.out.println("请选择想要制作的披萨(1.培根披萨 2.海鲜披萨)");
Scanner scanner = new Scanner(System.in);
int number2 = scanner.nextInt();
if (number2 == 1) {
Pizza p = new BaconPizza();
Scanner sc = new Scanner(System.in);
System.out.println("请输入培根克数: ");
((BaconPizza)p).setNumber(sc.nextInt());
System.out.println("请输入比萨大小: ");
p.setSize(sc.nextInt());
System.out.println("请输入比萨价格: ");
p.setPrice(sc.nextInt());
((BaconPizza) p).show();
} else if (number2 == 2) {
Pizza p1 = new SeaPizza();
Scanner sc = new Scanner(System.in);
System.out.println("请输入配料信息: ");
((SeaPizza)p1).setIngredient(sc.next());
System.out.println("请输入比萨大小: ");
p1.setSize(sc.nextInt());
System.out.println("请输入比萨价格: ");
p1.setPrice(sc.nextInt());
((SeaPizza) p1).show();
}
}
2、编写程序实现软料购买:编写程序,接收用户输入的信息,选择购买的饮料。可供选择的饮料有:咖啡、矿泉水和可乐。其中,购买咖啡时可以选择:加糖、加奶还是什么都不加。购买可乐时可以选择:买可口可乐还是百事可乐。
程序运行效果如图所示
class Drink {
private String name;
private int volume;
public Drink() {
super();
}
public Drink(String name, int volume) {
super();
this.name = name;
this.volume = volume;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public void show() {
System.out.println("您购买的饮料信息如下:");
System.out.println("名称:" + name);
System.out.println("容量:" + volume);
}
}
class Coffee extends Drink {
private String mixture;//添加物
public Coffee() {
super();
}
public Coffee(String name, int volume, String mixture) {
super(name, volume);
this.mixture = mixture;
}
public String getMixture() {
return mixture;
}
public void setMixture(String mixture) {
this.mixture = mixture;
}
public void show() {
System.out.println("您购买的饮料信息如下:");
System.out.println("名称:" + super.getName());
System.out.println("添加配料:" + this.getMixture());
System.out.println("容量:" + super.getVolume());
}
}
//可乐
class Coco extends Drink {
public Coco() {
}
public Coco(String name, int volume) {
super(name, volume);
}
@Override
public void show() {
System.out.println("你购买的饮料信息如下");
System.out.println("姓名:" + getName());
System.out.println("容量:" + getVolume());
}
}
class Water extends Drink {
public Water() {
}
public Water(String name, int volume) {
super(name, volume);
}
public void show() {
System.out.println("您购买的饮料信息如下:");
System.out.println("名称:" + super.getName());
System.out.println("容量:" + super.getVolume());
}
}
class DrinkFactory {
public static Drink getDrink(String no) {
Scanner sc = new Scanner(System.in);
Drink drink = null;
String name;
if ("1".equals(no)) {
System.out.println("请输入购买容量:");
int volume = sc.nextInt();
System.out.println("请问是否要配料(1、加糖 2、加奶 3、什么也不加)");
int choice = sc.nextInt();
String mixture;
switch (choice) {
case 1:
mixture = "加糖";
break;
case 2:
mixture = "加奶";
break;
default:
mixture = "什么一个不加";
}
drink = new Coffee("咖啡", volume, mixture);
} else if ("2".equals(no)) {
System.out.println("请输入购买容量:");
int volume = sc.nextInt();
System.out.println("请输入你需要购买的可乐:1.百事可乐 2.可口可乐");
int choose = sc.nextInt();
switch (choose) {
case 1:
drink = new Coco("百事可乐", volume);
break;
case 2:
drink = new Coco("可口可乐", volume);
break;
}
} else if ("3".equals(no)) {
System.out.println("请输入购买容量:");
int volume = sc.nextInt();
drink = new Water("矿泉水", volume);
}
return drink;
}
}
public class Test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请选择饮料(1.咖啡 2 可乐 3 矿泉水):");
String choice = sc.next();
Drink drink = DrinkFactory.getDrink(choice);
drink.show();
}
}