1
我想知道如果有人可以建议,如果一个类可以一次实现以下接口?一个类可以实现具有相同datamember名称但不同类型的多个接口吗?
interface a1
{
int mycount;
}
interface a2
{
string mycount;
}
interface a3
{
double mycount;
}
我想知道如果有人可以建议,如果一个类可以一次实现以下接口?一个类可以实现具有相同datamember名称但不同类型的多个接口吗?
interface a1
{
int mycount;
}
interface a2
{
string mycount;
}
interface a3
{
double mycount;
}
你的接口都不会编译,我假设它们是方法而不是字段。
实现与冲突的membernames多个接口的唯一方法是通过使用显式实现:
interface a1
{
int mycount();
}
interface a2
{
string mycount();
}
class Foo : a1, a2
{
int a1.mycount() { ... }
string a2.mycount() { ... }
// you can _only_ access them through an interface reference
// even Bar members need to typecast 'this' to call these methods
void Bar()
{
var x = mycount(); // Error, won't compile
var y = (this as a2).mycount(); // Ok, y is a string
}
}
谢谢Henk。这是假设您已将数据成员更改为方法,并且我知道此实现将起作用。 我的问题是,在最近的一次采访中,我遇到了同样的问题,并且我的答案被拒绝了 - 来自不同界面的同名数据成员不可能继承到同一个类中。所以我想确认这是正确的。面试官因为某种原因认为不然,这让我感到十分困惑。 – Alag20 2013-05-11 09:53:55
你有**试过** – 2013-05-11 09:20:48
这些特性,而不是场吧? – 2013-05-11 09:28:30
这些是数据成员而不是方法。 – Alag20 2013-05-11 09:53:18