c#中合并类的一个好用例?

时间:2014-02-03 18:03:46

标签: c# enums

我有3类共轭表 - 一个用于名词形容词动词。正如answer对我之前的一个问题所建议的那样,为动词创建一个基类会非常有用,并使用Enum来实现所有实际结合类型,以便我可以区分类的实例。 Enum看起来像这样:

enum VerbConjugationType
{
    IndicativePresent,
    Participle,
    // etc
}

VerbConjugation类:

class VerbConjugation
{
    public VerbConjugationType conjugationType { get; set; }
    public string firstPersonSingular { get; set; }
    public string secondPersonSingular { get; set; }
    public string thirdPersonSingular { get; set; }
    public string firstPersonPlural { get; set; }
    public string secondPersonPlural { get; set; }
    public string thirdPersonPlural { get; set; }
    public string present { get; set; }
    public string past { get; set; }
}

然而,有一个小问题 - 动词变形的几个类型不具有相同的格式,也就是说IndicativePresent看起来像这样:

{
  public string firstPersonSingular { get; set; }
  public string secondPersonSingular { get; set; }
  public string thirdPersonSingular { get; set; }
  public string firstPersonPlural { get; set; }
  public string secondPersonPlural { get; set; }
  public string thirdPersonPlural { get; set; }
}

Participle看起来像这样:

{
  public string present { get; set; }
  public string past { get; set; }
}

有没有办法让单一类 VerbConjugation可以使用不同的数据进行实例化,具体取决于VerbConjugationType(会是Enum)吗?也就是说,如果我使用 IndicativePresent VerbConjugation实例化VerbConjugationType,我只希望数据(如上所示)成为该实例的成员的班级。同样适用于Participle;只有string presentstring past应该是该实例的成员。

修改:如果我可以通过以下方式访问数据成员,那么首选

VerbConjugation.Indicative.Present.fps(指示性礼物的第一人称单数) 和 VerbConjugation.Participle.present(现在分词)

是否有类/组织方法允许我这样做,即使每种共轭类型的数据成员存在差异?

另一个编辑:这是我目前拥有的代码,可能会提示我所需的结构 - 我所拥有的东西似乎不是正确的方法:

class VerbTable
    {
        public char group { get; set; }
        public char auxillary { get; set; }
        public string[] prepositions { get; set; }
        public bool transitive { get; set; }
        public bool pronominal { get; set; }

        /*
         * The subject of the verb determined by the markers:
         * 'fps' (first person singular)
         * 'sps' (second person singular)
         * 'tps' (third person singular)
         * 'fpp' (first person plural)
         * 'spp' (second person plural)
         * 'tpp' (third person plural)
         * 'present' (present tense)
         * 'past' (past tense)
         * and their accompanying conjugations.
         */

        struct IndicativePresent
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct IndicativeSimplePast
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct IndicativePresentPerfect
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct IndicativePastPerfect
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct IndicativeImperfect
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct IndicativePluperfect
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct IndicativeFuture
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct IndicativePastFuture
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }

        struct SubjunctivePresent
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct SubjunctivePast
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct SubjunctiveImperfect
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct SubjunctivePluperfect
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct ConditionalPresent
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct ConditionalFirstPast
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct ConditionalSecondPast
        {
            public string fps { get; set; }
            public string sps { get; set; }
            public string tps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
            public string tpp { get; set; }
        }
        struct ImperativePresent
        {
            public string sps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
        }
        struct ImperativePast
        {
            public string sps { get; set; }
            public string fpp { get; set; }
            public string spp { get; set; }
        }
        struct Infinitive
        {
            public string present { get; set; }
            public string past { get; set; }
        }
        struct Participle
        {
            public string present { get; set; }
            public string past { get; set; }
        }
    }

此类实例将成为Word类的成员,如此

class Word
{
    // ...
    // Can access a specific conjugation with:
    // Word.VerbTable.IndicatifPresent.fps;
    public VerbTable verbTable { get; set; }
}

4 个答案:

答案 0 :(得分:2)

不。不要这样做。为Verb创建一个抽象基类:

abstract class Verb
{
   // You can add in any properties that all Verbs share
}

然后创建一个IndicativePresent类:

class IndicativePresent : Verb
{
  public string firstPersonSingular { get; set; }
  public string secondPersonSingular { get; set; }
  public string thirdPersonSingular { get; set; }
  public string firstPersonPlural { get; set; }
  public string secondPersonPlural { get; set; }
  public string thirdPersonPlural { get; set; }
}

Participle班级:

class Participle : Verb
{
  public string present { get; set; }
  public string past { get; set; }
}

这两种类型都来自Verb,因此您可以告诉每个is一个动词,但是它们都会有不同的属性。您将不再需要VerbConjugationType类型,因为您只需查看对象的类型:

var ip = new IndicativePresent();
var par = new Participle();

(ip is Verb)              // True
(par is Verb)             // True
(ip is IndicativePresent) // True
(par is Participle)       // True

答案 1 :(得分:1)

我认为你想要的最终是Dictionary<whicheverenum, string>而不是一类数据元素。

让我们说我们有一个动词共轭,它有一个指示性时态:

var verb = new Dictionary<IndicativePresent, string>();
if(current == VerbConjugationType.IndicativePresent)
{
  verb[IndicativePresent.FirstPersonSingular] = "whatever";
  ...
}

除了枚举定义之外,这是您需要的所有代码。它开始听起来像你可能真的想要一个嵌套字典结构甚至是多维数组(或更多可能是锯齿状的)。你也许可以逃脱,因为枚举是不可或缺的类型。最终,结构将变得非常复杂,特别是如果你想要你最近指出的链接级别。

在最坏的情况下,你最终会得到像verb[ConjugationType.Indicative][Tense.Present][Target.FirstPerson][Number.Singular] = "something";这样的东西,此时设计类可能会再次开始变得像个好主意。数据越复杂,表示就越难,而且语法类型也比语法更复杂。

答案 2 :(得分:0)

最有效的方法是将VerbConjugation班级成员移至派生VerbConjugationWithPersons班级。 ParticipleImperative应该来自前者,而不是后者。

编辑:我当然是指“人”级成员。 :)

答案 3 :(得分:0)

这是不可能的,但你可以使用继承。

abstract class VerbConjugation {}
class StandardConjugation : VerbConjugation {
  public string firstPersonSingular { get; set; }
  public string secondPersonSingular { get; set; }
  public string thirdPersonSingular { get; set; }
  public string firstPersonPlural { get; set; }
  public string secondPersonPlural { get; set; }
  public string thirdPersonPlural { get; set; }
}
class IndicativePresent : StandardConjugation {}
class IndicativeSimplePast : StandardConjugation {}
class IndicativePresentPerfect : StandardConjugation {}
class Imperative : VerbConjugation 
{
    public string secondPersonSingular { get; set; }
    public string firstPersonPlural { get; set; }
    public string secondPersonPlural { get; set; }
}
class ImperativePast: Imperative {}
class ImperativePresent: Imperative {}
class Participle : VerbConjugation
{
  public string present { get; set; }
  public string past { get; set; }
}