使用可以属于不同类的参数调用函数
我有两个在领域略有不同的课程。是否可以实现一个可以接受第一类和第二类对象作为参数的函数。我尝试使用接口来实现它,但是在设计类时,它给出了错误 - “Class1Top”未实现接口成员“interface Top.Property2”。 “Class1 Top.Property2”无法实现“interfaceTop.Property2”,因为它没有相应的返回类型“interfaceMiddle”。
public interface interfaceTop {
int Property1 { get; set; }
interfaceMiddle Property2 { get; set; }
}
public interface interfaceMiddle {
interfaceBottom Property1 { get; set; }
}
public interface interfaceBottom {
int Property1 { get; set; }
}
public class Class1Top :interfaceTop {
public int Property1 { get; set; }
public Class1Middle Property2 { get; set; }
}
public class Class1Middle : interfaceMiddle {
public Class1Bottom Property1 { get; set; }
}
public class Class1Bottom : interfaceBottom {
public int Property1 { get; set; }
}
public class Class2Top :interfaceTop {
public int Property1 { get; set; }
public Class2Middle Property2 { get; set; }
public int Property3 { get; set; }
}
public class Class2Middle : interfaceMiddle {
public Class2Bottom Property1 { get; set; }
}
public class Class2Bottom: interfaceBottom {
public int Property1 { get; set; }
public int Property2 { get; set; }
}
// I need just such a check
public class result {
public bool Result(interfaceTop obj) {
return obj.Property2.Property1.Property1 == 1;
}
}
如何实现这种传输的可能性或检查的可能性 结果 public bool(顶层接口对象)
I have two classes that are slightly different in fields. Is it possible to implement a function that could accept objects of both one and the second class as a parameter. I tried to implement it using interfaces, but when designing classes, it gives an error -
"Class1Top" does not implement the interface member "interface Top.Property2". "Class1 Top.Property2"cannot implement "interfaceTop.Property2", because it does not have the corresponding return type "interfaceMiddle".
public interface interfaceTop {
int Property1 { get; set; }
interfaceMiddle Property2 { get; set; }
}
public interface interfaceMiddle {
interfaceBottom Property1 { get; set; }
}
public interface interfaceBottom {
int Property1 { get; set; }
}
public class Class1Top :interfaceTop {
public int Property1 { get; set; }
public Class1Middle Property2 { get; set; }
}
public class Class1Middle : interfaceMiddle {
public Class1Bottom Property1 { get; set; }
}
public class Class1Bottom : interfaceBottom {
public int Property1 { get; set; }
}
public class Class2Top :interfaceTop {
public int Property1 { get; set; }
public Class2Middle Property2 { get; set; }
public int Property3 { get; set; }
}
public class Class2Middle : interfaceMiddle {
public Class2Bottom Property1 { get; set; }
}
public class Class2Bottom: interfaceBottom {
public int Property1 { get; set; }
public int Property2 { get; set; }
}
// I need just such a check
public class result {
public bool Result(interfaceTop obj) {
return obj.Property2.Property1.Property1 == 1;
}
}
How to implement such a possibility of transmission or the possibility of checking as a
result of
public bool (top interface object)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找的是泛型:
您可以做一个泛型类,可以设置泛型参数实现任意接口。查看 这个
所以你的解决方案例如可以是这样的:
What you are looking for are Generics:
You can do a generic class , and you can set that the generic parameter implements any interface. Check out this
So your solution can be for example something like this: