好的,对不起,我知道你们会告诉我我需要进行搜索,但是我已经做到了,而且我敢肯定,我认为这可以按我希望的方式工作是正确的,但是我想我会在这里问由于统一答案不太好,因此请尝试在我的学习经历上寻求专业帮助。
无论如何,我正在尝试开始构建另一个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)
{
}
}
现在人们怎么看?
最佳答案
我建议重新设计实施
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")
{
}
}