从具有相同名称属性的接口继承
Inheriting from an interface with properties of the same name
我有一个接口 IProduct 和两个部分 classes SearchedProductInternal 和 SearchedProductExternal。
这两个 classes 扩展了来自第 3 方网络服务搜索的 classes,但两者 return 结果类型略有不同。
我想对两者都使用该接口,因此它们键入 returned 是相同的。我知道如何继承,但由于接口和 SearchedProductInternal 具有相同的对象名称,我该如何处理 return 和 "Name"?
我的界面类似如下:
public interface IProduct
{
string Name { get; }
string ID { get; }
string DescriptionShort { get; }
string DescriptionLong { get; }
}
我的对象 SearchedProductInternal 具有以下属性:
string Name;
int ObjectIdField;
string DescriptionShortField;
string DescriptionLongField;
所以我的这就是我继承的地方
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; }
}
}
我想要 return 已在 SearchedProductInternal class 中独创的名称,但我不知道该怎么做,因为如果我只是输入
return Name
我收到一个 Whosebug 错误,因为它似乎一直在调用它自己?
我认为您在这里应该做的是显式实现接口,这样您就可以同时拥有 class 和 IProduct.Name 中定义的名称 属性 属性 从您的界面。
你可以explicitly implement界面,像这样:
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"; }
}
}
这样您就可以区分对接口属性的调用和对 class 上同名属性的调用。
访问这两个属性时,您还可以通过以下方式决定您想要哪个:
var test = new SearchedProductInternal();
Console.WriteLine(test.Name); // returns "Class name"
Console.WriteLine((test as IProduct).Name); // returns "Interface name"
在这种情况下,您必须更改变量的名称。
如果那是一个模棱两可的句子,那么请记住它对 PC 来说是一样的。名字不能是两个东西。但是 Name 和 _Name 可以。
public class SearchedProductInternal : IProduct
{
string _name = "test";
public string Name
{
get
{
return _name;
}
}
}
public interface IProduct
{
string Name { get; }
}
如果您的 SearchedProductInternal
已经定义了 属性 Name
而您正在尝试 return 相同的值 Name
属性 ,您无需执行任何操作。
不要再创建一个名为 Name
的 属性。只需删除您添加的 Name
属性 即可。一切都应该有效,因为 class 已经实现了接口 IProduct
.
定义的契约
如果你想 return 不同于 IProduct.Name
属性 的值,你可以使用 explicit interface implementation.
同意以上回答。但这里有一个小问题,我们不能将接口成员公开为 public,因为它会导致编译错误。
我们可以同时拥有 class 级和界面级成员。接口成员不能使用class实例访问,只能通过接口实例访问。
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();
}
}
我不确定我是否只是以一种不被理解的方式解释了这一点,但我使它起作用的方式是使用 {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; }
}
}
我有一个接口 IProduct 和两个部分 classes SearchedProductInternal 和 SearchedProductExternal。 这两个 classes 扩展了来自第 3 方网络服务搜索的 classes,但两者 return 结果类型略有不同。 我想对两者都使用该接口,因此它们键入 returned 是相同的。我知道如何继承,但由于接口和 SearchedProductInternal 具有相同的对象名称,我该如何处理 return 和 "Name"?
我的界面类似如下:
public interface IProduct
{
string Name { get; }
string ID { get; }
string DescriptionShort { get; }
string DescriptionLong { get; }
}
我的对象 SearchedProductInternal 具有以下属性:
string Name;
int ObjectIdField;
string DescriptionShortField;
string DescriptionLongField;
所以我的这就是我继承的地方
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; }
}
}
我想要 return 已在 SearchedProductInternal class 中独创的名称,但我不知道该怎么做,因为如果我只是输入
return Name
我收到一个 Whosebug 错误,因为它似乎一直在调用它自己?
我认为您在这里应该做的是显式实现接口,这样您就可以同时拥有 class 和 IProduct.Name 中定义的名称 属性 属性 从您的界面。
你可以explicitly implement界面,像这样:
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"; }
}
}
这样您就可以区分对接口属性的调用和对 class 上同名属性的调用。
访问这两个属性时,您还可以通过以下方式决定您想要哪个:
var test = new SearchedProductInternal();
Console.WriteLine(test.Name); // returns "Class name"
Console.WriteLine((test as IProduct).Name); // returns "Interface name"
在这种情况下,您必须更改变量的名称。
如果那是一个模棱两可的句子,那么请记住它对 PC 来说是一样的。名字不能是两个东西。但是 Name 和 _Name 可以。
public class SearchedProductInternal : IProduct
{
string _name = "test";
public string Name
{
get
{
return _name;
}
}
}
public interface IProduct
{
string Name { get; }
}
如果您的 SearchedProductInternal
已经定义了 属性 Name
而您正在尝试 return 相同的值 Name
属性 ,您无需执行任何操作。
不要再创建一个名为 Name
的 属性。只需删除您添加的 Name
属性 即可。一切都应该有效,因为 class 已经实现了接口 IProduct
.
如果你想 return 不同于 IProduct.Name
属性 的值,你可以使用 explicit interface implementation.
同意以上回答。但这里有一个小问题,我们不能将接口成员公开为 public,因为它会导致编译错误。
我们可以同时拥有 class 级和界面级成员。接口成员不能使用class实例访问,只能通过接口实例访问。
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();
}
}
我不确定我是否只是以一种不被理解的方式解释了这一点,但我使它起作用的方式是使用 {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; }
}
}