首页 新闻 会员 周边

红色的地方提示报错 什么情况 哪位大神解释一下

0
[待解决问题]
  • // 定义一个角色(Role)类, 成员有 hp血条, atk攻击, def防御, weapon武器实例

    //定义一个武器(Weapon)类, 成员有 name atk

    //在Main()实例化一个角色, 实例化 n 个武器

    //在 Role 中写方法, 装备武器,更换武器,拆下武器

    //当需要用角色的攻击力的时候:攻击 = 自身攻击力 + 武器攻击力

    //封装Role的hp, hp不能小于0大于9999,

    //武器的名字在构造函数中赋值,只读不能写

  • class Program { static void Main(string[] args) { var role = new Role() { Atk = 1000, Hp = 1000 }; var weapon1 = new Weapon("") { }; var weapon2 = new Weapon("") { }; var weapon3 = new Weapon("") { }; role.AddWeapon(weapon1); role.ChangeWeapon(weapon2); role.DeleteWeapon(weapon2); Console.ReadKey();//停止 } } public class Weapon { public Weapon(string name) { this.Name = name; } public string Name { get; } public int Atk { get; set; } } public class Role { private int _hp; public int Hp
  • { get { return_hp; } set { if (value < 0 || value > 9999) throw new Exception("设置不在范围内"); _hp=value; } } public int Atk{ get; set; } public string Def { get; set; } public Weapon Weapon { get; set; } public int GetAtk() { int atk = (Weapon == null) ? 0 : Weapon.Atk; return Atk + atk; } public void AddWeapon(Weapon weapon) { this.Weapon = weapon; } public void ChangeWeapon(Weapon weapon) { this.Weapon = weapon; } public void DeleteWeapon(Weapon weapon) { this.Weapon = null; } } }
Cheers11的主页 Cheers11 | 菜鸟二级 | 园豆:215
提问于:2018-06-14 14:09
< >
分享
所有回答(3)
0

java啊,你要是用C++编写的话。。。

TearsOfDawn | 园豆:34 (初学一级) | 2018-06-14 20:48
0

是不是少了:,return后面应该有:的吧!

往愿 | 园豆:226 (菜鸟二级) | 2018-06-14 21:50
0

return_hp;
return _hp;
少空格

、熙和 | 园豆:1508 (小虾三级) | 2018-06-14 22:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册