主类:
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("妈的,失手了,狗日的,反侦察能力挺强。");
}
}
}
}