内部类构造...这有效吗?

时间:2016-07-13 07:09:31

标签: c# unity3d

好对不起伙计们,我知道你们会告诉我我需要搜索和搜索,但我已经并且我很确定我是正确的,我认为这将按照我想要的方式工作,但我想我我会问这里并尝试在我的学习经历上获得一些专业帮助,因为统一答案并不是那么好。

无论如何,我正在尝试开始构建另一个MMORPG,同时我也在学习c sharp。我有一个Vocation类(玩家的工作,像mage,knight,ect)我想在创建我的玩家类的同时创建,所以我需要使用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)
    {
    }
}

现在看看哪些人?

3 个答案:

答案 0 :(得分:3)

我建议重新设计实现

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")
    {
    }
}

答案 1 :(得分:0)

代码可以使用,但构造函数名称除外(Warrior而不是Mage)。

我确实想知道为什么Mage必须是Vocation的内部嵌套类。有什么用?

我可以理解你为什么会这样做internal:你不想让外部程序集创建一个实例,你可以通过工厂来实现。但是,没有理由使该类嵌套。基类可以独立存在。只需拉出嵌套类,在基类上使用protected成员就可以在基类和派生类之间进行通信。

小旁注:您可以在此使用this代替base

public Mage(int vid) : base(vid)
{
    this.Vid = 1;
    this.Name = "Mage";
}

答案 2 :(得分:0)

除了其他答案之外,很少有事情要评论,但它们都在代码审查的背景下。也许这在codereview.stackexchange.com中更好。

首先(这取决于风格) - 我尽量避免使用名为Vid的变量 - vocationId没有任何问题 - 最好是描述性的。

其次,与问题更相关,我不确定你的Mage /人,从语义上来说,一个职业 - 所以它不应该继承它。也许这是一个拥有职业但却不是职业的人 - 它可能不会扩展职业功能。这是继承的唯一目的。

你是否因为练习而想继承传承的概念?

另外,在C#6中,你有表达式身体属性,所以你可以稍微压缩你的代码:

public string Name => "None";

虽然这些看起来像常量。这基本上需要一个大的重新设计,从封装的OOP基础开始,并保持简单。

相关问题