Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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#_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Inheritance_Interface - Fatal编程技术网 elasticsearch,inheritance,interface,C#,elasticsearch,Inheritance,Interface" /> elasticsearch,inheritance,interface,C#,elasticsearch,Inheritance,Interface" />

C# 如何基于其他类声明定义类

C# 如何基于其他类声明定义类,c#,elasticsearch,inheritance,interface,C#,elasticsearch,Inheritance,Interface,我正在建立一个网上购物网站。我创建了一个名为Item的类,它包含我销售的所有产品的所有公共属性 // all common attribues class Item { int price; string title; string description; string category; } 现在,我有了更多具有附加属性的类,它们都包含项: 下一步是为我的网站实现搜索功能。我想使用Elasticsearch。我需要创建一个(并且只有一个)文档,其中包含需要搜

我正在建立一个网上购物网站。我创建了一个名为Item的类,它包含我销售的所有产品的所有公共属性

// 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;
}