这是一个游戏项的编程题,由于重复代码过多,所以要用super来进行赋值,并且在父类中进行数值的修改
package HomeWork4_15.Gameing;
import jdk.swing.interop.SwingInterOpUtils;
import javax.management.relation.RoleResult;
/**
* 角色
*/
public class Role {
/**
* 名字
*/
private String name;
/**
* 等级
*/
private int lv;
/**
* 体力
*/
private int power;
/**
* 最高体力
*/
private int powerMax;
/**
* 攻击力
*/
private int ass;
/**
* 防御力
*/
private int def;
/**
* 金币
*/
private int money;
public int getPowerMax() {
return powerMax;
}
public void setPowerMax(int powerMax) {
this.powerMax = powerMax;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLv() {
return lv;
}
public void setLv(int lv) {
this.lv = lv;
}
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
public int getAss() {
return ass;
}
public void setAss(int ass) {
this.ass = ass;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Role(String name, int lv, int addPower, int addAss, int addDef) {
this.name = name;
this.lv = lv;
this.power = this.lv * addPower;
this.ass = this.lv * addAss;
this.def = this.lv * addDef;
this.money = 100;
this.powerMax=this.power;
}
/**
* 攻击
*/
public void attack() {
System.out.println("攻击");
}
/**
* 防御
*/
private void defens() {
System.out.println("防御");
}
@Override
public String toString() {
return
"名字=" + name + '\'' +
", 等级=" + lv +
", 体力=" + power +
", 攻击力=" + ass +
", 防御力=" + def +
", 金币=" + money;
}
}
/**
* 盗贼
*/
class Robbers extends Role{
public Robbers(String name, int lv, int addPower, int addAss, int addDef) {
super(name, lv, addPower, addAss, addDef);
}
public void steal(){
System.out.println("我是盗贼,我可以偷盗");
if (this.getPower()<9){
System.out.println("我太累了,太难了");
return;
}
this.setMoney(this.getMoney()+12);
this.setPower(this.getPower()-9);
System.out.println("偷盗成功");
}
}
/**
* 猎人
*/
class hunter extends Role{
public hunter(String name, int lv, int addPower, int addAss, int addDef) {
super(name, lv, addPower, addAss, addDef);
}
public void hunting(){
if (this.getPower()<8){
System.out.println("体力不足");
return;
}
this.setMoney(this.getMoney()+10);
this.setPower(this.getPower()-8);
System.out.println("打猎成功");
}
}
/**
* 医生类
*/
class Docter extends Role{
public Docter(String name, int lv, int addPower, int addAss, int addDef) {
super(name, lv, addPower, addAss, addDef);
}
public void help(Role role){
int add=this.getLv()*2;
if ("医生".equals(role.getName())){
helping(add,role);
}else {
if (role.getMoney()<add){
System.out.println("钱么的了");
return;
}
role.setMoney(role.getMoney()-add);
helping(add,role);
}
}
public void helping(int add, Role role){
role.setPower(role.getPower()+add);
if (role.getPower()>role.getPowerMax()){
role.setPower(role.getPowerMax());
System.out.println("以为您治疗到最大生命值");
return;
}
return;
}
}