zoukankan      html  css  js  c++  java
  • 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类中的四个输出语句输出结果依次是()。(选择一项)

     

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

    二、              判断题

    1. 将子类对象赋给父类引用变量,称为向下转型,将无法访问子类特有的方法。( ×  )
    2. 继承是多态的基础,没有继承就没有多态。(√ )

     

    三、              简答题

    1. 多态的含义和作用

    事物的多种形态

    提高代码的维护性(继承保证) ,提高代码的扩展性(由多态保证)

    1. 向上转型和向下转型

    父类引用指向子类对象  将父类引用强制转换为子类引用

    四、              编码题

    1. 编写程序实现比萨制作。需求说明编写程序,接收用户输入的信息,选择需要制作的比萨。可供选择的比萨有:培根比萨和海鲜比萨。

    实现思路及关键代码

    1)         分析培根比萨和海鲜比萨

    2)         定义比萨类

    3)         属性:名称、价格、大小

    4)         方法:展示

    5)         定义培根比萨和海鲜比萨继承自比萨类

    6)         定义比萨工厂类,根据输入信息产生具体的比萨对象

    程序运行结果如图所示

     

     

    import java.util.Scanner;

    class Test {

                    public static void main(String[] args) {

                                    System.out.println("请输入想要制作的披萨(1.培根披萨 2.海鲜披萨)");

                    Scanner sc = new Scanner(System.in);

                    int kind = sc.nextInt();

                                    if (kind == 1) {

                                                    Bacon a = new Bacon();

                                                    System.out.println("输入披萨的价格");

                                                    a.price = sc.nextInt();

                                                    System.out.println("请输入披萨的尺寸");

                                                    a.size = sc.nextInt();

                                                    System.out.println("请输入培根的克数");

                                                    a.weight = sc.nextInt();

                                                    a.print();

                                    }else if (kind == 2) {

                                                    Seafood a = new Seafood();

                                                    System.out.println("输入披萨的价格");

                                                    a.price = sc.nextInt();

                                                    System.out.println("请输入披萨的尺寸");

                                                    a.size = sc.nextInt();

                                                    System.out.println("请输入披萨的配料");

                                                    a.peiLiao = sc.next();     

                                                    a.print();

                                    }else {

                                                    System.out.println("输入有误");

                                    }

                                   

                    }

    }

    class Pizza {

                    String name;

                    int size;

                    int price;

                    public Pizza() {

    }

                    public Pizza(String name,int size,int price){

                                    this.name = name;

                                    this.size = size;

                                    this.price = price;

                    }

                    public void show() {

                                    System.out.println("名称:" + name);

                                    System.out.println("价格:" + price);

                                    System.out.println("尺寸:" + size);

                                   

                    }

    }

    class Bacon extends Pizza {

                    int weight;

                    public Bacon() {

                    super.name = "培根披萨";

                    }

                    public void print() {

                                    super.show();

                                    System.out.println("培根克数:" + weight);

                    }

                   

    }

    class Seafood extends Pizza{

                    String peiLiao;

                    public Seafood() {

                    super.name = "海鲜披萨";

                    }

                    public void print() {

                                    super.show();

                                    System.out.println("配料:" + peiLiao);

                    }

    }

    五、              可选题

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

    程序运行效果如图所示。

     

    import java.util.Scanner;

    class Test1 {

                 public static void main(String[] args) {

                                 System.out.println("请输入想要购买的饮料(1.咖啡 2.矿泉水 3.可乐)");

                 Scanner sc = new Scanner(System.in);

                 int kind = sc.nextInt();

                                 if (kind == 1) {

                                                 Coffee a = new Coffee();

                                                 a.name = "咖啡";

                                                 System.out.println("请输入容量");

                                                 a.volume = sc.nextInt();

                                                 System.out.println("请问是否要配料(1.加糖 2.加奶 3.啥也不加)");

                                                 int x = sc.nextInt();

                                                                 if (x == 1) {

                                                                                 a.ingredient = "加糖";

                                                                 }else if (x == 2) {

                                                                                 a.ingredient = "加奶";

                                                                 }else {

                                                                                 a.ingredient = "啥也不加";

                                                                 }

                                                 a.print();

                                 }else if (kind == 2) {

                                                 Water a = new Water();

                                                 a.name = "矿泉水";

                                                 System.out.println("请输入容量");

                                                 a.volume = sc.nextInt();

                                                 a.print();

                                 }else {

                                                 Cola a = new Cola();

                                                 a.name = "可乐";

                                                 System.out.println("请输入类型(1.可口可乐 2.百事可乐)");

                                                 int t = sc.nextInt();

                                                                 if (t == 1) {

                                                                                 a.type = "可口可乐";

                                                                 }else {

                                                                                 a.type = "百事可乐";

                                                                 }

                                                 System.out.println("请输入容量");

                                                 a.volume = sc.nextInt();

                                                 a.print();

                                 }

                                

                 }

    }

    class Water {

                 String name;

                 int volume;

                

                 public Water(){}

                 public Water(String name,int volume){

                                 this.name = name;

                                 this.volume = volume;

                 }

                 public void print() {

                                 System.out.println("名称:" + name);

                                 System.out.println("容量:" + volume);

                                

                 }

    }

    class Coffee extends Water {

                 String ingredient;

                 public Coffee() {

                

                 }

                 public void print() {

                                 super.print();

                                 System.out.println("配料:" + ingredient);

                 }

                

    }

    class Cola extends Water{

                 String type;

                 public Cola() {

                

                 }

                 public void print() {

                                 super.print();

                                 System.out.println("类型:" + type);

                 }

    }

  • 相关阅读:
    WPF Window对象的生命周期
    MVC 控制器中传递dynamic(对象) 给视图
    MVC 获取路由的 URL 参数值和默认值的集合。
    mvc路由配置.html结尾的伪静态
    javascript 模拟按键点击提交
    微信小程序调用接口返回数据或提交数据
    清理电脑文件夹中的Thumbs.db文件
    asp.net动态增加服务器端控件并提交表单
    c# asp.net 实现分页(pager)功能
    注册时发短信如何防止别人恶意调用短信接口
  • 原文地址:https://www.cnblogs.com/houjx/p/9368238.html
Copyright © 2011-2022 走看看