1. 程式人生 > >繼承練習題,

繼承練習題,

重載 都是 override public 更改 generate power stub nds

主類:

public class Role {
// 共有屬性
/**
* 姓名屬性---由玩家輸入,可更改
*/
private String name;
private int level;
private int hp;
private int ac;
private int dc;
private int gold;

public Role() {

}

/**
* 角色類常用構造方法
* @param name 玩家姓名
* @param level 角色等級
* @param hp 角色體力
* @param ac
* @param dc
* @param gold
*/
public Role(String name, int level, int hp, int ac, int dc, int gold) {
super();
this.name = name;
this.level = level;
this.hp = hp;
this.ac = ac;
this.dc = dc;
this.gold = gold;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

//最終方法:表示該方法不能被子類重寫!
public final int getLevel() {
return level;
}

public void setLevel(int level) {
this.level = level;
}

public int getHp() {
return hp;
}

public void setHp(int hp) {
this.hp = hp;
}

public int getAc() {
return ac;
}

public void setAc(int ac) {
this.ac = ac;
}

public int getDc() {
return dc;
}

public void setDc(int dc) {
this.dc = dc;
}

public int getGold() {
return gold;
}

public void setGold(int gold) {
this.gold = gold;
}

// 共有行為
public void selfIndroduce() {

}

public void attack() {
System.out.println("玩家" + this.name + "執行攻擊動作。");
}

private void defend() {
System.out.println("玩家" + this.name + "受到攻擊,被動執行防禦動作。");
}
}

醫生類:

public class Doctor extends Role {
public final int HP_POWER = 8;
public final int AC_POWER = 1;
public final int DC_POWER = 2;

public Doctor(){

}

public Doctor(String name){
this.setName(name);
this.setLevel(1);
}

public Doctor(String name, int level, int gold){
this.setName(name);
this.setLevel(level);
this.setGold(gold);
}

@Override
public void setLevel(int level) {
// TODO Auto-generated method stub
super.setLevel(level);
this.setHp(level * this.HP_POWER);
this.setAc(level * this.AC_POWER);
this.setDc(level * this.DC_POWER);
}

@Override
public void selfIndroduce() {
// TODO Auto-generated method stub
System.out.println("老夫行醫數十年,現在是等級為" + this.getLevel() + "的聖手,道號" + this.getName());
}

public void cure(){
if(this.getLevel()*this.HP_POWER - this.getHp() > 2 * this.getLevel()){
this.setHp(this.getHp() + 2 * this.getLevel());
}else{
this.setHp(this.getLevel() * this.HP_POWER);
}
}
//方法重載
public void cure(Thief th){
int maxAddHp = 2 * this.getLevel();
if(th.getLevel()*th.HP_POWER - th.getHp() > maxAddHp){
if(th.getGold() >= maxAddHp){
th.setHp(th.getHp() + maxAddHp);
this.setGold(this.getGold() + maxAddHp);
th.setGold(th.getGold() - maxAddHp);
}else{
System.out.println("金幣不夠");
}
}else{
int addGold = th.getLevel()*th.HP_POWER - th.getHp();
if(th.getGold() >= addGold){
th.setHp(this.getLevel() * this.HP_POWER);
this.setGold(this.getGold() + addGold);
th.setGold(th.getGold() - addGold);
}else{
System.out.println("金幣不夠");
}
}
}




}

小偷類:

public class Thief extends Role{
public final int HP_POWER = 10;
public final int AC_POWER = 3;
public final int DC_POWER = 1;

public Thief(){

}

public Thief(String name){
this.setName(name);
this.setLevel(1);
}

public Thief(String name, int level, int gold){
this.setName(name);
this.setLevel(level);
this.setGold(gold);
}

@Override
public void setLevel(int level) {
// TODO Auto-generated method stub
super.setLevel(level);
this.setHp(level * this.HP_POWER);
this.setAc(level * this.AC_POWER);
this.setDc(level * this.DC_POWER);
}

@Override
public void selfIndroduce() {
// TODO Auto-generated method stub
System.out.println("我是一個等級為" + this.getLevel() + "的盜賊,這條街都是我罩的!");
}

@Override
public String toString() {
// TODO Auto-generated method stub
return "角色名:"+this.getName()+";職業:盜賊;等級:"+this.getLevel()+";攻擊力:"+this.getAc()+";防禦力:"+this.getDc()+";金幣:不給你看";
}

@Override
public boolean equals(Object arg0) {
// TODO Auto-generated method stub
Thief th = (Thief)arg0;
if(th.getName().equals(this.getName()) &&
th.getLevel() == this.getLevel()){
return true;
}
return false;
}

public void steal(){
System.out.println("我要下手了.....");
if(this.getHp() <= 9){
System.out.println("今天太累了,算了,休息一下");
}else{
int random = (int)(Math.random() * 10);
if(random <= this.getLevel()){
this.setGold(this.getGold() + 12);
this.setHp(this.getHp() - 9);
System.out.println("得手了,走,喝酒去.....");
}else{
System.out.println("媽的,失手了,狗日的,反偵察能力挺強。");
}
}
}

}

繼承練習題,