• 作业




    父类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;
        
        
    }
        }
        
        
    }

  • 相关阅读:
    C#中upd分包与发送,已经实现全部代码
    Jmeter字体大小、背景色
    Linux查看日志 tail -f , grep
    xshell如何选中即复制,右键即粘贴
    Jmeter如何做接口测试
    Jmeter 线程数、ramp-up period (in seconds)、循环次数
    java反射机制入门01
    java实现文件夹(包括其中的子文件夹、子文件)的复制——递归
    ViewPager实现广告自动轮播核心代码(Handler+Thread)
    ViewPager实现启动引导页面(个人认为很详细)
  • 原文地址:https://www.cnblogs.com/bichen-01/p/11206235.html
Copyright © 2020-2023  润新知