如何引用父类在C#中使用的接口



我有一个我的类使用的接口。我有一个子类,我需要引用这个接口。我似乎搞不清楚,这是代码:

接口:

public interface ICanBeCalled
{
string NameCalled{ get; set; }
}

父类:

public class ParentClass: ICanBeCalled
{
private string _name;
public string NameCalled
{
get => _name;
set => _name = value;
}    
// functions below are omitted as not part of question
}

子类:

public class SubClass : ParentClass
{
// Here I want to be able to set and get the name var, 
// how can this be done? 
// Ideally the set functionality would have it 
// hard coded in a private string.
}

您可以在SubClass的构造函数中设置父值

public class SubClass: ParentClass
{
SubClass()
{
this.NameCalled = "MyHardcodedName";
}
}

最新更新