day10作业-韩明琰


多态作业

⦁ 选择题

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. 编译错误

3. 在Java中,关于引用数据类型的类型转换说法正确的是(AB     )。(选择二项)
  
 A 引用数据类型的类型转换有向上转型和向下转型
 B. 向下转型,必须转换成其真实子类型,而不能随意转换
 C. 向下转型是自动进行的,也称隐式转换
 D. 向上转型可以使用instanceof操作符来判断转型的合法性


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. true,true,false,编译错误

⦁ 判断题
⦁ 将子类对象赋给父类引用变量,称为向下转型,将无法访问子类特有的方法。( false  )
⦁ 继承是多态的基础,没有继承就没有多态。(true   )

⦁ 简答题
⦁ 多态的含义和作用

多态就是事物存在的多种形态,多态的前提是要有继承关系,要有方法重写,要有父类引用指向子类对象,当你用此引用调用方法时,在编译阶段看的是父类,代码运行阶段调用的是子类重写的父类方法,动态绑定,这个过程就叫多态。而成员方法编译和运行都看左边(父类)。

作用:提高了代码的维护性(继承保证)

           提高了代码的扩展性(多态保证)

           可以当作形式参数,可以接受任意子类对象
⦁ 向上转型和向下转型

向上转型:父类引用指向子类对象Person p = new SuperMan();

向下转型:SuperMan() = (SuperMan)p;一定要先有向上转型才能有向下转型

⦁ 编码题
⦁ 编写程序实现比萨制作。需求说明编写程序,接收用户输入的信息,选择需要制作的比萨。可供选择的比萨有:培根比萨和海鲜比萨。
实现思路及关键代码
⦁ 分析培根比萨和海鲜比萨
⦁ 定义比萨类
⦁ 属性:名称、价格、大小
⦁ 方法:展示
⦁ 定义培根比萨和海鲜比萨继承自比萨类
⦁ 定义比萨工厂类,根据输入信息产生具体的比萨对象
程序运行结果如图所示

//测试类

package com.zkc.test;

import java.util.Scanner;

public class Demo3 {
 public static void main(String[] args) {
  Pizza_Factory p = new Pizza_Factory();
  p.proPizza();
 }

}

//披萨类

package com.zkc.test;

public class Pizza {
 String name;
 int price;
 int size;
 public void show() {
  System.out.println("名称:"+name);
  System.out.println("价格:"+price+"元");
  System.out.println("大小:"+size+"寸");
 }

}

//海鲜披萨类

package com.zkc.test;

public class HaixianPizza extends Pizza {
 String ingredient;
 public void show() {
  name = "海鲜披萨";
  super.show();
  System.out.println("配料:"+ingredient);
 }
}

//培根披萨类

package com.zkc.test;

public class PeigenPizza extends Pizza{
 int weight;
 public void show() {
  name = "培根披萨";
  super.show();
  System.out.println("培根克数:"+weight);
 }

}

//披萨工厂类

package com.zkc.test;

import java.util.Scanner;

public class Pizza_Factory {
 public void proPizza() {
  Scanner sc = new Scanner(System.in);
  System.out.println("请输入想要制作的披萨(1.培根披萨2.海鲜披萨):");
  int choose = sc.nextInt();
  switch(choose) {
  case 1:
   PeigenPizza p = new PeigenPizza();
   System.out.println("请输入培根克数:");
   p.weight = sc.nextInt();
   System.out.println("请输入披萨大小:");
   p.size = sc.nextInt();
   System.out.println("请输入披萨价格:");
   p.price = sc.nextInt();
   p.show();
   break;
  case 2:
   HaixianPizza h = new HaixianPizza();
   System.out.println("请输入配料信息:");
   h.ingredient = sc.next();
   System.out.println("请输入披萨大小:");
   h.size = sc.nextInt();
   System.out.println("请输入披萨价格:");
   h.price = sc.nextInt();
   h.show();
   break;
   default:
    System.out.println("输入有误");
    break;
  }
 }

}

 
 

⦁ 可选题

⦁ 编写程序实现软料购买:编写程序,接收用户输入的信息,选择购买的饮料。可供选择的饮料有:咖啡、矿泉水和可乐。其中,购买咖啡时可以选择:加糖、加奶还是什么都不加。购买可乐时可以选择:买可口可乐还是百事可乐。
程序运行效果如图所示。

//测试类购买饮料

package com.zkc.test;

import java.util.Scanner;

public class Demo4 {
 public static void main(String[] args) {
  System.out.println("请选择饮料(1.咖啡2.矿泉水3.可乐):");
  Scanner sc = new Scanner(System.in);
  int choose = sc.nextInt();
  switch(choose) {
  case 1:
   Coffee c = new Coffee();
   System.out.println("请输入要购买的容量:");
   c.capacity = sc.nextInt();
   System.out.println("请问是否要配料:(1.加糖2.加奶3.什么都不加)");
   int num = sc.nextInt();
   switch(num) {
   case 1:
    c.ingredient = "加糖";
    break;
   case 2:
    c.ingredient = "加奶";
    break;
   case 3:
    c.ingredient = "什么都不加";
    break;
    default:
     System.out.println("输入有误");
     break;
   }
   c.show();
   break;
  case 2:
   Water w = new Water();
   System.out.println("请输入要购买的容量:");
   w.capacity = sc.nextInt();
   w.show();
   break;
  case  3:
   Cola co = new Cola();
   System.out.println("请输入要购买的容量:");
   co.capacity = sc.nextInt();
   System.out.println("请输入要购买的品牌(1.百事可乐2.可口可乐):");
   int n = sc.nextInt();
   switch(n) {
   case 1:
    co.brand = "百事可乐";
    break;
   case 2:
    co.brand = "可口可乐";
    break;
    default:
     System.out.println("输入有误");
   }
   co.show();
  }
 }

}

//饮料类

package com.zkc.test;

public class drink {
 String name;
 int capacity;
 public void show() {
  System.out.println("您购买的饮料信息如下。");
  System.out.println("名称:"+name);
  System.out.println("容量:"+capacity);
  
 }

}

//咖啡类

package com.zkc.test;

public class Coffee extends drink {
 String ingredient;
 public void show() {
  name = "咖啡";
  super.show();
  System.out.println("添加配料:"+ingredient);
 }
}

//矿泉水类

package com.zkc.test;

public class Water extends drink {
 public Water() {
  name = "矿泉水";
 }
}

//可乐类

package com.zkc.test;

public class Cola extends drink {
 String brand;
 public void show() {
  name = "可乐";
  super.show();
  System.out.println("品牌:"+brand);
 }

}


 

原文地址:https://www.cnblogs.com/hmyhh/p/9368065.html