泛型和继承:将泛型与基类及其子类一起使用

蒂亚戈·瑞斯(Tiago Reis)

在过去的几天中,我一直在尝试开发UI项目拖放系统,并使其尽可能地灵活(它正在使用Unity 3D开发,尽管与我的问题并不完全相关)。在准确解释问题所在之前,这是以下代码:

public class ItemHandler<ItemType> : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IHoverHandler where ItemType : Item {

     public ItemType item { get; protected set; }

     ...
}

public abstract class SlotHandler<ItemType> : MonoBehaviour, IDropHandler where ItemType : Item {

    ...

    public ItemHandler<ItemType> itemHandler { get; protected set; }

    ...

    public void OnDrop(PointerEventData eventData) {

        // Retrieve the slots
        SlotHandler<Item> originalSlotHandler = ItemManager.draggedItemParent.GetComponent<SlotHandler<Item>>();
        SlotHandler<ItemType> destinationSlotHandler = this;

        // Retrieve the handlers from their items
        ItemHandler<Item> originalItemHandler = originalSlotHandler.itemObject != null ? originalSlotHandler.itemObject.GetComponent<ItemHandler<Item>>() : null;
        ItemHandler<ItemType> destinationItemHandler = destinationSlotHandler.itemObject != null ? destinationSlotHandler.itemObject.GetComponent<ItemHandler<ItemType>>() : null;

        // Add the original item to the destination slot
        bool canAddToDestination = originalSlotHandler is SlotHandler<ItemType> || originalSlotHandler is SlotHandler<Item>;
        bool canRemoveFromOrigin = originalSlotHandler is SlotHandler<ItemType> || destinationSlotHandler is SlotHandler<Item>;

        // Swap the items
        if(canAddToDestination == true && canRemoveFromOrigin == true) {

            // Remove the old items
            originalSlotHandler.RemoveItem();
            destinationSlotHandler.RemoveItem();

            // Add the new items
            originalSlotHandler.AddItem(destinationItemHandler);
            destinationSlotHandler.AddItem(originalItemHandler);

            ...
        }

        ...
    }

    public virtual void AddItem(ItemHandler<ItemType> itemHandler) {

        this.itemHandler = itemHandler;
    }
}

其背后的想法是让给定类型的ItemHandler(例如ItemHandler <Gem>,ItemHandler <Weapon>等),然后仅允许将它们放入相同类型或相同基本类型的SlotHandlers中。

  • ItemHandler <Gem>进入SlotHandler <Gem>和SlotHandler <Item>。
  • ItemHandler <武器>进入SlotHandler <武器>和SlotHandler <Item>。

我尝试以这种方式进行操作,因为我想限制我可以放置特定项目的插槽。

问题来自以下事实:我必须考虑特定类型的插槽SlotHandler <Item>(您在代码段中看到的Item类,它是所有Items的基类)。库存槽应能够容纳每种类型的物品。

现在,由于ItemHandler <Gem>不是ItemHandler <Item>的子类(或Item的任何其他派生类型),因此将不起作用。现在您可能会说为什么不只存储Item而不存储Generic ItemType?但我的想法是派生SlotHandler并在每个子类中具有特定的行为(将项目放入不同类型的插槽应做不同的事情)。

我想我理解为什么这样做无法按我尝试的方式工作,但找不到适合我的解决方案,因此我想尝试寻求帮助。抱歉,如果我对问题尚不十分清楚,请告诉我是否应该解决一些问题。

在此先感谢您为解决我的问题提供的任何帮助!

cjmarsh

在我看来,尽管您需要跟踪类型,但您真正需要的只是记录每个项目的父母(和他们的父母)的类型。与其在编程意义上不使用类型本身,还不如仅使用字符串标签并在基类中为该项目的有效类型的层次结构创建列表。这样,您可以拥有复杂的连接和组织,只需要将单个字符串与列表进行比较以查看其是否有效。

using UnityEngine;
using System.Collections.Generic;

public class Item {

  public List<string> validInvTypes = new List<string>();

  //...
}

public class Gem : Item {

  //...
}

public class Ruby : Gem {

  public Ruby()
  {
    validInvTypes.Add("Ruby");
    validInvTypes.Add("Gem");
    validInvTypes.Add("Item");
  }

  //...
}

public abstract class SlotHandler<ItemType> : MonoBehaviour, IDropHandler where ItemType : Item {

  //...

  public void OnDrop(PointerEventData eventData)
  {
    //...

    //string currentInvSlot
    //Item droppedItem

    if (droppedItem.validInvTypes.Contains(currentInvSlot))
      // Swap the items...
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将Comparable <T>与泛型类一起使用

来自分类Dev

如何将Swift协议与泛型方法和泛型类型一起使用

来自分类Dev

打字稿:将typeof与泛型一起使用

来自分类Dev

将联合类型与泛型一起使用

来自分类Dev

将Swift协议与泛型一起使用

来自分类Dev

在将泛型引用类型与泛型类型一起使用时收到警告

来自分类Dev

将泛型/参数化的Java与多个类一起使用

来自分类Dev

如何使用自定义逻辑将泛型类型与继承绑定在一起?

来自分类Dev

@property的objective-c“泛型”类型,可与多个子类一起使用

来自分类Dev

泛型和继承?

来自分类Dev

泛型类和子类的数组

来自分类Dev

多重继承和对另一个基类的指针的泛型访问

来自分类Dev

使用泛型方法将继承的泛型类添加到字典中

来自分类Dev

使用类泛型构造的子类型

来自分类Dev

具有变量JsonProperty的Jackson泛型(与泛型一起使用)

来自分类Dev

具有变量JsonProperty的Jackson泛型(与泛型一起使用)

来自分类Dev

Kotlin:无法与泛型一起使用的方法

来自分类Dev

与泛型一起使用的抽象常量

来自分类Dev

在Java中将通配符与嵌套泛型一起使用

来自分类Dev

将比较器与泛型一起使用

来自分类Dev

Kotlin:无法与泛型一起使用的方法

来自分类Dev

将单例与泛型和complementHandler一起使用时发生编译错误

来自分类Dev

在C#中将泛型类与依赖注入一起使用

来自分类Dev

泛型类的 Autowire 子类

来自分类Dev

泛型与类继承的混淆

来自分类Dev

当基类在C#中是泛型时,如何将子类转换为基类

来自分类Dev

如何将Injekt库中的injectLazy()委托与泛型一起使用?

来自分类Dev

如何将Java泛型与restTemplate一起使用

来自分类Dev

如何将Jackson与嵌套泛型一起使用?

Related 相关文章

  1. 1

    将Comparable <T>与泛型类一起使用

  2. 2

    如何将Swift协议与泛型方法和泛型类型一起使用

  3. 3

    打字稿:将typeof与泛型一起使用

  4. 4

    将联合类型与泛型一起使用

  5. 5

    将Swift协议与泛型一起使用

  6. 6

    在将泛型引用类型与泛型类型一起使用时收到警告

  7. 7

    将泛型/参数化的Java与多个类一起使用

  8. 8

    如何使用自定义逻辑将泛型类型与继承绑定在一起?

  9. 9

    @property的objective-c“泛型”类型,可与多个子类一起使用

  10. 10

    泛型和继承?

  11. 11

    泛型类和子类的数组

  12. 12

    多重继承和对另一个基类的指针的泛型访问

  13. 13

    使用泛型方法将继承的泛型类添加到字典中

  14. 14

    使用类泛型构造的子类型

  15. 15

    具有变量JsonProperty的Jackson泛型(与泛型一起使用)

  16. 16

    具有变量JsonProperty的Jackson泛型(与泛型一起使用)

  17. 17

    Kotlin:无法与泛型一起使用的方法

  18. 18

    与泛型一起使用的抽象常量

  19. 19

    在Java中将通配符与嵌套泛型一起使用

  20. 20

    将比较器与泛型一起使用

  21. 21

    Kotlin:无法与泛型一起使用的方法

  22. 22

    将单例与泛型和complementHandler一起使用时发生编译错误

  23. 23

    在C#中将泛型类与依赖注入一起使用

  24. 24

    泛型类的 Autowire 子类

  25. 25

    泛型与类继承的混淆

  26. 26

    当基类在C#中是泛型时,如何将子类转换为基类

  27. 27

    如何将Injekt库中的injectLazy()委托与泛型一起使用?

  28. 28

    如何将Java泛型与restTemplate一起使用

  29. 29

    如何将Jackson与嵌套泛型一起使用?

热门标签

归档