简体   繁体   English

从具有相同名称的属性的接口继承

[英]Inheriting from an interface with properties of the same name

I have an interface IProduct and two partial classes SearchedProductInternal and SearchedProductExternal. 我有一个接口IProduct和两个局部类SearchedProductInternal和SearchedProductExternal。 These two classes are extending classes coming from 3rd party webservice searches but both return slightly different result types. 这两个类是来自第三方网络服务搜索的扩展类,但是两者都返回略有不同的结果类型。 I want to use the interface for both so they type returned is the same. 我想为两者使用接口,因此它们返回的类型相同。 I know how to inherit but what do I do to return the "Name" as both the interface and SearchedProductInternal have the same object name? 我知道如何继承,但由于接口和SearchedProductInternal具有相同的对象名称,该怎么做才能返回“名称”?

My Interface is similar to as follows: 我的界面类似于以下内容:

   public interface IProduct
        {  
            string Name { get; }
            string ID { get; }
            string DescriptionShort { get; }
            string DescriptionLong { get; }
         } 

My Object SearchedProductInternal has the following properties: 我的对象SearchedProductInternal具有以下属性:

     string Name; 
     int ObjectIdField;
     string DescriptionShortField;
     string DescriptionLongField;

So my this is where I am inheriting 所以我这就是我继承的地方

  public partial class SearchedProductInternal : IProduct
        {

            public string ID
            {
                get { return ObjectIdField.ToString(); }
            }
            public string Name
            {
                //What do I do here?
            }
            public string DescriptionShort{get { return shortDescriptionField; }
            }

            public string DescriptionLong {get { return longDescriptionField; }
            }
}

I want to the return the name that has been originality assigned in the SearchedProductInternal class but I don't know how to do that because if I just put 我想返回在SearchedProductInternal类中分配的具有原创性的名称,但我不知道该怎么做,因为如果我把

return Name

I get a stackoverflow error as it appears to be just keeping calling its self? 我收到一个stackoverflow错误,因为它似乎只是不断调用其自身?

我认为您在这里应该做的是显式实现该接口,以便同时拥有该类中定义的Name属性和该接口的IProduct.Name属性。

You can explicitly implement the interface, like so: 您可以显式实现该接口,如下所示:

public partial class SearchedProductInternal : IProduct
{
    string IProduct.ID
    {
        get { return ObjectIdField.ToString(); }
    }

    string IProduct.Name
    {
        get { return "Interface name"; }
    }

    string IProduct.DescriptionShort
    {
        get { return shortDescriptionField; }
    }

    string IProduct.DescriptionLong 
    {
        get { return longDescriptionField; }
    }

    // Name property for the class, not the interface
    public string Name
    {
        get { return "Class name";  }
    }
}

This way you can differentiate between calls to your interface properties and properties with the same name on your class. 这样,您就可以区分对接口属性的调用和在类上具有相同名称的属性的调用。

When accessing both properties you can also decide which you want, in the following manner: 访问这两个属性时,还可以通过以下方式决定要使用的属性:

    var test = new SearchedProductInternal();       
    Console.WriteLine(test.Name); // returns "Class name"
    Console.WriteLine((test as IProduct).Name); // returns "Interface name"

If your SearchedProductInternal already defines the property Name and you're trying to return the value of same Name property, you don't have to do anything. 如果您的SearchedProductInternal已经定义了Name属性,并且您试图返回同一个Name属性的值,则无需执行任何操作。

Don't create one more property named Name . 不要再创建一个名为Name属性。 Just get rid of the Name property you added. 只是摆脱添加的Name属性。 Everything should work because the class already implemented the contract defined by the interface IProduct . 一切都应该工作,因为该类已经实现了由IProduct接口定义的IProduct

If you want to return different value from the IProduct.Name property, you can use explicit interface implementation . 如果要从IProduct.Name属性返回不同的值,则可以使用显式接口实现

You must change the name of the variable in this case Name. 在这种情况下,您必须更改变量的名称。

If that was an ambigous sentence then remember it's the same for the PC. 如果那是一个模棱两可的句子,请记住,对于PC来说也是一样。 Name cannot be two things. 名称不能是两件事。 but Name and _Name can. 但Name和_Name可以。

public class SearchedProductInternal : IProduct
{
    string _name = "test";
    public string Name
    {
        get
        {
            return _name;
        }
    }
}
public interface IProduct
{
    string Name { get; }
}

I agree with the above answer. 我同意以上答案。 But a minor issue here, we cannot expose the interface member as public, as it causes compile error. 但这里有个小问题,我们不能将接口成员公开,因为它会导致编译错误。

We can have both class level and interface level members. 我们可以同时具有类级别和接口级别的成员。 The interface member cannot be accessed by using class instance, which can be accessed only through interface instance. 使用类实例不能访问该接口成员,该类实例只能通过接口实例进行访问。

public interface IProduct
{
    string Name { get; }
    string ID { get; }
    string DescriptionShort { get; }
    string DescriptionLong { get; }
}

public partial class SearchedProductInternal : IProduct
{
    private string _clsName;

    private string _interfaceName;
    private string _objectID;
    private string _shortDesc;
    private string _longDesc;

    public SearchedProductInternal(string _cName, string _iName)
    {
        _clsName = _cName;
        _interfaceName = _iName;
    }

    public string Name
    {
        get { return _clsName; }
    }

    string IProduct.Name
    {
        get { return _interfaceName; }
    }

    string IProduct.ID
    {
        get { return _objectID; }
    }

    string IProduct.DescriptionShort
    {
        get { return _shortDesc; }
    }

    string IProduct.DescriptionLong
    {
        get { return _longDesc; }
    }
}

class Program
{
    static void Main(string[] args)
    {
        SearchedProductInternal clsSearchProduct = new SearchedProductInternal("clsName", "interfaceName");
        Console.WriteLine(clsSearchProduct.Name);

        IProduct interfaceProduct = (IProduct)clsSearchProduct;
        Console.WriteLine(interfaceProduct.Name);

        Console.ReadLine();
    }
}

I am not sure if I just explained this in a way that was not understood but the way that I got this to work was by just using {get;set;} 我不确定我是否只是以一种无法理解的方式解释了这一点,但是使它起作用的方式仅仅是使用{get; set;}

public partial class SearchedProductInternal : IProduct
    {

        public string ID
        {
            get { return ObjectIdField.ToString(); }
        }
        public string Name  {get;set;}

        public string DescriptionShort{get { return shortDescriptionField; }
        }

        public string DescriptionLong {get { return longDescriptionField; }
        }

} }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM