//filename:Monster.java
public interface Monster {
public String getName();
public Integer getAttack();
public String doAttack();
}
//filename:RedPig.java
public class RedPig implements Monster {
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return this.color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getAttack() {
return this.attack;
}
public void setAttack(Integer attack) {
this.attack = attack;
}
public String doAttack() {
String str = getName() + "猪是"
+ getColor() +"颜色的,打掉了人"
+ getAttack() +"点血";
System.out.println(str);
return str;
}
public RedPig() {}
public RedPig(String name, String color,
Integer attack) {
this.name = name;
this.color = color;
this.attack = attack;
}
private String name;
private String color;
private Integer attack;
}
//filename:Snake.java
public class Snake implements Monster {
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getLegs() {
return this.color;
}
public void setLegs(Integer color) {
this.color = color;
}
public Integer getAttack() {
return this.attack;
}
public void setAttack(Integer attack) {
this.attack = attack;
}
public String doAttack() {
String str = getName() + "蛇用"
+ getLegs()
+"条脚跑到人面前去打人,打掉了人"
+ getAttack() +"点血";
System.out.println(str);
return str;
}
public Snake() {}
public Snake(String name, Integer legs,
Integer attack) {
this.name = name;
this.legs = legs;
this.attack = attack;
}
private String name;
private Integer legs;
private Integer attack;
}
//filename:Test.java
public class Test {
@Test // 用的junit 记得导包
public void test() {
String pname = "pigpig";
String pcolor = "red";
Integer pattack = 500;
String pdoattack = pname + "猪是"
+ pcolor +"颜色的,打掉了人"
+ pattack +"点血";
Monster m = new RedPig(name, pcolor, pattack);
String doStrPig = m.doAttack();
assertEquals(doStrPig, pdoattack);
String sname = "snasnake";
Integer slegs = 4;
Integer sattack = 50000;
String sdoattack = sname + "蛇用"
+ slegs
+"条脚跑到人面前去打人,打掉了人"
+ sdoattack +"点血";
m = new Snake(sname, slegs, sattack);
String doStrSnake = m.doAttack();
assertEquals(doStrSnake, sdoattack);
}
}
温馨提示:答案为网友推荐,仅供参考