宠物类例题

public class Person {
     private String name;
     private Pet pet;
     
     public Person() {}
     public Person(String name,Pet pet){
         this.name=name;
         this.pet=pet;     
     }
     public void petHappy(){
         pet.scream();
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     
}
public abstract class Pet {
    private String pname;
    
    public Pet(){}
    public Pet(String pname){
        this.pname = pname;
    }
    public abstract void scream();/*{
        System.out.println("宠物的叫声");
    }*/
    
    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }
}
public class Tiger extends Pet{
    public String fur;

    public Tiger(String fur,String name) {
        super(name);
        this.fur = fur;
    }

    public Tiger() {
        super();
        
    }
    

    @Override
    public void scream() {
        System.out.println("这是一只老虎的叫声");
    }

    public String getFur() {
        return fur;
    }

    public void setFur(String fur) {
        this.fur = fur;
    }
    
    
}
public class Main {
    public static void main(String[] args){
        Tiger t1 =new Tiger();
        Rabbit t2 = new Rabbit();
        
        Person p1 = new Person("小明",t1);
        Person p2 = new Person("小红",t2); 
        p1.petHappy();
        p2.petHappy();
    }
}

 

转载于:https://www.cnblogs.com/jgjk/p/7219686.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值