好吧,对不起,大家,我知道你们会告诉我,我需要不断地搜索,但我已经这么做了,而且我很确定我的假设是正确的,这会按照我想要的方式工作,但我想我会在这里询问并尝试就我的学习经历获得一些专业帮助,因为统一的答案不是很好。
无论如何,我正在尝试开始构建另一个 MMORPG,同时我也在学习 c Sharp。我有一个职业类(玩家的工作,如法师、骑士等),我想在创建玩家类的同时创建它,所以我需要使用一个 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)
{
}
}
现在看起来怎么样?
最佳答案
我建议重新设计实现
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; }
//DONE: let's ensure the property to be overriden
public abstract HitPointsPerLevel { 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, string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
Vid = vid;
Name = name;
}
}
//DONE: do not declare derived class as inner one
internal class Mage : Vocation
{
sealed public override float HitPointsPerLevel { get { return 12f; } }
//DONE: typo constructor should have been "Mage"
public Mage() : base(1, "Mage")
{
}
}
关于c# - 内部类构造函数...这有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38344869/