[英]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. 在关系数据库中,有两种主要的实现方法,这就是最常见的数据库系统。
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. 每个类别的项目都需要一个表,但是如果您小心一点,则只需要几个表。
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.