day11-类作为成员变量类型(hero,weapon,armor)

This commit is contained in:
2026-05-30 14:26:35 +08:00
parent 3956c0173d
commit 21914a5025
5 changed files with 152 additions and 2 deletions

View File

@@ -0,0 +1,28 @@
package com.inmind.class_member_var09;
/*
类作为成员变量类型(hero,weapon,armor)
案例:英雄角色,获取武器,获取防御装,闯关
*/
public class Demo07 {
public static void main(String[] args) {
//创建英雄角色对象
Hero hero = new Hero();
hero.setName("德玛西亚");
//打怪,掉落武器
Weapon weapon = new Weapon("屠龙刀", 999);
hero.setWeapon(weapon);
//打怪,掉落防具
Armor armor = new Armor("复活甲", 888);
hero.setArmor(armor);
//打boss
hero.attack();
//boss回击
hero.protect();
System.out.println("游戏结束");
}
}