Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java检查子级是否正在实现接口_Java_Interface_Polymorphism_Abstract - Fatal编程技术网

Java检查子级是否正在实现接口

Java检查子级是否正在实现接口,java,interface,polymorphism,abstract,Java,Interface,Polymorphism,Abstract,我想通过实现接口来做一些事情(不在子类中编写主体) 这就是我想要实现的目标: interface I{ void foo(); } 具有功能的类I: class A extends C implements I{ } class B extends C { } 不带功能的类I: class A extends C implements I{ } class B extends C { } 父类: class C implements I{ @Override

我想通过实现接口来做一些事情(不在子类中编写主体)

这就是我想要实现的目标:

interface I{
    void foo();
}
具有功能的类
I

class A extends C implements I{


}
class B extends C {

}
不带功能的类
I

class A extends C implements I{


}
class B extends C {

}
父类:

class C implements I{
    @Override
    public void foo(){
        // if child is implementing interface dirctly it should do stuff
        // Skip this otherwise 
    }
}
但是如何检查我的
A
类是否直接实现
I
接口?要做到这一点:

主要内容:

主类{
公用干管(){
I classA=新的A();
classA.foo();//做事
I classB=new B();//尝试
if(I的一个实例)

如果(I的一个实例)

尝试
if(I的一个实例)


这应该起作用

如果您知道方法名称,可以使用反射检查:

class Main {
  public main() throws Exception{
    I classA = new A();
    boolean implementsIByItself=true;
    try{
      classA.getClass().getDeclaredMethod("foo", new Class[0]);
    } catch (NoSuchMethodException nsme){
      //Exception will be thrown if A doesnt declare "foo"
      implementsIByItself=false;
    }

  }
}
getDeclaredMethod
将只返回一个
方法
对象,如果调用它的类不仅继承它,而且声明它


请记住,这不是一种非常干净的方法,可能会有更合适的解决方案。如果您依赖于知道哪些类通过重写方法来更改行为,那么您的问题看起来像是设计中的一个缺陷。

如果您知道方法名称,可以使用反射检查它:

class Main {
  public main() throws Exception{
    I classA = new A();
    boolean implementsIByItself=true;
    try{
      classA.getClass().getDeclaredMethod("foo", new Class[0]);
    } catch (NoSuchMethodException nsme){
      //Exception will be thrown if A doesnt declare "foo"
      implementsIByItself=false;
    }

  }
}
getDeclaredMethod
将只返回一个
方法
对象,如果调用它的类不仅继承它,而且声明它


请记住,这不是一个非常干净的方法,可能会有更合适的解决方案。如果您依赖于知道哪个类通过重写方法来更改行为,那么您的问题看起来像是设计中的一个缺陷。

虽然这在多态性方面没有任何意义,但您可以使用以下内容检查该类是否影响直接输入
I

package example;

public class A extends B implements I{
    public static void main(String...args)
    {
        new A().foo();
        new B().foo();
        new C().foo();
    }
}
class B extends C{

} 
class C implements I{

    @Override
    public void foo() {
        boolean implementsDirectly = false;
        for (Class c : getClass().getInterfaces()) {
            if(c.getName().equals("example.I")) {
                implementsDirectly = true;
            }
        }
        if(implementsDirectly) {
            System.out.println("The class " + getClass().getName() +" does implement I directly in the class definition");
        } else {
            System.out.println("The class " + getClass().getName() +" does not implement I directly in the class definition");
        }
    }

}

interface I {
    void foo();
}
输出:

The class example.A does implement I directly in the class definition
The class example.B does not implement I directly in the class definition
The class example.C does implement I directly in the class definition

虽然这在多态性方面没有任何意义,但您可以使用下面的内容检查类是否直接实现了
I

package example;

public class A extends B implements I{
    public static void main(String...args)
    {
        new A().foo();
        new B().foo();
        new C().foo();
    }
}
class B extends C{

} 
class C implements I{

    @Override
    public void foo() {
        boolean implementsDirectly = false;
        for (Class c : getClass().getInterfaces()) {
            if(c.getName().equals("example.I")) {
                implementsDirectly = true;
            }
        }
        if(implementsDirectly) {
            System.out.println("The class " + getClass().getName() +" does implement I directly in the class definition");
        } else {
            System.out.println("The class " + getClass().getName() +" does not implement I directly in the class definition");
        }
    }

}

interface I {
    void foo();
}
输出:

The class example.A does implement I directly in the class definition
The class example.B does not implement I directly in the class definition
The class example.C does implement I directly in the class definition

你的确切意思是什么?如果方法
foo
A
覆盖?否则编译器会告诉你
A
是否继承自
I
,不清楚你在问什么不清楚你想问什么..但是你可以尝试使用
instanceof
你说的不正确。首先,“具有特性I的类A”-它已经从父类C实现了I,不需要在这里写“实现I”。第二,“没有特性I的类(B)”"-不正确,因为B扩展了实现I的C,所以B类也实现了I。我想你在这里把自己搞糊涂了……试着让C类不实现I,你会得到一个编译器错误,正如我怀疑你可能想看到的那样。@vikingsteve是的,我知道->这就是我试图实现的。你的确切意思是什么?如果方法
foo
超过了由
A
识别?否则编译器将告诉您
A
是否继承自
I
,不清楚您在问什么不清楚您在这里试图问什么..但是您可以尝试使用
instanceof
您所说的是错误的。首先,“具有特性I的A类”-它已经从父类C实现了I,不需要在这里写“实现I”。第二,“没有特性I的类(B)”-不正确,因为B扩展了实现I的C,类B也实现了I。我想你在这里混淆了自己…试着让类C不实现I,你会得到一个编译器错误,我怀疑你可能想看到这个错误。@vikingsteve yes I know->这是我试图实现的。这没有任何意义,如果a是一个类。例如,如果a是一个字符串,我是RandomAccess,这甚至不会编译。这毫无意义,如果a是一个类,结果在编译时是已知的。例如,如果a是一个字符串,我是RandomAccess,这甚至不会编译