简体   繁体   English

我如何制作某个类的列表,但仍允许使用其所有参数C#在列表中允许该类的扩展

[英]How can I make a list of a certain class, but still allow extensions of the class to be allowed in the list with all of their parameters C#

So I am writing an item system for unity C#, and I have this base item class that I extend upon for different types of items, for example I would extend the base class and add a variable for attack and attack speed, and a function for attacking for a weapon class. 因此,我正在编写一个用于统一C#的项目系统,并且具有针对不同类型的项目扩展的该基础项目类,例如,我将扩展该基础类并添加一个用于攻击和攻击速度的变量,以及一个用于进攻武器类。

But my issue is I am writing a database for the items, and I want to store these different classes into one big list, but when I make a list of Base Items, and I pass a weapon item, it cleaves away the attack function and the attack variables, and basically turns the weapon item into a base item, rendering it useless. 但是我的问题是我正在为物品编写数据库,我想将这些不同的类存储到一个大列表中,但是当我列出基本物品列表并传递武器物品时,它会切断攻击功能,攻击变量,并且基本上将武器项目变成了基础项目,使其无用。

如果您喜欢RDBMS,而不是像SledgeHammer建议的那样使用NOSQL,那么您可能会在这里找到答案。

There are two main ways of doing this in a relational database, which is what most common database systems are. 在关系数据库中,有两种主要的实现方法,这就是最常见的数据库系统。

  1. You can have different tables for each type of Item, so Weapon s have one table, Potion s have another, and the Item table is a list of (Name, Type, Id) tuples, telling you which item in which table to look at to get its exact details. 对于每种类型的项,您可以有不同的表,因此, Weapon有一个表, Potion有另一个表, Item表是(Name, Type, Id)元组的列表,告诉您要在哪个表中查看哪个项以获得其确切的细节。 This will require one table for each class of item, but if you are careful, you only need a few. 每个类别的项目需要一个表,但是如果您小心一点,则只需要几个表。

  2. Have an Item table, which stores an Item Type (again). 有一个Item表,该表再次存储Item Type。 You also have an "Attribute" table, which has columns ItemId, Attribute, Value So if you have a sword with speed 5 and damage 10, and a potion that heals 3, you would have the following rows 您还有一个“属性”表,该表具有列ItemId, Attribute, Value因此,如果您的剑速度为5,伤害为10,并且药水可以恢复3,则您将获得以下行

     Item ==== Id | Name | Category 1 | "Sword" | "weapon" 2 | "Healing Potion" | "potion" Attribute ========= ItemId | Attribute | Value 1 | "speed" | 5 1 | "damage" | 10 2 | "healing" | 3 

It's then up to you to join on ItemId, and pivot the rows into columns and convert into your item's class. 然后由您决定是否要join ItemId,然后将行pivot到列中并转换为项目的类。

Or you can use a document-based database, as the other answers suggest. 或者您可以使用基于文档的数据库,如其他答案所建议的那样。

Sounds like you want to use a document store instead of a RDBS. 听起来好像您想使用文档存储而不是RDBS。 RDBS requires all items in a table to be of the same type. RDBS要求表中的所有项目均为同一类型。 The drawback is you end up with a bunch of NULL columns for the types that don't contain all the columns which makes everything hokey. 缺点是您最终得到一堆NULL列,这些类型的NULL并不包含所有使所有事情变得曲折的列。 A document store is schema-less, so you only have the attributes that pertain to the type. 文档存储区没有架构,因此您仅具有与类型相关的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从单声道的c ++中获取某个类中所有方法的列表? - How can I get a list of all methods in a certain class from c++ in mono? C#:如何实例化私有内部类的列表? - C#: How can I instantiate a List of private inner class? 如何上课并允许方法接受通用列表 - How can I take a class and allow the methods to accept a generic list C# 中的通用函数,它采用随机类列表和某些属性名称的数组作为参数 - A generic function in C# that take a List of random class and an array of certain property names as parameters 我如何转换列表 <Interface> 列表 <Class> 在c#中 - How do i convert a List<Interface> to List<Class> in c# 如何在另一个 class 方法中使用 class 和构造函数并将它们存储到我的列表中? C# - How can I use a class and constructor in a another class, method and store them to my list? C# 我如何存储我的清单 <List<Class> &gt;在WPF中使用C#转换为文本文件? - How can i store my List<List<Class>> to a text file using C# in WPF? 如何从 c# 中的另一个 class 访问私人列表,以便我可以比较两个列表字段? - How to access private list from another class in c# so that I can compare two list fields? 如何列出实现抽象类和接口(C#)的东西? - How to make a list of something that implements abstract class and interface (C#)? 如何在列表 c# 中收集 class 的所有创建对象 - how to gather all created objects of a class in a list c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM