与超类输入通用方法来访问子类属性(subclass properties accessed in g

2019-10-18 16:34发布

我想盘活/设备系统与我的物品系统的接口。 我的项目系统基于共享属性继承的类系统。

我存储在枚举类型,并给予项目的枚举值。

我的继承结构:

Item {
  Equippable {
    Armor {Chest, Head}
    Weapon {Melee, Ranged}
  }
}

我有各亚型定义它的父类的枚举变量,因此胸部就与类型的显式基构造函数调用定义armorTypes枚举。

public enum armorTypes {Chest, Helmet}

public class Armor : Equippable
{
    armorTypes armorType;

    public Armor(armorTypes at) : base(equippableTypes.Armor) 
    {
        armorType = at;
    }
}

public class Chest : Armor 
{
    int defense = 10;

    public Chest() : base(armorTypes.Chest) {}
}

public class Helmet : Armor 
{
    int defense = 5;

    public Helmet() : base(armorTypes.Helmet) {}
}

在我的库存I类有这些项目的空白版本。

Inventory{
  public Chest chestSlot;
  public Helmet helmetSlot;
  public Melee meleeSlot;
  public Ranged rangedSlot;
}

我想的对象基于其特性的两个插槽比较。

void Compare(Item item) 
{
  switch((item as Armor).armorType)
  {
      case armorTypes.Chest :
        Console.WriteLine((item as Chest).defense + " or " + chestSlot.defense);
        break;
      case armorTypes.Helmet :
        Console.WriteLine((item as Helmet).defense + " or " + helmetSlot.defense);
        break;
  }
}

然而,当我使用的关键字将其丢,不知怎么我失去了我的实例?

另外,因为我需要访问item.armorType我不能封装护甲,可以吗?

Answer 1:

这就是我想实现这一点。 首先,让基类中定义的基本比较方法:

abstract class Item 
{
    // these methods define base comparison operations;
    // by default, items are not comparable;
    public virtual bool CanCompareWith(Item item)
    {
        return false;
    }

    // since we don't know all possible item properties,
    // this method hasn't implementation
    public abstract void CompareWith(Item item);
}

接下来,我们必须定义一些默认设备。 盔甲:

abstract class Equippable : Item {}    

abstract class Armor : Equippable
{
    // every armor has a Defence property
    public abstract int Defence { get; }

    // the base comparison logic of two armors
    public override void CompareWith(Item item)
    {
        Console.WriteLine("{0} or {1}", ((Armor)item).Defence, this.Defence);
    }
}

class Chest : Armor
{
    // if you want for chests to be comared with chests only:
    public override bool CanCompareWith(Item item)
    {
        return item is Chest;
    }

    public override int Defence
    {
        get { return 10; }
    }
}

class Helmet : Armor
{
    public override bool CanCompareWith(Item item)
    {
        return item is Helmet;
    }

    public override int Defence
    {
        get { return 5; }
    }
}

武器:

abstract class Weapon : Equippable
{
    // every weapon has Attack property
    public abstract int Attack { get; }

    // the base comparison logc of two weapons
    public override void CompareWith(Item item)
    {
        Console.WriteLine("{0} or {1}", ((Weapon)item).Attack, this.Attack);
    }
}

class Melee : Weapon
{
    public override bool CanCompareWith(Item item)
    {
        return item is Melee;
    }

    public override int Attack
    {
        get { return 20; }
    }
}

class Ranged : Weapon
{
    public override bool CanCompareWith(Item item)
    {
        return item is Ranged;
    }

    public override int Attack
    {
        get { return 25; }
    }
}

请注意,这里没有任何枚举。 这是因为该类型本身(例如, ChestHelmet ,等等)定义,其中混凝土项目属于。 枚举只是多余这里。

这里的盘点:

class Inventory
{
    // this is inventory storage;
    // collection allows the storage to be extended later, or be displayed at once
    // (e.g., some ShowAllInventory method)
    private readonly List<Item> inventoryItems;

    public Inventory()
    {
        inventoryItems = new List<Item>
        {
            // some predefined inventory slots with default inventory items
            new Chest(),
            new Helmet(),
            new Melee(),
            new Ranged()
        };
    }

    // these properties are required, if you want to access predefined 
    // inventory slots in strongly-typed manner; they are NOT required for comparison
    public Chest ChestSlot
    {
        get { return (Chest)inventoryItems[0]; }
        set { inventoryItems[0] = value; }
    }

    public Helmet HelmetSlot
    {
        get { return (Helmet)inventoryItems[1]; }
        set { inventoryItems[1] = value; }
    }

    public Melee MeleeSlot
    {
        get { return (Melee)inventoryItems[2]; }
        set { inventoryItems[2] = value; }
    }

    public Ranged RangedSlot
    {
        get { return (Ranged)inventoryItems[3]; }
        set { inventoryItems[3] = value; }
    }

    // The comparison.
    public void Compare(Item newItem)
    {
        foreach (var item in inventoryItems)
        {
            if (item.CanCompareWith(newItem))
            {
                item.CompareWith(newItem);
            }
        }
    }
}

请注意,内部库存使用集合来存储它的项目。 这允许使用的物品(如比较,在实施执行批处理操作Compare法)。

现在,让我们定义了一些新类型的项目和测试我们的库存:

class SuperHelmet : Helmet
{
    public override int Defence
    {
        get { return 50; }
    }
}

class SuperMelee : Melee
{
    public override int Attack
    {
        get { return 200; }
    }
}

考试:

        var inventory = new Inventory();
        var superHelmet = new SuperHelmet();
        var superMeele = new SuperMelee();

        inventory.Compare(superHelmet);
        inventory.Compare(superMeele);


文章来源: subclass properties accessed in generic method with superclass input