为了账号安全,请及时绑定邮箱和手机立即绑定

破坏统一性的构造函数

破坏统一性的构造函数

C#
杨__羊羊 2021-04-30 15:09:56
编码Army.Add(new PigSquad("The Old Guard", 10, 4));哪里Army是List<Unit>和Pigsquad被定义为public class PigSquad : Unit{    public override bool Dead { get; set; }    public override string Name { get; set;}    public override int Strength { get; set; }    public override int Age { get { return Age; } set { Age = value; Strength += 20 - Age; if (Age > 20) { Dead = true; } } }    public override int Veterancy { get; set; }    public PigSquad()    {    }    public PigSquad(string Name)    {        this.Name = Name;    }    public PigSquad(string Name, int Strength)    {        this.Name = Name;        this.Strength = Strength;    }    public PigSquad(string Name, int Strength, int Age)    {        this.Name = Name;        this.Strength = Strength;        this.Age = Age;    }}运行时使Unity编辑器崩溃。我不知道为什么会这样。在这方面的任何帮助将不胜感激。
查看完整描述

1 回答

?
九州编程

TA贡献1785条经验 获得超4个赞

public override int Age { get { return Age; } set { Age = value; Strength += 20 - Age; if (Age > 20) { Dead = true; } } }

应该

private int _Age; //maybe make this in your UNIT object as well and that way you do not need it in your child.
public override int Age { get { return _Age; } set { _Age = value; Strength += 20 - _Age; if (Age > 20) { Dead = true; } } }



查看完整回答
反对 回复 2021-05-08
  • 1 回答
  • 0 关注
  • 123 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信