• day11作业


    一.选择题

    1.B 2.D 3.AB 4.C

    二.判断题

    1.× 2.√

    三.简答题

    1.

    多态就是事物存在的多种形态。

    提高程序的复用性,提高程序的可扩展性和可维护性。

    2.

    向上转型是指父类引用指向子类对象,向下转型是指子类引用指向父类对象。

    import java.util.Scanner;
    
    class Test5_Pizza {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请选择想要制作的披萨(1.培根披萨 2.海鲜披萨):");
            int choice = sc.nextInt();
            System.out.println("请输入培根克数:");
            int gram = sc.nextInt();
            System.out.println("请输入披萨大小:");
            int size = sc.nextInt();
            System.out.println("请输入披萨价格:");
            int price = sc.nextInt();
            if (choice == 1) {
                PeiGenPizza p1 = new PeiGenPizza(gram,size,price);
                p1.show1();
            }else if(choice == 2){
                HaiXianPizza p2 = new HaiXianPizza(gram,size,price);
                p2.show2();
            }
        }
    }
    
    class Pizza {
        private int gram;
        private int size;
        private int price;
        public Pizza(){}
        public Pizza(int gram,int size,int price){
            this.gram = gram;
            this.size = size;
            this.price = price;
        }
        public void setGram(int gram){
            this.gram = gram;
        }
        public int getGram(){
            return gram;
        }
        public void setSize(int size){
            this.size = size;
        }
        public int getSize(){
            return size;
        }
        public void setPrice(int price){
            this.price = price;
        }
        public int getPrice(){
            return price;
        }
    }
    
    class PeiGenPizza extends Pizza {
        public PeiGenPizza(){}
        public PeiGenPizza(int gram,int size,int price){
            super(gram,size,price);
        }
        public void show1(){
            System.out.println("名称:培根披萨" + "
    " + "价格:" + getPrice() + "元" + "
    " + "大小:" + getSize() + "寸" + "
    " + "培根克数:" + getGram() + "克");
        }
    }
    
    class HaiXianPizza extends Pizza {
        public HaiXianPizza(){}
        public HaiXianPizza(int gram,int size,int price){
            super(gram,size,price);
        }
        public void show2(){
            System.out.println("名称:海鲜披萨" + "
    " + "价格:" + getPrice() + "元" + "
    " + "大小:" + getSize() + "寸" + "
    " + "培根克数:" + getGram() + "克");
        }
    }
    import java.util.Scanner;
    
    class Test6_Drink {
        public static void main(String[] args) {
            Scanner sc  = new Scanner(System.in);
            System.out.println("请选择饮料(1.咖啡 2.矿泉水 3.可乐):");
            int choice = sc.nextInt();
            System.out.println("请输入购买容量:");
            int capacity = sc.nextInt();
            System.out.println("您购买饮料信息如下。");
            if (choice == 1) {
                System.out.println("请问是否需要配料(1.加糖 2.加奶 3.什么都不加):");
                int add = sc.nextInt();
                Coffee c1 = new Coffee();
                c1.setCapacity(capacity);
                switch (add) {
                case 1:
                    c1.setAdd("加糖");
                break;
                case 2:
                    c1.setAdd("加奶");
                break;
                case 3:
                    c1.setAdd("什么都不加");
                break;
                }
                c1.show1();
            }else if(choice == 2){
                SpringWater s = new SpringWater(capacity); 
                s.show2();
            }else if (choice == 3) {
                System.out.println("请选择(1.可口可乐 2.百事可乐):");
                int brand = sc.nextInt();
                Cola c2 = new Cola();
                c2.setCapacity(capacity);
                switch (brand) {
                case 1:
                    c2.setBrand("可口可乐");
                break;
                case 2:
                    c2.setBrand("百事可乐");
                break;
                }
                c2.show3();
            }
        }
    }
    
    class Drink {
        private int capacity;
        public Drink(){}
        public Drink(int capacity){
            this.capacity = capacity;
        }
        public void setCapacity(int capacity){
            this.capacity = capacity;
        }
        public int getCapacity(){
            return capacity;
        }
    }
    
    class Coffee extends Drink {
        private String add;
        public Coffee(){}
        public Coffee(int capacity,String add){
            super(capacity);
            this.add = add;
        }
        public void setAdd(String add){
            this.add = add;
        }
        public String getAdd(){
            return add;
        }
        public void show1(){
            System.out.println("名称:咖啡" + "
    " + "添加配料:" + getAdd() + "
    " + "容量:" + getCapacity());
        }
    }
    
    class SpringWater extends Drink {
        public SpringWater(){}
        public SpringWater(int capacity){
            super(capacity);
        }
        public void show2(){
            System.out.println("名称:矿泉水" + "
    " +  "容量:" + getCapacity());
        }
    }
    
    class Cola extends Drink {
        private String brand;
        public Cola(){}
        public Cola(int capacity,String brand){
            super(capacity);
            this.brand = brand;
        }
        public void setBrand(String brand){
            this.brand = brand;
        }
        public String getBrand(){
            return brand;
        }
        public void show3(){
            System.out.println("名称:" + getBrand() + "
    " +  "容量:" + getCapacity());
        }
    }
  • 相关阅读:
    LDAP+Jenkins
    LDAP+SASL+SVN
    Ubuntu下的LDAP服务搭建
    LDAP+Confluence
    LDAP+Nextcloud
    vim -d
    linux默认编辑器
    vim相关调优
    MySQL中的保留字
    RHEL7配置ip地址
  • 原文地址:https://www.cnblogs.com/lyx210019/p/9319399.html
Copyright © 2020-2023  润新知