Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建变量以存储从同一类继承的类的对象_C#_Class_Inheritance - Fatal编程技术网

C# 创建变量以存储从同一类继承的类的对象

C# 创建变量以存储从同一类继承的类的对象,c#,class,inheritance,C#,Class,Inheritance,我试图创建一个库存,可以存储不同类型的项目 如果我只为每个磁贴的库存使用一个变量,并且我不尝试将每种类型的项目放入一个类中,那么我是否可以解决这个问题 以下是我开始执行的一些代码: 瓷砖等级: public class Tile { public Item inventory; } 项目类别: using UnityEngine; public class Item { public itemType type; } public enum itemType { c

我试图创建一个库存,可以存储不同类型的项目

如果我只为每个磁贴的库存使用一个变量,并且我不尝试将每种类型的项目放入一个类中,那么我是否可以解决这个问题

以下是我开始执行的一些代码:

瓷砖等级:

public class Tile
{
    public Item inventory;
}
项目类别:

using UnityEngine;

public class Item
{
    public itemType type;
}

public enum itemType
{
    crop,
    tool,
    cooker,
    dish
}
谢谢你的关注


编辑:这可以通过使用polymorphysm解决,将派生类强制转换为基类。

您可以创建一个所有项都实现的接口。这样,您就可以让许多类实现该接口,并将它们的实例设置为Tile类中的
type
变量

using UnityEngine;

public class Tile
{

    public List<itemType> allowedItems;

    public IItem inventory;

    public virtual void interact (IItem item, PlayerInteraction player)
    {
        foreach (itemType allowedItem in allowedItems)
        {
            if (allowedItem != item.type)
            {
                continue;
            }
            if (inventory == null && item != null)
            {
                player.setItem(inventory)
                inventory = item;

            }
        }
    }
}
项目接口

public interface IItem
{
    public itemType type {get; set;}
}
public class Item : IItem
{
    public itemType type {get; set;}
}
Item类实现接口

public interface IItem
{
    public itemType type {get; set;}
}
public class Item : IItem
{
    public itemType type {get; set;}
}
瓷砖类

using UnityEngine;

public class Tile
{

    public List<itemType> allowedItems;

    public IItem inventory;

    public virtual void interact (IItem item, PlayerInteraction player)
    {
        foreach (itemType allowedItem in allowedItems)
        {
            if (allowedItem != item.type)
            {
                continue;
            }
            if (inventory == null && item != null)
            {
                player.setItem(inventory)
                inventory = item;

            }
        }
    }
}
使用UnityEngine;
公共级瓷砖
{
公共物品清单;
公共物品清单;
公共虚拟虚空交互(IItem项,PlayerInteraction播放器)
{
foreach(allowedItems中的itemType allowedItem)
{
如果(allowedItem!=item.type)
{
继续;
}
如果(库存==null&&item!=null)
{
player.setItem(库存)
存货=物品;
}
}
}
}

您可以将基类或接口存储在
平铺中,您可以向基类/接口添加公共方法,也可以仅将其存储

以下代码基于


您是否有任何代码来演示您的问题?您的问题的答案是“是的,这是可能的”。如果您想要更具体的答案,请尝试提供具体的问题。代码示例可能会有所帮助。好的,我在问题的描述中提供了一些代码。你能分享相关代码吗。?看到代码比阅读问题描述更容易看到问题。@ChetanRanpariya我将代码编辑到问题中了。
{get;set;}在项目界面中做什么?它是一个属性。据我所知,接口不能有纯变量,它们必须是属性。您可以在这里阅读更多关于属性的内容:因此get和set方法是需要添加到接口中的成员?c#具有内置的getter和setter。在其他OOP语言中,您必须创建公共get和set方法,以使其他类可以访问私有变量,但在c#中,您使用get和set关键字。您可以在我上面发送给您的链接中看到示例和深入的解释。默认接口实现是C#8特性,现在您需要使用alpha版本