zoukankan      html  css  js  c++  java
  • 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());
        }
    }
  • 相关阅读:
    Sharepoint 2013默认dll部署位置
    Sharepoint 2010 Form认证自定义登录页面,总是自动登录问题
    cocos打包后页面在ios浏览器、安卓钉钉等部分app中横竖屏问题
    cocos构建出来的 Web Mobile 项目在微信开发者工具里面无法点击
    git 合并某个提交commit到指定的分支上
    cc.sys.localStorage存储和读取用户数据
    cocos creater关于blend,关于预乘premultiply alpha,关于图片白边灰边的问题
    cocos遮罩层点击穿透问题解决
    postman中 form-data、x-www-form-urlencoded、raw、binary操作
    ‘webpack-dev-server' 不是内部或外部命令,也不是可运行的程序
  • 原文地址:https://www.cnblogs.com/lyx210019/p/9319399.html
Copyright © 2011-2022 走看看