c# - 内部类构造函数...这有效吗?

标签 c# unity-game-engine

好吧,对不起,大家,我知道你们会告诉我,我需要不断地搜索,但我已经这么做了,而且我很确定我的假设是正确的,这会按照我想要的方式工作,但我想我会在这里询问并尝试就我的学习经历获得一些专业帮助,因为统一的答案不是很好。

无论如何,我正在尝试开始构建另一个 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/

相关文章:

android - 在 Unity 中为 Android 构建 Google VR 项目,重新打包资源时失败

java - 通过 Unity/Java 插件启动 Android 应用程序

c# - 在 Microsoft 测试资源管理器中创建具有相同工作项 ID 的测试方法的播放列表

c# - 无效的 Json 原语

c# - Json.NET StringEnumConverter 并不总是有效

ios - 如何使用触摸拖动游戏对象(unity2d)

ios - 在后处理中启用 Unity3D xCode 项目的功能

c# - 使用反射获取元组的值

c# - DateTime.ToString 格式

iphone - Unity 3D 与 iOS 版 UDK 3