package choose;
public class Xiaoming {
private double money;
private Xiaoming xiaoming;
public Xiaoming() {
}
public Xiaoming(double money) {
this.money = money;
}
public void setMoney(double money) {
this.money = money;
}
public void buy() {
if(money > 30000) {
xiaoming = new Piano();
}else if(money > 8000) {
xiaoming = new Guitar();
}else if(money < 1000) {
xiaoming = new Harmonica();
}
}
public void play() {
xiaoming.play();
}
}
package choose;
public class Tests {
public static void main(String[] args) {
Xiaoming xiaoming=new Xiaoming();
xiaoming.setMoney(50000);
xiaoming.buy();
xiaoming.play();
}
}
package choose;
public class Piano extends Xiaoming{
public void play() {
System.out.println("钢琴在演奏。。。");
}
}
package choose;
public class Harmonica extends Xiaoming{
public void play() {
System.out.println("口琴在演奏。。。");
}
}
package choose;
public class Guitar extends Xiaoming {
public void play() {
System.out.println("吉他在演奏。。。。");
}
}