C#: 如何 override/replace 变量继承
C#: How to override/replace a variables inheritance
这是一个奇怪的问题,我知道您不能在 C# 中覆盖变量。也许这行不通。我正在尝试获取一个类型为 class 的变量,并用 class.
的子项覆盖它
具体来说,我有一个 Character
class。它有一个类型为 AttackSystem
的变量 attackSystem
。我有一个继承自 Character
的 NPC
class,我试图将 attackSystem
覆盖为继承自 AttackSystem
的 NPCAttackSystem
类型。
这可行吗?或者,我是不是把事情复杂化了?我不应该“覆盖”变量,而只是在 NPC
的构造函数中说 attackSystem = new NPCAttackSystem()
(A) 我在做什么(不起作用):
public class Character
{
public AttackSystem attackSystem = new AttackSystem();
}
public class NPC : Character
{
public NPCAttackSystem attackSystem = new NPCAttackSystem();;
}
public class AttackSystem {}
public class NPCAttackSystem: AttackSystem {}
(B)我该怎么办?
public class Character
{
public AttackSystem attackSystem = new AttackSystem();;
}
public class NPC : Character
{
NPC()
{
attackSystem = new NPCAttackSystem();
}
}
public class AttackSystem {}
public class NPCAttackSystem: AttackSystem {}
我经常在我自己的问题中回答我自己的问题。只是想知道我是否可以按照我想要的方式来做(A)或者我是否应该以其他方式来做(B)。另一种方式 (B) 行得通吗?这样我可以访问 NPCAttackSystem
的成员吗?
抱歉所有问题,简单的 A.) 或 B.) 就可以了。
感谢您的帮助,我喜欢在这里提问。
你可以这样做:
public class Character
{
public Character() : this(new AttackSystem())
{
}
protected Character(AttackSystem attackSystem)
{
AttackSystem = attackSystem;
}
public AttackSystem AttackSystem { get; }
}
public class NpcCharacter : Character
{
public NpcCharacter() : base(new NpcAttackSystem())
{
}
}
考虑如下方法。这种方法的主要好处是编译器知道 npc.AttackSystem
是 NPCAttackSystem
类型(或者至少是可以安全地转换为 NPCAttackSystem 的类型)。
using System;
public class Program
{
public abstract class Character<T> where T: AttackSystem, new()
{
public T AttackSystem { get; } = new T();
}
public class PC: Character<AttackSystem>
{
}
public class NPC : Character<NPCAttackSystem>
{
}
public class AttackSystem {}
public class NPCAttackSystem: AttackSystem {}
public static void Main()
{
var normal = new PC();
var npc = new NPC();
Console.WriteLine(normal.AttackSystem.GetType());
Console.WriteLine(npc.AttackSystem.GetType());
}
}
这是一个奇怪的问题,我知道您不能在 C# 中覆盖变量。也许这行不通。我正在尝试获取一个类型为 class 的变量,并用 class.
的子项覆盖它具体来说,我有一个 Character
class。它有一个类型为 AttackSystem
的变量 attackSystem
。我有一个继承自 Character
的 NPC
class,我试图将 attackSystem
覆盖为继承自 AttackSystem
的 NPCAttackSystem
类型。
这可行吗?或者,我是不是把事情复杂化了?我不应该“覆盖”变量,而只是在 NPC
的构造函数中说 attackSystem = new NPCAttackSystem()
(A) 我在做什么(不起作用):
public class Character
{
public AttackSystem attackSystem = new AttackSystem();
}
public class NPC : Character
{
public NPCAttackSystem attackSystem = new NPCAttackSystem();;
}
public class AttackSystem {}
public class NPCAttackSystem: AttackSystem {}
(B)我该怎么办?
public class Character
{
public AttackSystem attackSystem = new AttackSystem();;
}
public class NPC : Character
{
NPC()
{
attackSystem = new NPCAttackSystem();
}
}
public class AttackSystem {}
public class NPCAttackSystem: AttackSystem {}
我经常在我自己的问题中回答我自己的问题。只是想知道我是否可以按照我想要的方式来做(A)或者我是否应该以其他方式来做(B)。另一种方式 (B) 行得通吗?这样我可以访问 NPCAttackSystem
的成员吗?
抱歉所有问题,简单的 A.) 或 B.) 就可以了。
感谢您的帮助,我喜欢在这里提问。
你可以这样做:
public class Character
{
public Character() : this(new AttackSystem())
{
}
protected Character(AttackSystem attackSystem)
{
AttackSystem = attackSystem;
}
public AttackSystem AttackSystem { get; }
}
public class NpcCharacter : Character
{
public NpcCharacter() : base(new NpcAttackSystem())
{
}
}
考虑如下方法。这种方法的主要好处是编译器知道 npc.AttackSystem
是 NPCAttackSystem
类型(或者至少是可以安全地转换为 NPCAttackSystem 的类型)。
using System;
public class Program
{
public abstract class Character<T> where T: AttackSystem, new()
{
public T AttackSystem { get; } = new T();
}
public class PC: Character<AttackSystem>
{
}
public class NPC : Character<NPCAttackSystem>
{
}
public class AttackSystem {}
public class NPCAttackSystem: AttackSystem {}
public static void Main()
{
var normal = new PC();
var npc = new NPC();
Console.WriteLine(normal.AttackSystem.GetType());
Console.WriteLine(npc.AttackSystem.GetType());
}
}