day11-类作为成员变量类型(hero,weapon,armor)
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.inmind.class_interface04;
|
||||
//防御装类
|
||||
public class Armor {
|
||||
private String name;//防御装的名字
|
||||
private int protectNum;//防御力
|
||||
|
||||
public Armor(String name, int protectNum) {
|
||||
this.name = name;
|
||||
this.protectNum = protectNum;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getProtectNum() {
|
||||
return protectNum;
|
||||
}
|
||||
|
||||
public void setProtectNum(int protectNum) {
|
||||
this.protectNum = protectNum;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.inmind.class_interface04;
|
||||
/*
|
||||
类作为成员变量类型(hero,weapon,armor)
|
||||
案例:英雄角色,获取武器,获取防御装,闯关
|
||||
*/
|
||||
public class Demo05 {
|
||||
public static void main(String[] args) {
|
||||
//开始游戏
|
||||
//创建英雄角色
|
||||
Hero hero = new Hero("德玛西亚");
|
||||
//打怪,掉落装备,掉出一把极品屠龙刀
|
||||
Weapon weapon = new Weapon("屠龙刀", 999);
|
||||
//捡起武器,使用武器
|
||||
hero.setWeapon(weapon);
|
||||
//打怪,掉落装备,掉出了复活甲
|
||||
Armor armor = new Armor("复活甲", 1000);
|
||||
//捡起装备,使用装备
|
||||
hero.setArmor(armor);
|
||||
//如果想用背包将多余的武器保存起来
|
||||
Weapon weapon1 = new Weapon("倚天剑1", 100);
|
||||
Weapon weapon2 = new Weapon("倚天剑2", 100);
|
||||
Weapon weapon3 = new Weapon("倚天剑3", 100);
|
||||
hero.getWeapons().add(weapon1);
|
||||
hero.getWeapons().add(weapon2);
|
||||
hero.getWeapons().add(weapon3);
|
||||
//打boss
|
||||
hero.attack();
|
||||
hero.defend();
|
||||
System.out.println("打怪结束");
|
||||
hero.showWeapons();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.inmind.class_interface04;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
//英雄角色,获取武器,获取防御装
|
||||
public class Hero {
|
||||
private String name;
|
||||
private Weapon weapon;//武器
|
||||
private Armor armor;//防御装
|
||||
private ArrayList<Weapon> weapons = new ArrayList<>();//武器背包
|
||||
//技能
|
||||
|
||||
|
||||
public Hero() {
|
||||
}
|
||||
|
||||
public Hero(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Hero(String name, Weapon weapon, Armor armor) {
|
||||
this.name = name;
|
||||
this.weapon = weapon;
|
||||
this.armor = armor;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Weapon getWeapon() {
|
||||
return weapon;
|
||||
}
|
||||
|
||||
public void setWeapon(Weapon weapon) {
|
||||
this.weapon = weapon;
|
||||
}
|
||||
|
||||
public Armor getArmor() {
|
||||
return armor;
|
||||
}
|
||||
|
||||
public void setArmor(Armor armor) {
|
||||
this.armor = armor;
|
||||
}
|
||||
|
||||
public ArrayList<Weapon> getWeapons() {
|
||||
return weapons;
|
||||
}
|
||||
|
||||
public void setWeapons(ArrayList<Weapon> weapons) {
|
||||
this.weapons = weapons;
|
||||
}
|
||||
|
||||
//攻击和防御行为
|
||||
public void attack(){
|
||||
//哪个英雄,使用什么武器,输出了多少伤害
|
||||
System.out.println(this.name + "使用" + this.weapon.getName() + "攻击,输出了" + this.weapon.getHurt() + "伤害");
|
||||
}
|
||||
|
||||
public void defend(){
|
||||
//哪个英雄,使用什么防御装,可以抵消多少伤害
|
||||
System.out.println(this.name + "使用" + this.armor.getName() + "防御,抵消了" + this.armor.getProtectNum() + "伤害");
|
||||
}
|
||||
|
||||
//展示武器背包
|
||||
public void showWeapons(){
|
||||
System.out.println(this.name + "的武器背包有:");
|
||||
for (int i = 0; i < weapons.size(); i++) {
|
||||
Weapon w = weapons.get(i);
|
||||
System.out.println(w.getName()+", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.inmind.class_interface04;
|
||||
|
||||
//武器类
|
||||
public class Weapon {
|
||||
private String name;//武器的名称
|
||||
private int hurt;//伤害值
|
||||
|
||||
//满参构造
|
||||
public Weapon(String name, int hurt) {
|
||||
this.name = name;
|
||||
this.hurt = hurt;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getHurt() {
|
||||
return hurt;
|
||||
}
|
||||
|
||||
public void setHurt(int hurt) {
|
||||
this.hurt = hurt;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user