[英]How to give two different classes the same interface?
Can I give 2 different classes (that I cannot modify as they are autocreated webservice classes) the same interface? 我可以给2个不同的类(我不能修改,因为它们是自动处理的webservice类)相同的接口?
Problem: I have several autocreated webservice classes, that have a method with the same signature. 问题:我有几个自动处理的webservice类,它们有一个具有相同签名的方法。 But as the classes have no common interface, I cannot group them and thus cannot call both of them from a single method.
但由于类没有通用接口,我无法对它们进行分组,因此无法从单个方法中调用它们。
Example: 例:
class A {
void sameMethod();
}
class B {
void sameMethod();
}
class MyService() {
//I cannot do the following as I cannot group A and B with the same interface
void callAorB(<Class A or B> object) {
object.sameMethod();
}
}
Will it still be somehow possible to just execute òbject.sameMethod() even though I cannot make a common reference between
class A and
class B`? even though I cannot make a common reference between
A类and
类even though I cannot make a common reference between
它仍然可能以某种方式执行òbject.sameMethod()吗?
The hacked solution would be: 被黑客攻击的解决方案是:
if(object instanceof A)
((A)object).sameMethod();
else if(object instanceof B)
((B)object).sameMethod();
but if you can make them implement an interface would be much better 但如果你能让他们实现一个接口会好得多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.