首先,我们可以存储
instances这样实现特定功能
interface的
interface reference variable类。
package com.test;public class Test implements Testeable { public static void main(String[] args) { Testeable testeable = new Test(); // OR Test test = new Test(); if (testeable instanceof Testeable) System.out.println("instanceof succeeded"); if (test instanceof Testeable) System.out.println("instanceof succeeded"); }}interface Testeable {}即,任何实现特定接口的运行时实例都将通过
instanceof测试
编辑
和输出
instanceof succeededinstanceof succeeded
@RohitJain
您可以通过使用这样的匿名内部类来创建接口的实例
Runnable runnable = new Runnable() { public void run() { System.out.println("inside run"); }};然后使用
instanceof类似的运算符测试实例的类型为interface
System.out.println(runnable instanceof Runnable);
结果为“ true”



