22222

public abstract class Toy {
    protected String name;

    public abstract void howToPlay();

    protected Toy(String name) {
        this.name = name;
    }
}

class Football extends Toy {
    private int radius;

    public Football(String name, int radius) {
        super(name);
        this.radius = radius;
    }

    @Override
    public void howToPlay() {
        System.out.println("足球名字=" + super.name);
        System.out.println("足球半径=" + radius);
        System.out.println("足球玩法=踢着玩");
    }
}

class TeddyBear extends Toy {
    private String furColor;

    public TeddyBear(String name, String furColor) {
        super(name);
        this.furColor = furColor;
    }

    @Override
    public void howToPlay() {
        System.out.println("泰迪熊名字=" + super.name);
        System.out.println("泰迪熊毛色=" + furColor);
        System.out.println("泰迪熊玩法=抱着玩");
    }
}

class Kite extends Toy {
    private String shape;

    public Kite(String name, String shape) {
        super(name);
        this.shape = shape;
    }

    @Override
    public void howToPlay() {
        System.out.println("风筝名字=" + super.name);
        System.out.println("风筝形状=" + shape);
        System.out.println("风筝玩法=放飞玩");
    }
}


public class Person {
    protected String name;

    protected Person(String name) {
        this.name = name;
    }
}


class Child extends Person {
    private Toy toy;

    public Child(String name, Toy toy) {
        super(name);
        this.toy = toy;
    }

    public void playMyToy() {
        System.out.println("孩子名字=" + name);
        toy.howToPlay();
    }
}

public class Test1 {
    public static void main(String[] args) {
        Toy toy1 = new Football("小足足",5);
        Child child1 = new Child("小张",toy1);

        Toy toy2 = new TeddyBear("小熊熊","棕色");
        Child child2 = new Child("小李",toy2);

        Toy toy3 = new Kite("小筝筝","燕子");
        Child child3 = new Child("小赵",toy3);

        child1.playMyToy();
        child2.playMyToy();
        child3.playMyToy();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值