[英]How do I create a method to check that an implementation satisfies an interface?
I have an interface (p) and an implementation (imp). 我有一个接口(p)和一个实现(imp)。 If I do the following in the code, then the check works: 如果我在代码中执行以下操作,则检查有效:
if (!(imp instanceof p)) {
logger.error(imp.getClass().getName()+
" doesn't satisfy the interface "
+p.getClass().getName());
}
I tried to make it into a callable method as follows: 我试着把它变成一个可调用的方法,如下所示:
private boolean checkInterfaceImplementation(Object implemen, Object inter){
return (implemen instanceof inter);
}
which failed. 失败了。
Then I found that inter needs to be a specific type and I cannot use a generic object. 然后我发现inter需要是一个特定的类型,我不能使用通用对象。 Then I found out about 然后我发现了
"B.class.isAssignableFrom(A.getClass())"
Then I did: 然后我做了:
System.out.println("B.class.isAssignableFrom(A
.getClass())");
The output was 输出是
true 真正
I read up more from this question. 我从这个问题中读到了更多。 My question is "Is this (the second implementation with ".isAssignableFrom") the preferred or standard way to implement said method? Is there any way that this present implementation can create problems? 我的问题是“这是(带有”.isAssignableFrom“的第二个实现)是实现所述方法的首选或标准方法吗?这个现有的实现有没有办法产生问题?
It's hard to understand your question. 很难理解你的问题。 I will edit when more details are received, if necessary, but as a general rule of thumb instanceof is an indicator of a lack of polymorphism and a design issue. 如果有必要,我会在收到更多详细信息时进行编辑,但作为一般经验法则, instanceof是缺乏多态性和设计问题的指标。 Not always the case, but if you are a beginner I would try to use it as little as possible. 并非总是这样,但如果你是初学者,我会尝试尽可能少地使用它。
Instead, consider why that check is even there. 相反,考虑为什么检查甚至在那里。 If "imp" implements "p", then you are guaranteeing that any "imp" will have all the methods in "p". 如果“imp”实现“p”,那么您保证任何“imp”将具有“p”中的所有方法。 If it doesn't, you will receive a compiler error before you can even build. 如果没有,您甚至可以在构建之前收到编译器错误。 This is very abstract right now so I will do quick example. 这是非常抽象的,所以我会做一些快速的例子。
public interface Runs {
public void run();
}
public class Cat implements Runs {
int numLegs;
public Cat() {
this.numLegs = 4;
}
public void run() {
System.out.println("does whatever running cats do");
}
}
public class Human implements Runs {
int numLegs;
public Human() {
this.numLegs = 2;
}
public void run() {
System.out.println("does whatever running humans do");
}
}
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
Human human = new Human();
ArrayList<Runs> listOfRunners = new ArrayList<Runs>();
listOfRunners.add(cat);
listOfRunners.add(human);
Runs runner = listOfRunners.get(0);
/* no compiler error because by implementing Runs we guarantee it has run() method */
runner.run();
runner = listOfRunners.get(1);
/* It doesn't matter what the object is. We don't care if it is cat or human */
runner.run();
}
}
Not sure exactly what you are trying to do, but something like this should work: 不确定你想要做什么,但这样的事情应该有效:
inter.getClass().isInstance(implemen)
Most likely what you are trying to do can be done in a much better way than resorting to this, though. 尽管如此,最有可能的是你要做的事情要比采用这种做法更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.