day11--引用类型作为成员变量类型(hero,weapon,armor,skill)
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package com.inmind.test05;
|
||||
|
||||
//装备类
|
||||
public class Armor {
|
||||
//名称
|
||||
private String name;
|
||||
//防御值
|
||||
private int protectedNum;
|
||||
|
||||
public Armor(String name, int protectedNum) {
|
||||
this.name = name;
|
||||
this.protectedNum = protectedNum;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getProtectedNum() {
|
||||
return protectedNum;
|
||||
}
|
||||
|
||||
public void setProtectedNum(int protectedNum) {
|
||||
this.protectedNum = protectedNum;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.inmind.test05;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Hero {
|
||||
//英雄的名字
|
||||
private String name;
|
||||
//装备的武器
|
||||
private Weapon weapon;
|
||||
//装备的防御装
|
||||
private Armor armor;
|
||||
//装备的技能
|
||||
private Skill skill;
|
||||
|
||||
//武器背包
|
||||
private ArrayList<Weapon> weaponList = new ArrayList<>();
|
||||
//装备背包
|
||||
private ArrayList<Armor> armorList = new ArrayList<>();
|
||||
//技能背包
|
||||
private ArrayList<Skill> skillList = new ArrayList<>();
|
||||
|
||||
public Hero() {
|
||||
}
|
||||
|
||||
public Hero(String name, Weapon weapon, Armor armor) {
|
||||
this.name = name;
|
||||
this.weapon = weapon;
|
||||
this.armor = armor;
|
||||
}
|
||||
|
||||
//攻击
|
||||
public void attack(){
|
||||
System.out.println(this.name+"英雄,使用了"+this.weapon.getName()+"武器,输出了"+this.weapon.getHurt()+"伤害");
|
||||
}
|
||||
//防御
|
||||
public void protect(){
|
||||
System.out.println(this.name+"英雄,使用了"+this.armor.getName()+"装备,防御了"+this.armor.getProtectedNum()+"伤害");
|
||||
}
|
||||
|
||||
|
||||
//释放技能方法
|
||||
public void outSkill(){
|
||||
System.out.println(this.name+"英雄");
|
||||
this.skill.outSkill();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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;
|
||||
System.out.println(this.name+"英雄装备了"+weapon.getName()+"武器");
|
||||
}
|
||||
|
||||
public Armor getArmor() {
|
||||
return armor;
|
||||
}
|
||||
|
||||
public void setArmor(Armor armor) {
|
||||
this.armor = armor;
|
||||
System.out.println(this.name+"英雄装备了"+armor.getName()+"护具");
|
||||
}
|
||||
|
||||
public Skill getSkill() {
|
||||
return skill;
|
||||
}
|
||||
|
||||
public void setSkill(Skill skill) {
|
||||
this.skill = skill;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.inmind.test05;
|
||||
//技能接口
|
||||
public interface Skill {
|
||||
public abstract void outSkill();
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.inmind.test05;
|
||||
/*
|
||||
类作为成员变量类型(hero,weapon,armor)
|
||||
案例:英雄角色,获取武器,获取防御装,技能(接口)闯关
|
||||
*/
|
||||
public class Test08 {
|
||||
public static void main(String[] args) {
|
||||
//开始游戏
|
||||
//选择一个英雄
|
||||
Hero hero = new Hero();
|
||||
hero.setName("德玛西亚");
|
||||
|
||||
//打开一个宝箱,掉落一把武器
|
||||
Weapon weapon = new Weapon("黑切", 999);
|
||||
hero.setWeapon(weapon);
|
||||
//打败了一个小怪,掉落一个防具
|
||||
Armor armor = new Armor("复活甲", 200);
|
||||
hero.setArmor(armor);
|
||||
//遇到了boss,使用武器输出,使用护具,防御
|
||||
hero.attack();
|
||||
hero.protect();
|
||||
//打败了boss,掉落了神级技能
|
||||
//使用匿名内部类,创建出一个技能实现类对象
|
||||
Skill skill = new Skill() {
|
||||
@Override
|
||||
public void outSkill() {
|
||||
System.out.println("释放了神鬼降临,全屏攻击");
|
||||
}
|
||||
};
|
||||
hero.setSkill(skill);
|
||||
//英雄释放自己的技能
|
||||
hero.outSkill();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.inmind.test05;
|
||||
|
||||
//武器类
|
||||
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