简体   繁体   English

在 c# 中实现接口时,如何从父 class 访问属性?

[英]How to access properties from parent class when implementing interface in c#?

If I have an interface如果我有一个界面

interface ISite {
    int g { get; set; }
}

and a base class和一个基础 class

class Site {
    public int g = 1;
}

and a derived class that implements the interface and extends the class和一个派生的 class 实现接口并扩展 class

class DSite : Site, ISite {
    // comaplains g is not defined
}

It complains that I did not implement the g property, but how do I make it take it from the parent class Site ?它抱怨我没有实现g属性,但是如何让它从父 class Site获取它?

Either make g in Site a property:要么使Site中的 g 成为属性:

class Site
{
    public int g {get;set;} = 1;
} 

or explicitly implement the interface in DSite :或在DSite中显式实现接口:

class DSite : Site, ISite
{
    int ISite.g
    {
        get { return g; }
        set { g = value; }
    }
}

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

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