作业



 

 

 

 

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

 

 

posted @ 2019-07-18 11:36  AxeBurner  阅读(119)  评论(0编辑  收藏  举报