zoukankan      html  css  js  c++  java
  • 多态作业

    多态作业

    一、    选择题

    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操作符来判断转型的合法性

    Parent p = new Child();

    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. 向上转型和向下转型

        向上转型:父类的引用指向子类的对象

             向下转型:把父类引用执行的子类对象强制转为子类类型

               注意:无论是基本类型还是引用类型,小转大都是自动的,大转小都是强制的,

    四、    编码题

    1. 编写程序实现乐手弹奏乐器。乐手可以弹奏不同的乐器从而发出不同的声音。可以弹奏的乐器包括二胡、钢琴和琵琶。

      实现思路及关键代码

      1)       定义乐器类Instrument,包括方法makeSound()

      2)       定义乐器类的子类:二胡Erhu、钢琴Piano和小提琴Violin

      3)       定义乐手类Musician,可以弹奏各种乐器play(Instrument i)

      4)       定义测试类,给乐手不同的乐器让他弹奏

     1 public class Instrument {
     2      public void makeSound(){
     3          System.out.println("乐器发出美妙的声音!");
     4      }
     5 
     6 }
     7 public class Erhu extends Instrument {
     8 
     9     @Override
    10     public void makeSound() {
    11         System.out.println("二胡拉响人生");
    12     }
    13 
    14 }
    15 public class Piano extends Instrument {
    16     @Override
    17     public void makeSound() {
    18         System.out.println("钢琴美妙无比");
    19     }
    20 }
    21 
    22 public class Violin extends Instrument {
    23 
    24     @Override
    25     public void makeSound() {
    26         System.out.println("小提琴来啦");
    27     }
    28 
    29 }
    30 public class Musician {
    31     public void paly(Instrument i) {
    32         i.makeSound();
    33     }
    34 
    35     public static void main(String[] args) {
    36         Instrument in =new Erhu();
    37         in.makeSound();
    38         Instrument i =new Piano();
    39         i.makeSound();
    40         Instrument ins =new Violin();
    41         ins.makeSound();
    42         
    43     }
    44 }

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

      实现思路及关键代码

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

      2)       定义比萨类

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

      4)       方法:展示

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

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

      1 public class Pizza {
      2     private String name;
      3     private double price;
      4     private int size;
      5 
      6     public Pizza() {
      7         super();
      8         // TODO Auto-generated constructor stub
      9     }
     10 
     11     public Pizza(String name, double price, int size) {
     12         super();
     13         this.name = name;
     14         this.price = price;
     15         this.size = size;
     16     }
     17 
     18     public String getName() {
     19         return name;
     20     }
     21 
     22     public double getPrice() {
     23         return price;
     24     }
     25 
     26     public int getSize() {
     27         return size;
     28     }
     29 
     30     public void setName(String name) {
     31         this.name = name;
     32     }
     33 
     34     public void setPrice(double price) {
     35         this.price = price;
     36     }
     37 
     38     public void setSize(int size) {
     39         this.size = size;
     40     }
     41 
     42     // 方法:展示
     43     public void display() {
     44     }
     45 
     46 }
     47 public class BaconPizza extends Pizza {
     48     private double gram;
     49 
     50     public BaconPizza() {
     51         super();
     52     }
     53 
     54     public BaconPizza(String name, double price, int size,int gram) {
     55         super(name, price, size);
     56         this.gram = gram;
     57     }
     58 
     59     public double getGram() {
     60         return gram;
     61     }
     62 
     63     public void setGram(double gram) {
     64         this.gram = gram;
     65     }
     66 
     67     @Override
     68     public void display() {
     69         System.out.println("名称:"+super.getName());
     70         System.out.println("价格:"+super.getPrice()+"元");
     71         System.out.println("大小:"+super.getSize()+"寸");
     72         System.out.println("培根克数:"+this.gram+"克");
     73     }
     74     
     75 
     76 }
     77 public class SeaPizza extends Pizza {
     78     private String burdening;
     79 
     80     public SeaPizza() {
     81         super();
     82     }
     83 
     84     public SeaPizza(String name, double price, int size, String burdening) {
     85         super(name, price, size);
     86         this.burdening = burdening;
     87     }
     88 
     89     public String getBurdening() {
     90         return burdening;
     91     }
     92 
     93     public void setBurdening(String burdening) {
     94         this.burdening = burdening;
     95     }
     96 
     97     @Override
     98     public void display() {
     99         System.out.println("名称:" + super.getName());
    100         System.out.println("价格:" + super.getPrice() + "元");
    101         System.out.println("大小:" + super.getSize() + "寸");
    102         System.out.println("配料:" + this.burdening);
    103     }
    104 
    105 }
    106 public class Factory {
    107     Scanner sc = new Scanner(System.in);
    108     public void makePizza(){
    109         System.out.println("请选择想要制作的披萨(1.培根披萨 ;2.海鲜披萨)");
    110         int num = sc.nextInt();
    111         if(num==1){
    112             System.out.println("请输入培根克数:");
    113             int gram = sc.nextInt();
    114             System.out.println("请输入披萨大小:");
    115             int size = sc.nextInt();
    116             System.out.println("请输入披萨价格:");
    117             double price = sc.nextDouble();
    118             Pizza p = new  BaconPizza();
    119             BaconPizza b =(BaconPizza) p;
    120             b.setGram(gram);
    121             p.setName("培根披萨");
    122             p.setSize(size);
    123             p.setPrice(price);
    124             p.display();
    125             System.out.println("
    ");
    126             makePizza();
    127         }else if(num==2){
    128             System.out.println("请输入配料信息:");
    129             String burdening = sc.next();
    130             System.out.println("请输入披萨大小:");
    131             int size = sc.nextInt();
    132             System.out.println("请输入披萨价格:");
    133             double price = sc.nextDouble();
    134             Pizza p = new SeaPizza();
    135             SeaPizza s = (SeaPizza)p;
    136             s.setBurdening(burdening);
    137             p.setName("海鲜披萨");
    138             p.setSize(size);
    139             p.setPrice(price);
    140             p.display();
    141             System.out.println("
    ");
    142             makePizza();
    143         }else{
    144             System.out.println("您输入的数字错误!");
    145             makePizza();
    146         }
    147     }
    148     public static void main(String[] args) {
    149         Factory f = new Factory();
    150         f.makePizza();
    151     }
    152 }

    五、    可选题

    1. 编写程序实现软料购买:编写程序,接收用户输入的信息,选择购买的饮料。可供选择的饮料有:咖啡、矿泉水和可乐。其中,购买咖啡时可以选择:加糖、加奶还是什么都不加。购买可乐时可以选择:买可口可乐还是百事可乐。
      1 public class Drinks {
      2     private String name;
      3     private String burdening;
      4     private int ml;
      5 
      6     public Drinks() {
      7         super();
      8         // TODO Auto-generated constructor stub
      9     }
     10 
     11     public Drinks(String name, String burdening, int ml) {
     12         this.name = name;
     13         this.burdening = burdening;
     14         this.ml = ml;
     15     }
     16 
     17     public String getName() {
     18         return name;
     19     }
     20 
     21     public String getBurdening() {
     22         return burdening;
     23     }
     24 
     25     public int getMl() {
     26         return ml;
     27     }
     28 
     29     public void setName(String name) {
     30         this.name = name;
     31     }
     32 
     33     public void setBurdening(String burdening) {
     34         this.burdening = burdening;
     35     }
     36 
     37     public void setMl(int ml) {
     38         this.ml = ml;
     39     }
     40    //购买信息
     41     public void show(){
     42         System.out.println("您购买饮料的信息如下:");
     43     }
     44 }
     45 46 public class Coffee extends Drinks{
     47 
     48     @Override
     49     public void show() {
     50         System.out.println("您购买饮料的信息如下:");
     51         System.out.println("名称:"+super.getName());
     52         System.out.println("添加配料:"+super.getBurdening());
     53         System.out.println("容量:"+super.getMl());
     54     }
     55    
     56 }
     57 public class Water extends Drinks {
     58 
     59     @Override
     60     public void show() {
     61         System.out.println("您购买饮料的信息如下:");
     62         System.out.println("名称:"+super.getName());
     63         System.out.println("添加配料:"+super.getBurdening());
     64         System.out.println("容量:"+super.getMl());
     65     }
     66 }
     67 public class Cola extends Drinks {
     68     @Override
     69     public void show() {
     70         System.out.println("您购买饮料的信息如下:");
     71         System.out.println("名称:"+super.getName());
     72         System.out.println("添加配料:"+super.getBurdening());
     73         System.out.println("容量:"+super.getMl());
     74     }
     75 }
     76 public class Store {
     77     Scanner sc = new Scanner(System.in);
     78 
     79     public void information() {
     80         System.out.println("请选择饮料:1.咖啡;2.矿泉水;3.可乐");
     81         int num = sc.nextInt();
     82         if (num == 1) {
     83             System.out.println("请输入购买容量:");
     84             int ml = sc.nextInt();
     85             System.out.println("请问是否需要添加配料:1.加糖;2.加奶;3.什么都不加");
     86             int n = sc.nextInt();
     87             if(n==1){
     88                 Drinks d = new Coffee();
     89                 d.setBurdening("加糖");
     90                 d.setMl(ml);
     91                 d.setName("咖啡");
     92                 d.show();
     93                 System.out.println();
     94                 information();
     95             }else if(n==2){
     96                 Drinks d = new Coffee();
     97                 d.setBurdening("加奶");
     98                 d.setMl(ml);
     99                 d.setName("咖啡");
    100                 d.show();
    101                 System.out.println();
    102                 information();
    103             }else{
    104                 Drinks d = new Coffee();
    105                 d.setBurdening("无");
    106                 d.setMl(ml);
    107                 d.setName("咖啡");
    108                 d.show();
    109                 System.out.println();
    110                 information();
    111             }
    112         }else if(num==2){
    113             System.out.println("请输入购买容量:");
    114             int ml = sc.nextInt();
    115             Drinks d = new Water();
    116             d.setName("矿泉水");
    117             d.setMl(ml);
    118             d.setBurdening("无");
    119             d.show();
    120             System.out.println();
    121             information();
    122         }else if(num==3){
    123             System.out.println("请问需要百事可乐还是可口可乐:1.百事可乐;2.可口可乐");
    124             int n = sc.nextInt();
    125             if(n==1){
    126                 System.out.println("请输入购买容量:");
    127                 int ml1 = sc.nextInt();
    128                 Drinks d = new Cola();
    129                 d.setName("百事可乐");
    130                 d.setMl(ml1);
    131                 d.setBurdening("无");
    132                 d.show();
    133                 System.out.println();
    134                 information();
    135             }else{
    136                 System.out.println("请输入购买容量:");
    137                 int ml1 = sc.nextInt();
    138                 Drinks d = new Cola();
    139                 d.setName("可口可乐");
    140                 d.setMl(ml1);
    141                 d.setBurdening("无");
    142                 d.show();
    143                 System.out.println();
    144                 information();
    145             }
    146         }else{
    147             System.out.println("数字输入错误!请从新输入!");
    148             information();
    149         }
    150     }
    151     public static void main(String[] args) {
    152         Store s= new Store();
    153         s.information();
    154         
    155     }
    156 }
  • 相关阅读:
    jsp第三次作业
    软件测试第一次
    jsp第二次作业
    JSP第七次作业
    JSP第六次作业
    JSP第五次作业
    软件测试第二次作业
    JSP第四次作业(二)
    JSP第四次作业(一)
    JSP第三次作业
  • 原文地址:https://www.cnblogs.com/topshark/p/10251908.html
Copyright © 2011-2022 走看看