C# 如何基于其他类声明定义类
我正在建立一个网上购物网站。我创建了一个名为Item的类,它包含我销售的所有产品的所有公共属性C# 如何基于其他类声明定义类,c#,
elasticsearch,inheritance,interface,C#,
elasticsearch,Inheritance,Interface,我正在建立一个网上购物网站。我创建了一个名为Item的类,它包含我销售的所有产品的所有公共属性 // all common attribues class Item { int price; string title; string description; string category; } 现在,我有了更多具有附加属性的类,它们都包含项: 下一步是为我的网站实现搜索功能。我想使用Elasticsearch。我需要创建一个(并且只有一个)文档,其中包含需要搜
// all common attribues
class Item {
int price;
string title;
string description;
string category;
}
现在,我有了更多具有附加属性的类,它们都包含项:
下一步是为我的网站实现搜索功能。我想使用Elasticsearch。我需要创建一个(并且只有一个)文档,其中包含需要搜索的所有字段:
class ElasticSearchDocument
{
// these are coming from Item
int price;
string title;
string category;
// there are coming from Shirt
string size;
string color;
// these are coming from Book
string author;
}
请注意,ElasticSearchDocument将包含需要搜索的每个属性。我需要ElasticSearchDocument类将数据发送到Elasticsearch
是否有任何方法可以使用我的其他类来构建ElasticSearchDocument?i、 e.是否可能从其他类派生ElasticSearchDocument,因此如果我更改它们,我不需要在两个位置重复更改
或者有更好的方法来实现这一点吗?为什么类不派生
项而不是包含它?像类书:Item{…}
?谢谢。。。没有特别的原因。。。我不完全确定哪种方法更好。。。我选择了这种方法,因为我认为从多个类继承是不可能的。。。在我的实际设计中,我有最终分类中需要的项目、地址等类。这是“是a”和“有a”之间的区别。一条裙子有一个项目?不是。裙子“是”项目,所以它应该继承该项目。假设你有一个狗类和一个项圈类。狗“有一个”项圈,所以项圈类型的属性应该是dog类的一部分。谢谢,很好地解释了我最终实现的
class ElasticSearchDocument
{
// these are coming from Item
int price;
string title;
string category;
// there are coming from Shirt
string size;
string color;
// these are coming from Book
string author;
}