[英]Clarification Help Needed - C# / OOP / Inheritance / Polymorphism / Multiple Inheritance
Architecture of my self structured problem我的自结构化问题的架构
- Hero class (string Name, int Health, int Gold) -英雄 class (字符串名称,int Health,int Gold)
- Mage class (string Name, int Health, int Gold, int Intelect) - every Mage is a Hero -法师 class (字符串名称,智力,智力,智力)——每个法师都是英雄
- Archimage class (string Name, int Health, int Gold, int Intelect, int SpellsCast) - every Archimage is a Mage - Archimage class (string Name, int Health, int Gold, int Intelect, int SpellsCast) - 每个 Archimage 都是一个法师
- Warrior class ( string Name, int Health, int Gold, int Strength) - every Warrior is a Hero -战士 class (字符串名称、智力、智力、智力)——每个战士都是英雄
- Champion class (string Name, int Health, int Gold, int Intelect, int DemonsSlain) - every Champion is a Warrior - Champion class (字符串名称,int Health,int Gold,int Intelect,int DemonsSlain) - 每个冠军都是战士
Now the part that puzzles me: I want to implement hybrid classes that inherit from both branches of the class tree.现在让我感到困惑的部分是:我想实现继承自 class 树的两个分支的混合类。
- Paladin class (string Name, int Health, int Gold, int Intelect, int Strength) - every Paladin is a Mage and a Warrior -圣骑士 class (字符串名称、智力、智力、智力、智力)——每个圣骑士都是法师和战士
- Monk class (string Name, int Health, int Gold, int Intelect, int Strength, int SpellsCast, int DemonsSlain) - every Monk is an Archimage and a Champion. - Monk class (字符串名称,int Health,int Gold,int Intelect,int Strength,int SpellsCast,int DemonsSlain)-每个 Monk 都是 Archimage 和冠军。
And lets do two diagonal:让我们做两个对角线:
- BattleMage class (string Name, int Health, int Gold, int Intelect, int Strength, int SpellsCast) - every BattleMage is an Archimage and a Warrior. - BattleMage class (string Name, int Health, int Gold, int Intelect, int Strength, int SpellsCast) - 每个 BattleMage 都是 Archimage 和战士。
- SpellBreaker class (string Name, int Health, int Gold, int Intelect, int Strength, int DemonsSlain) - every SpellBreaker is an Mage and a Champion. - SpellBreaker class (字符串名称,int Health,int Gold,int Intelect,int Strength,int DemonsSlain)-每个 SpellBreaker 都是法师和冠军。
The code is self explanatory after running it.该代码在运行后是不言自明的。
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.");
}
}
}
So far so good, but this到目前为止一切顺利,但这
public class Paladin : Warrior, Mage
{
public Paladin(string name, int health, int gold, int Intelect, int Strength)
: base(name, health, gold, strength)
{
}
}
obviously will not work.显然行不通。
I am getting a sense I should be using interfaces.我觉得我应该使用接口。 I am not entirely sure how I can extract the needed parameters from multiple classes in a singular interface, reusing their constructors in this case also boggles my mind.我不完全确定如何从单个接口中的多个类中提取所需的参数,在这种情况下重用它们的构造函数也让我大吃一惊。
Any pointers, viable solutions, critiques or completely different approaches are welcomed!欢迎任何指示、可行的解决方案、批评或完全不同的方法!
Best regards!此致! ^^ ^^
As @DavidG mentioned in the comment above is not possible to have multiple inheritance
in C#
, but there are some things you can do depending of the scenario:正如上面评论中提到的@DavidG,在 C# 中不可能有multiple inheritance
C#
但是根据情况,您可以做一些事情:
interfaces
implementations or mixin pattern
.如果在查看完所有内容后您仍然需要多个 inheritance ,那么您可以使用一些模式,例如interfaces
实现或mixin pattern
。 You can think in mixin
like add functionalities to your object, lets say you can encapsulate a lot of functionalities for your different cases like run
, jump
, attack
, etc... ( Mixin ).您可以在mixin
中思考,例如向 object 添加功能,假设您可以为不同的情况封装很多功能,例如run
、 jump
、 attack
等......( Mixin )。
Other option to achieve this is simulate multiple inheritance with an interface
, you can think in an interface like a set of properties
and functions
, that your object implement and by implement
that implies that you need to provide a definition for those functions inside the class that implements
that interface
( Interfaces ).实现此目的的其他选项是使用interface
模拟多个 inheritance ,您可以将接口视为一组properties
和functions
,您的 object 实现并通过implement
这意味着您需要为 ZA2F2ED4FDC4EBC2CBBDZC21 中的那些函数提供定义implements
该interface
( 接口)。
There is a proposal to allow method implementations inside an interface for C#-8
but still is just a proposal, hope the above helps to clarify the issue and point you to the right direction.有一个提议允许在C#-8
的接口内实现方法,但仍然只是一个提议,希望以上内容有助于澄清问题并为您指明正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.