day11-引用数据类型使用的案例1

This commit is contained in:
2026-01-26 10:23:34 +08:00
parent 03032d58ec
commit ad3e59f9ab
4 changed files with 128 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
package com.inmind.reference05;
//装备类
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;
}
}

View File

@@ -0,0 +1,7 @@
package com.inmind.reference05;
/*
类作为成员变量类型(hero,weapon,armor)
案例:英雄角色,获取武器,获取防御装,闯关,施放技能
*/
public class Demo07 {
}

View File

@@ -0,0 +1,62 @@
package com.inmind.reference05;
import java.util.ArrayList;
//英雄类
public class Hero {
private String name;//名字
private Weapon weapon;//武器属性 自定义引用数据类型作为了成员变量
private Armor armor;//防具属性
private ArrayList<Weapon> weapons = new ArrayList<>();//武器的背包
// private ArrayList<Armor> armors;//武器的背包
public Hero() {
}
//独有的功能
//攻击
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.getProtectNum()+"伤害");
};
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;
}
}

View File

@@ -0,0 +1,32 @@
package com.inmind.reference05;
//武器类
public class Weapon {
//名称
private String name;
//伤害值
private int hurt;
public Weapon() {
}
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;
}
}