【问题标题】:Clarification Help Needed - C# / OOP / Inheritance / Polymorphism / Multiple Inheritance需要澄清帮助 - C# / OOP / 继承 / 多态性 / 多重继承
【发布时间】:2021-03-29 03:31:03
【问题描述】:

我的自我结构化问题的架构

-英雄职业(字符串名称、int Health、int Gold)

-法师职业(字符串名称、int Health、int Gold、int Intelect)-每个法师都是英雄

-Archimage 类(字符串名称、int Health、int Gold、int Intelect、int SpellsCast)- 每个 Archimage 都是法师

-战士类(字符串名称,int Health,int Gold,int Strength)——每个战士都是英雄

-Champion class(字符串名称、int Health、int Gold、int Intelect、int DemonsSlain)- 每个冠军都是战士


现在让我感到困惑的部分是:我想实现继承自类树的两个分支的混合类。

-圣骑士职业(字符串名称、智力、智力、力量)——每个圣骑士都是法师和战士

-僧侣等级(字符串名称,智力,智力,力量,法术施法,恶魔杀手)——每个僧侣都是大形象和冠军。

让我们做两条对角线:

-BattleMage 类(字符串名称、int Health、int Gold、int Intelect、int Strength、int SpellsCast)- 每个 BattleMage 都是 Archimage 和 Warrior。

-SpellBreaker class(字符串名称、int Health、int Gold、int Intelect、int Strength、int DemonsSlain)- 每个 SpellBreaker 都是法师和冠军。


代码运行后一目了然。


namespace Heroes_Of_Inheritance_And_Polymorphism
{
    class Program
    {
        static void Main(string[] args)
        {
            Hero Hero1 = new Hero("Arthur", 100, 50);
            Hero1.ShowStatus();

            Console.WriteLine("##########");

            Mage Hero2 = new Mage("Arthur", 100, 50, 120);
            Hero2.ShowStatus();

            Console.WriteLine("##########");

            Warrior Hero3 = new Warrior("Arthur", 100, 50, 120);
            Hero3.ShowStatus();

            Console.WriteLine("##########");

            Archimage Hero4 = new Archimage("Arthur", 100, 50, 120, 1000);
            Hero4.ShowStatus();

            Console.WriteLine("##########");

            Champion Hero5 = new Champion("Arthur", 100, 50, 120, 1000);
            Hero5.ShowStatus();
        }
    }

    public class Hero
    {
        public string Name;
        public int Health;
        public int Gold;

        public Hero(string name, int health, int gold)
        {
            this.Name = name;
            this.Health = health;
            this.Gold = gold;
        }

        public virtual void ShowStatus()
        {
            Console.WriteLine($"I am a Hero called {Name}, with {Health} health and {Gold} gold pieces!");
        }
    }

    public class Mage : Hero
    {
        int Intelect; 

        public Mage(string name, int health, int gold, int intelect) : base(name, health, gold)
        {
            this.Intelect = intelect;
        }

        public override void ShowStatus()
        {
            base.ShowStatus();
            Console.WriteLine($"I, {Name}, have now become a Mage with {Intelect} intelect.");
        }
    }

    public class Archimage : Mage
    {
        int SpellsCast;

        public Archimage(string name, int health, int gold, int intelect, int spellscast) : base(name, health, gold, intelect)
        {
            this.SpellsCast = spellscast;
        }

        public override void ShowStatus()
        {
            base.ShowStatus();
            Console.WriteLine($"I, {Name}, am a wise Archimage that has cast {SpellsCast} spells.");
        }
    }

    public class Warrior : Hero
    {
        int Strength;

        public Warrior(string name, int health, int gold, int strength) : base(name, health, gold)
        {
            this.Strength = strength;
        }

        public override void ShowStatus()
        {
            base.ShowStatus();
            Console.WriteLine($"I, {Name}, have now become a Warrior with {Strength} strength.");
        }
    }

    public class Champion : Warrior
    {
        int DemonsSlain;

        public Champion(string name, int health, int gold, int strength, int demonsslain) : base(name, health, gold, strength)
        {
            this.DemonsSlain = demonsslain;
        }

        public override void ShowStatus()
        {
            base.ShowStatus();
            Console.WriteLine($"I, {Name}, am a strong Champion that has slain {DemonsSlain} demons.");
        }
    }
}

到目前为止一切都很好,但是这个

public class Paladin : Warrior, Mage
    {
        public Paladin(string name, int health, int gold, int Intelect, int Strength) 
            : base(name, health, gold, strength)
        {
        }
    }

显然行不通。

我觉得我应该使用接口。我不完全确定如何从单个接口中的多个类中提取所需的参数,在这种情况下重用它们的构造函数也让我大吃一惊。

欢迎任何指示、可行的解决方案、批评或完全不同的方法!

最好的问候! ^^

【问题讨论】:

  • hmm 但是你的类(战士等)不应该有不同的类......它应该是 1 个存储值的类......你应该为给定的类 fx 分配这个类的实例类ClassStats 枚举Classes 然后Dictionary<ClassesEnum, ClassStats> ... 然后Player 类将具有fx List<ClassesEnum> SelectedClasses 属性
  • 你不能在 C# 中进行多重继承,这是一件好事!您可能应该重新架构一下。例如,有一个Ability(或其他)对象的集合,您可以将它们提供给每个类。
  • 值得一读this

标签: c# oop inheritance polymorphism multiple-inheritance


【解决方案1】:

正如上面评论中提到的@DavidG,在C# 中不可能有multiple inheritance,但根据情况您可以做一些事情:

  • 首先查看所有架构,以确保您需要多重继承,有时只需稍作改动,我们就可以节省大量研究/实施时间。
  • 如果在查看完所有内容后仍需要多重继承,则可以使用一些模式,例如 interfaces implementations 或 mixin pattern

您可以在mixin 中思考,例如为您的对象添加功能,假设您可以为您的不同情况封装很多功能,例如runjumpattack 等... (@987654321 @)。

实现此目的的其他选项是使用interface 模拟多重继承,您可以在像propertiesfunctions 的集合这样的接口中思考,您的对象实现了implement,这意味着您需要为 implementsinterface(Interfaces) 的类中的那些函数提供定义。

有一个建议允许在C#-8 的接口内实现方法,但仍然只是一个建议,希望以上内容有助于澄清问题并为您指明正确的方向?。

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 2011-05-29
    • 2015-07-30
    • 2015-10-26
    相关资源
    最近更新 更多