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();
}
}