Joh*_*nny 1 c# oop wpf winforms
为什么我不能通过它的接口(ItestClass)访问基类(testClass)属性?我创建了接口以避免实际的Control(winforms/wpf)属性在第三个类(newClass)中可见.如果那不可能有更好的方法吗?
public class testClass : Control, ItestClass
{
public int Property1 { set; get; }
public int Property2 { set; get; }
public testClass() { }
}
public interface ItestClass
{
int Property1 { get; set; }
int Property2 { get; set; }
}
public class newClass : ItestClass
{
public newClass()
{
// Why are the following statements are not possible?
Property1 = 1;
// OR
this.Property1 = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
接口实际上并没有实现属性 - 您仍然需要在实现类中定义它们:
public class newClass : ItestClass
{
int Property1 { get; set; }
int Property2 { get; set; }
// ...
}
Run Code Online (Sandbox Code Playgroud)
编辑
C#不支持多重继承,因此您不能testClass继承Control和另一个具体类.不过,你总是可以使用构图.例如:
public interface ItestClassProps
{
public ItestClass TestClassProps { get; set; }
}
public class testClass : Control, ItestClassProps
{
public ItestClass TestClassProps { set; get; }
public testClass() { }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |