好吧,不好意思,我知道你們會告訴我我需要搜索和搜索,但是我已經確定我是正確的,並且認爲這樣會按照我想要的方式工作,但是我想我會在這裏問一下,並嘗試在我的學習經驗上得到一些專業幫助,因爲統一答案並不是那麼好。內部類Constructer ...做這個工作嗎?
反正我想開始構建另一個MMORPG,我也在同一時間學習c sharp。我有一個課程,我想在創建玩家類的同時創建一個Vocation類(玩家的職業,像法師,騎士等),所以我需要用一個ID來決定哪個職業和什麼職業它們繼承的屬性值。
這就是我的,這項工作,我正在努力?還是我在做可怕的錯事...?
編輯
using UnityEngine;
using System.Collections;
//DONE: abstract: a personage can't be "Vocation", but Mage, Warrior, Archer...
public abstract class Vocation
{
//DONE: just a readonly property
public int Vid {get; }
//DONE: just a readonly property
public string Name { get { return _Name; } }
protected string _Name = "None";
//DONE: let's ensure the property to be overriden
public abstract HitPointsPerLevel { get; }
public abstract ManaPointsPerLevel { get; }
//DONE: you don't want this constructor to be public, but protected only
//DONE: Assign all the data in one place
protected Vocation(int vid)
{
Vid = vid;
}
}
//DONE: do not declare derived class as inner one
internal class Mage : Vocation
{
sealed public override float HitPointsPerLevel { get { return 12f; } }
sealed public override string _Name = "Mage";
//DONE: typo constructor should have been "Mage"
public Mage() : base(1)
{
}
}
如何做IT現在看GUYS?
您沒有使用法師類。 – user3185569
'internal'表示一個類只能從相同的程序集(exe,dll)中訪問。沒有進一步的背景,我會說你現在不需要打擾。 「法師」也是一個內部階級,但我沒有看到它應該是的原因。我認爲'戰士'是一個複製粘貼的錯誤,你的意思是'法師'?最後,你爲什麼傳入'vid'並將其分配給只讀屬性?這是行不通的。 –
如果你嘗試過,你會看到'base.Vid = 1;'產生一個編譯器錯誤,因爲'Vid'沒有setter。即使'Vid = vid;'失敗,也不會做任何事情,因爲'Vid'總是返回'0'。其次,'base.Name =「法師」;'也不會出於同樣的原因。你能*做的是在'Vocation'中有一個'protected string _Name ='None「',然後將Name屬性變成'public string Name {get {return _Name}}',並在'Mage'中設置字段被稱爲「戰士」的構造函數?認真地說,讓IDE(Visual Studio)以這樣的錯誤幫助你... – Corak