内部类构造器...起作用吗? [英] Internal Class Constructer... does this work?
问题描述
对不起,伙计们,我知道你们会告诉我我需要进行搜索,但是我已经肯定了,我确信我认为这可以按我想要的方式工作是正确的,但是我想我会在这里提出问题,并尝试从我的学习经历中获得一些专业的帮助,因为统一答案并不是那么好.
Ok sorry guys, I know you guys are gonna tell me I need to search and search, but I have already and I am pretty sure I am correct in assuming this will work the way I want it to, but I figured I would ask here and try to get some professional help on my learning experience since unity answers isn't so great.
无论如何,我都试图开始构建另一个MMORPG,同时我也在学习c Sharp.我有一个职业类(玩家的职业,例如法师,骑士等),我想在创建玩家职业的同时创建它,因此我需要使用一个ID来确定哪个职业以及什么职业他们继承的属性值.
Anyways I am trying to start constructing another MMORPG and I am also learning c sharp at the same time. I have a class for Vocation (the job of the player, like mage, knight, ect) that I want to be created during the same time my player class is created, and so I need to use an id to decide which vocation and what property values they inherit.
这就是我所拥有的,在我努力做到这一点时会起作用吗?还是我做错了什么严重的事情??
This is what I have, will this work as I am trying to make it? Or am I doing something horribly wrong...?
编辑
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)
{
}
}
现在如何看待人?
HOW DOES IT LOOK NOW GUYS?
推荐答案
我建议重新设计实现
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")
{
}
}
这篇关于内部类构造器...起作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!