• 7.16 作业 韩宗轩


     

    多态作业

    一、    选择题

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

    二、    判断题

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

     

    三、    简答题

    1. 多态的含义和作用
      事物存在的多种形态。

    多态不仅可以减少代码量,还可以提高代码的可扩展性和可维护性

    1. 向上转型和向下转型

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

             SuperMan sm = (SuperMan)p;向下转型 : 将子类对象赋给父类引用变量

    **记住 一定是先有向上转型之后才有向下转型**

    四、    编码题

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

    实现思路及关键代码

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

    2)         定义比萨类

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

    4)         方法:展示

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

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

    程序运行结果如图所示

                                         

     

     

    public class TestMain5 {

            public static void main(String[] args) {

                    // TODO Auto-generated method stub

                    Pizza pizza = PizzaFactory.getMyPizz();

                    if(pizza != null){

                            pizza.show();

                    }         

            }

    }

    package com.polymorphism.task;

    import java.util.Scanner;

    public class PizzaFactory {

            

            public static Pizza getMyPizz(){

                    Pizza pizza = null;

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

                    @SuppressWarnings("resource")

                    Scanner sca = new Scanner(System.in);

                    int temp = sca.nextInt();

                    if(temp == 1){

                            BaconPizza pizza1 = new BaconPizza();

                            pizza1.setName("培根披萨");

                            

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

                            int temp2 = sca.nextInt();

                            pizza1.setBaconWeight(temp2);

                            

                            System.out.println("请输入披萨大小:");

                            temp2 = sca.nextInt();

                            pizza1.setSize(temp2);

                            

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

                            temp2 = sca.nextInt();

                            pizza1.setPrice(temp2);

                            

                    return pizza1; 

                    }

                    else if(temp == 2){

                            SeafoodPizza pizza2 = new SeafoodPizza();

                            pizza2.setName("海鲜披萨");

                            

                            System.out.println("请输入配料信息:");

                            String temp3 = sca.next();

                            pizza2.setIngredient(temp3);

                            

                            System.out.println("请输入披萨大小:");

                            int temp2 = sca.nextInt();

                            pizza2.setSize(temp2);

                            

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

                            temp2 = sca.nextInt();

                            pizza2.setPrice(temp2);

                            

                    return pizza2; 

                    }

                    else {

                            System.out.println("没有选择种类!");

                            return null;

                    }

            }

    }

    package com.polymorphism.task;

    public class Pizza {

            private String name;

            private int price;

            private int size;      

            public String toString(Pizza pizza){

                    return "名称: "+pizza.getName()+" "+"价格: "+pizza.getPrice()+"元"+" "+"大小: "+pizza.getSize()+"寸";

            }

            

            public void show(){

                    System.out.println(this.toString(this));

            }

            

            public String getName() {

                    return name;

            }

            public void setName(String name) {

                    this.name = name;

            }

            public int getPrice() {

                    return price;

            }

            public void setPrice(int price) {

                    this.price = price;

            }

            public int getSize() {

                    return size;

            }

            public void setSize(int size) {

                    this.size = size;

            }

    }

    package com.polymorphism.task;

    public class SeafoodPizza extends Pizza{

            private String ingredient;

                    

            public String toString(SeafoodPizza seafoodpizza){

                    return super.toString(seafoodpizza)+" "+"配料:"+seafoodpizza.getIngredient();

            }

            public void show(){

                    System.out.println(this.toString(this));

            }

            

            public String getIngredient() {

                    return ingredient;

            }

            public void setIngredient(String ingredient) {

                    this.ingredient = ingredient;

            }

    }

    package com.polymorphism.task;

    public class BaconPizza extends Pizza {

            

            private int baconWeight;

            public String toString(BaconPizza baconpizza){

                    return super.toString(baconpizza)+" "+"培根克数:"+baconpizza.getBaconWeight();

            }

            public void show(){

                    System.out.println(this.toString(this));

            }

            

            public int getBaconWeight() {

                    return baconWeight;

            }

            public void setBaconWeight(int baconWeight) {

                    this.baconWeight = baconWeight;

            }

    }

    五、    可选题

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

    程序运行效果如图所示。

     

    package cn.pxd.drink;

    public class Drink {

    private double buyNum;

    private String name;

    public double getBuyNum() {

    return buyNum;

    }

    public void setBuyNum(double buyNum) {

    this.buyNum = buyNum;

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    void show(){

    }

    }

    package cn.pxd.drink;

    import java.util.Scanner;

    public class Coffee extends Drink{

    static String add;

    void show(){

    System.out.println("您购买饮料信息如下。 名称: "+this.getName()+" 添加配料: "+

    this.add+" 容量: "+this.getBuyNum());

    }

    }

    package cn.pxd.drink;

    public class Water extends Drink{

    }

    package cn.pxd.drink;

    public class Colar extends Drink{

    static String type;

    void show(){

    System.out.println("可乐种类: "+Colar.type);

    }

    }

    package cn.pxd.drink;

    import java.util.Scanner;

    public class DrinkFctory {

    public static Drink selectDrink(int which){

    Scanner sc=new Scanner(System.in);

    Drink d= null;

    if(which==1){

    d=new Coffee();

    d.setName("咖啡");

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

    d.setBuyNum(sc.nextDouble());

    System.out.println(" 请问是否要配料: (1、加糖 2、加奶 3、什么都不加)");

    int n=sc.nextInt();

    switch(n){

    case 1: Coffee.add="加糖";

    break;

    case 2: Coffee.add="加奶";

    break;

    case 3: Coffee.add="什么都不加";

    break;

    default:break;

    }

    }else if(which==2){

    d=new Water();

    d.setName("矿泉水");

    }else if(which==3){

    d=new Colar();

    d.setName("可乐");

    System.out.println("请选择牌子 (1、可口可乐 2、百事可乐)");

    int n=sc.nextInt();

    switch(n){

    case 1: Colar.type="可口可乐";

    break;

    case 2: Colar.type="百事可乐";

    break;

    default:break;

    }

    }

    return d;

    }

    }

    package cn.pxd.drink;

    import java.util.Scanner;

    public class TestDrink {

    public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);

    System.out.println("请选择饮料 (1、咖啡 2、矿泉水 3、可乐): ");

    int n=sc.nextInt();

    Drink d=DrinkFctory.selectDrink(n);

    d.show();

    }

    }

  • 相关阅读:
    JAVA 调用https接口, java.security.cert.CertificateException
    Java泛型用法总结
    深入探索 Java 热部署
    单例模式
    Java中的事务——JDBC事务和JTA事务
    常见的网站攻击手段及预防措施
    JAVA 动态代理原理和实现
    详解 CAP 定理 Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性)
    Set
    List
  • 原文地址:https://www.cnblogs.com/Hveritas/p/9320728.html
Copyright © 2020-2023  润新知