问题描述
如果我有界面
interface ISite {
int g { get; set; }
}
和一个基类
class Site {
public int g = 1;
}
和实现该接口并扩展该类的派生类
class DSite : Site,ISite {
// comaplains g is not defined
}
它抱怨我没有实现g
属性,但是如何使它从父类Site
中获取呢?
解决方法
在Site
中将g设为属性:
class Site
{
public int g {get;set;} = 1;
}
或在DSite
中显式实现接口:
class DSite : Site,ISite
{
int ISite.g
{
get { return g; }
set { g = value; }
}
}