1.用面向对象的思维来模拟LOL里的盖仑上阵杀敌
public class Homework5_1 {
String name;
String skill_q;
String skill_w;
String skill_e;
String skill_r;
public Homework5_1(String name) {
this.name = name;
}
public Homework5_1(String name, String skill_q, String skill_w, String skill_e, String skill_r) {
this(name);
this.skill_q = skill_q;
this.skill_w = skill_w;
this.skill_e = skill_e;
this.skill_r = skill_r;
}
public void fight() {
System.out.println(this.name + "在上阵杀敌");
}
public static void main(String[] args) {
Homework5_1 h = new Homework5_1("盖仑", "小宝剑", "防御", "电风扇", "大宝剑");
h.fight();
}
}
2.用面向对象的思维来完成植物大战僵尸
Homework5_2_ZhiWu:
public class Homework5_2_ZhiWu {
String name;
int hp;
int attack;
public Homework5_2_ZhiWu(String name, int hp, int attack) {
this.name = name;
this.hp = hp;
this.attack = attack;
}
public void fight(Homework5_2_JiangShi js) {
System.out.println(this.name + "在干" + js.name);
js.hp -= this.attack;
System.out.println("僵尸的血量剩余:" + js.hp);
}
}
Homework5_2_JiangShi
public class Homework5_2_JiangShi {
String name;
int hp;
int attack;
public Homework5_2_JiangShi(String name, int hp, int attack) {
this.name = name;
this.hp = hp;
this.attack = attack;
}
public void eat(Homework5_2_ZhiWu zw) {
System.out.println(this.name + "正在吃" + zw.name + "植物");
zw.hp -= this.attack;
System.out.println("植物还有血量:" + zw.hp);
}
}
Homework5_2_Client
public class Homework5_2_Client {
public static void main(String[] args) {
Homework5_2_ZhiWu zw = new Homework5_2_ZhiWu("豌豆", 1000, 5);
Homework5_2_JiangShi js = new Homework5_2_JiangShi("铁通僵尸", 800, 20);
zw.fight(js);
js.eat(zw);
}
}