【问题标题】:C#: How to override/replace a variables inheritanceC#:如何覆盖/替换变量继承
【发布时间】:2020-12-19 02:56:30
【问题描述】:

这是一个奇怪的问题,我知道您不能在 C# 中覆盖变量。也许这行不通。我正在尝试获取一个类型为类的变量,并用该类的子级覆盖它。

为了把它放在上下文中,我有一个Character 类。它有一个AttackSystem 类型的变量attackSystem。我有一个从Character 继承的NPC 类,我试图将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<T> where T: AttackSystem.
  • 你可以在一个方法里面做。现在 attachSystem 是在基类中定义的,所以你不能在继承的类中拥有另一个属性 attachsystem 除非你使用 override。
  • 你的意思是像public class Character<AttackSystem>?如果是这样,我可以添加多个像这样public class Character<AttackSystem, MotorSystem, InventorySystem> 的系统吗?我的大多数基类系统都会有这样的子类。 public class NPC<NPCAttackSystem, NPCMotorSystem, NPCInventory>: Character.
  • public virtual AttackSystem AttackSystem { get; set; }= new AttackSystem(); public override AttackSystem AttackSystem { get; set; }= new NPCAttackSystem(); 我不明白问题出在哪里

标签: c# inheritance overriding multiple-inheritance


【解决方案1】:

你可以这样做:

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())
    {

    }
}

【讨论】:

  • 这真的很干净。如果AttackSystem 同名,我如何在不访问类定义的情况下访问它?
【解决方案2】:

考虑如下方法。这种方法的主要好处是编译器知道npc.AttackSystemNPCAttackSystem 类型(或者至少是可以安全地转换为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());
    }
}

【讨论】:

    猜你喜欢
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 2011-01-10
    • 2016-01-25
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多