小编Aja*_*rma的帖子

'instanceof'运算符对接口和类的行为有所不同

我想知道instanceofJava 中运算符的以下行为.

interface C {}

class B {}

public class A {
    public static void main(String args[]) {
        B obj = new B();
        System.out.println(obj instanceof A);      //Gives compiler error
        System.out.println(obj instanceof C);      //Gives false as output
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样?interface C和之间没有关系class B,但是它给出了假,而在obj instanceof A它出现编译错误的情况下?

java inheritance interface class instanceof

88
推荐指数
1
解决办法
3538
查看次数

标签 统计

class ×1

inheritance ×1

instanceof ×1

interface ×1

java ×1