简体   繁体   English

如何创建检查实现是否满足接口的方法?

[英]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.

相关问题 我如何在比较接口的两种实现类型的通用接口中声明 static equals 方法? - How do i declare a static equals method in a generic interface that compares two types of implementation of the interface? 如何测试没有实现的接口 - How do I test an interface that has no implementation 检查实现后,如何允许来自接口的方法? - after check for implementation, how to allow method from interface? 如何创建传递对象的方法来实现接口? - How do I create a method passing an object implements an interface? 如何在Java中使用泛型来创建一个泛型接口,该接口定义仅接受实现作为参数的方法? - How would I use generics in Java to create a generic interface that defines a method that only accepts the implementation as a parameter? 如果我已经有一个泛型的实现,如何声明一个接口方法 - How to declare an interface method if i already have an implementation with generic 如何为Fields创建功能接口实现? - How can I create a Functional interface implementation for Fields? 如何将接口与通用类的实现绑定? - How do I bind the Interface with Implementation for Generic Classes? 我如何确保通过接口实现公共静态功能 - How do i ensure implementation of a public static function with interface 如何打印界面的方法 - How do I print method of interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM