西南科技大学计算机学院 《面向对象系统分析和设计》实验报告 public void defense() {
System.out.println(\外籍中锋\ + name + \防守\); }
public String getName() {
return name; }
public void setName(String name) {
this.name = name; }
}
public class Forwards extends Player {
public Forwards(String name) {
super(name); }
public void attack() {
System.out.println(\前锋\ + name + \进攻\); }
public void defense() {
System.out.println(\前锋\ + name + \防守\); }
}
public class Translator extends Player {
private ForeignCenter wjzf = new ForeignCenter(); public Translator(String name) {
super(name);
wjzf.setName(name); }
public void attack() {
wjzf.attack(); }
public void defense() {
17
西南科技大学计算机学院 《面向对象系统分析和设计》实验报告 wjzf.defense(); }
}
public class Client {
public static void main(String[] args) {
Player b = new Forwards(\巴蒂尔\); b.attack();
Player m = new Guards(\麦克格雷迪\); m.attack();
Player ym = new Translator(\姚明\); ym.attack(); ym.defense(); }
}
3) 实现结果:
18