zoukankan      html  css  js  c++  java
  • 作业



    父类Pet

    package cn.d;
    
    public abstract class Pet {
        private String name;
        private int health;
        private int love;
    //    public Pet() {}
    //    public Pet(String name,int health,int love) {
    //        
    //        
    //        this.name = name;
    //        this.health = health;
    //        this.love = love;
    //    }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getHealth() {
            return health;
        }
        public void setHealth(int health) {
            this.health = health;
        }
        public int getLove() {
            return love;
        }
        public void setLove(int love) {
            this.love = love;
        }
    
        
        //显示宠物信息
        public void print() {
            
            System.out.println("宠物的自白:");
            System.out.println("我的姓名是:"+this.name+" "
            +"我的健康值是:"+this.health+" "+"与主人的亲密程度是:"+this.love);
        }
        //宠物游泳的方法
        public abstract void swim();
        
        public void play() {
            System.out.println("主人带宠物去玩耍");
            
        }
    }

    子类Dog类

    package cn.d;
    
    public class Dog extends Pet{
    
        private String strain = "雪纳瑞";
        
        public String getStrain() {
            return strain;
        }
    
        public void setStrain(String strain) {
            this.strain = strain;
        }
    
    //    public Dog(String name,int health,int love) {
    //        
    //        super(name,health,love);
    //    }
        //显示品种
        public void print() {
            super.print();
            System.out.println("我的品种是"+this.strain);
        }
        
        public void swim() {
            System.out.println("带"+this.getName()+"去游泳");
        }
        
        @Override
        public void play() {
            super.play();
            System.out.println(this.getName()+"去叼飞碟");
        }
        
        
    }

    子类PunGuin类

    package cn.d;
    
    public class PenGuin extends Pet{
        
        private String sex = "Q仔";
        
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
    //    public PenGuin(String name,int health,int love) {
    //        
    //        
    //        super(name,health,love);
    //    }
    //    
        public void print() {
            super.print();
            System.out.println("我的性别是:"+this.sex);
        }
        
        public void swim() {
            System.out.println("带"+this.getName()+"去游泳");
        }
        @Override
        public void play() {
            super.play();
            System.out.println(this.getName()+"去溜冰");
        }
        
    
    }

    主人Master 类

    package cn.d;
    
    public class Master {
    
        
        public void getSwim(Pet pet) {
            
            
            pet.swim();
            
            
            
        }
        
        public void play(Pet pet) {
            
            
            pet.play();
            
        }
        
        
    }

    测试类

    package cn.d;
    
    import java.util.Scanner;
    
    public class TestPet {
            static Scanner sc = new Scanner(System.in);
        public static void main(String[] args) {
            
            Pet pet = new PenGuin();
            
            pet.setName("马化疼");
            pet.setHealth(100);
            pet.setLove(100);    
            pet.print();
            
            Pet pet1 = new Dog();
            pet1.setName("哆啦A梦");
            pet1.setHealth(100);
            pet1.setLove(100);    
            pet1.print();
            System.out.println("您要带哪只宠物去游泳?(1、狗狗/2、企鹅)");
            int choose = sc.nextInt();
            Master master = new Master();
        switch(choose) {
        
        case 1:
            master.getSwim(pet1);
            break;
        case 2:
            master.getSwim(pet);
            
            break;
            
            
        }
        System.out.println("您要带哪只宠物去玩耍?(1、狗狗/2、企鹅)");
        int choose1 = sc.nextInt();
        
    switch(choose1) {
    
    case 1:
        master.play(pet1);
        break;
    case 2:
        master.play(pet);
        
        break;
        
        
    }
        }
        
        
    }

  • 相关阅读:
    SAP MM 采购发票上的金额小差异
    SAP MM 物料号到物料的库存转移过账里的差异
    SAP MM 采购附加费在收货以及发票过账时候的会计分录
    SAP MM 移动平均价的商品发票价格和采购订单价格差异的处理
    WPF 使用 VisualBrush 在 4k 加 200 DPI 设备上某些文本不渲染看不见问题
    dotnet 写一个支持层层继承属性的对象
    dotnet OpenXML 读取 PPT 内嵌 xlsx 格式 Excel 表格的信息
    WPF 在 .NET Core 3.1.19 版本 触摸笔迹偏移问题
    linux下将编译错误输出到一个文本文件
    浮点型(FLOAT)与CHAR型转换
  • 原文地址:https://www.cnblogs.com/bichen-01/p/11206235.html
Copyright © 2011-2022 走看看