Check if a type is an interface

后端 未结 3 1058
灰色年华
灰色年华 2021-01-11 14:57

I want to validate a parameter sent to a method, it must be an interface type. What to ask?

void (Class interfaceType){
  if (thisisnotaninterface){         


        
相关标签:
3条回答
  • 2021-01-11 15:22

    You have got a Class#isInterface() method that does exactly what you want: -

    if (!interfaceType.isInterface()) {
        throw...
    }
    
    0 讨论(0)
  • 2021-01-11 15:26

    For a normal java object you can always use instanceof.

    If Test implements Testable

    Test test = new Test();
    

    test instanceof Testable will be true

    0 讨论(0)
  • 2021-01-11 15:27

    Just use Class#isInterface() to check that

    And seriously, you should be reading the Javadocs before asking here.

    0 讨论(0)
提交回复
热议问题