我想知道如果我可以使用重寫方法動態添加屬性設置屬性。我想添加屬性的值(分數),以便最終獲得總分。從屬性動態添加值
這裏我的代碼
public abstract class player
{
public string nickName { get; set; }
public virtual int computeScore()
{
throw new NotImplementedException();
}
}
public class Bes : player
{
public int initScore =0;
public int score { get; set; }
public int ttlScore { get; set; }
public override int computeScore()
{
return this.ttlScore += this.score;
}
}
你的代碼有什麼問題?任何異常或意外行爲? – Peter
每次我放置數據(bes1.score = 30)時,computeScore返回的值始終是最近輸入的值。我期待看到總分。 我試着輸入3個值。 bes1.score = 10; bes1.score = 20; bes1.score = 30; 總分是30; 我不知道我該怎麼做。 – KuysChan
你應該做的是添加到分數,而不是分配給它。像'bes1.score = bes1.score + 10;' –