【问题标题】:Errors in using instanceof operator with an interface?将 instanceof 运算符与接口一起使用时出错?
【发布时间】:2012-10-15 12:18:27
【问题描述】:

我正在尝试根据链接了解 Java 中的 instanceof 运算符:- instanceof

但是当我尝试运行他们的以下代码时:-

class InstanceofDemo {
public static void main(String[] args) {

    Parent obj1 = new Parent();
    Parent obj2 = new Child();

    System.out.println("obj1 instanceof Parent: "
        + (obj1 instanceof Parent));
    System.out.println("obj1 instanceof Child: "
        + (obj1 instanceof Child));
    System.out.println("obj1 instanceof MyInterface: "
        + (obj1 instanceof MyInterface));
    System.out.println("obj2 instanceof Parent: "
        + (obj2 instanceof Parent));
    System.out.println("obj2 instanceof Child: "
        + (obj2 instanceof Child));
    System.out.println("obj2 instanceof MyInterface: "
        + (obj2 instanceof MyInterface));
}

}

class Parent {}
class Child extends Parent implements MyInterface {}
interface MyInterface {}

我在编译时收到以下错误:

./Child.java:1: error: cannot find symbol
class Child extends Parent implements MyInterface{
                                      ^
  symbol: class MyInterface
InstanceOfDemo.java:9: error: cannot find symbol
System.out.println("obj1 instanceOf MyInterface" + (obj1 instanceof MyInterface));
                                                                    ^
  symbol:   class MyInterface
  location: class InstanceOfDemo
InstanceOfDemo.java:12: error: cannot find symbol
System.out.println("obj1 instanceOf MyInterface" + (obj2 instanceof MyInterface ));
                                                                    ^
  symbol:   class MyInterface
  location: class InstanceOfDemo
3 errors

下面是放置src代码的目录结构:-

-rw-rw-r-- 1 ankit ankit 174 Oct 25 15:36 Child.class
-rw-rw-r-- 1 ankit ankit  31 Oct 25 15:36 Child.java
-rw-rw-r-- 1 ankit ankit 920 Oct 25 15:41 InstanceOfDemo.class
-rw-rw-r-- 1 ankit ankit 637 Oct 25 15:41 InstanceOfDemo.java
-rw-rw-r-- 1 ankit ankit  25 Oct 25 15:20 MyInterface
-rw-rw-r-- 1 ankit ankit 186 Oct 25 15:36 Parent.class
-rw-rw-r-- 1 ankit ankit  16 Oct 25 15:18 Parent.java

注意:我从Child.javaInstanceOfDemo.java 中删除了接口实现以编译和运行代码。

编辑 1:- 我可以认为 instanceof 运算符不能与接口一起使用,因为根据 Oracle 对 instanceof 运算符的定义如下:-

instanceof 运算符将对象与指定类型进行比较。您可以使用它来测试对象是类的实例、子类的实例还是实现特定接口的类的实例。

【问题讨论】:

  • MyInterface 似乎是在不同的文件中定义的,文件名甚至不是MyInterface.java。因此没有生成 .class 文件(因此编译器找不到)
  • 哇,回答这个问题似乎需要 7 分钟。

标签: java interface instanceof


【解决方案1】:

你的界面也应该在.java file,所以你需要把MyInterface放在MyInterface.java

-rw-rw-r-- 1 ankit ankit  25 Oct 25 15:20 MyInterface<---Rename it as 
                                                         MyInterface.java

再次编译后一切正常

【讨论】:

    【解决方案2】:

    查看有问题的文件列表,您的 MyInterface 缺少 .java 扩展名。

    instanceof 肯定可以与接口一起使用。

    如果您想知道“或实现特定接口的类的实例”部分,这意味着myObj instanceof AnInterface 测试 myObj 是否是实现特定接口的类的实例,即 AnInterface。

    【讨论】:

      【解决方案3】:

      MyInterface 似乎是在不同的文件中定义的,文件名甚至不是MyInterface.java。因此不会生成 .class 文件(因此编译器不会找到)

      解决方法是将文件MyInterface重命名为MyInterface.java并编译。这将生成一个 .class 文件,该文件由您的 JVM 加载。

      【讨论】:

        【解决方案4】:

        将 MyInterface 重命名为 MyInterface.java。您的目录列表和错误消息都同意它从未编译为类文件。

        【讨论】:

          【解决方案5】:

          将您的文件 MyInterface 重命名为 MyInterface.java 然后您的代码将运行

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-02-13
            • 2021-11-09
            • 2023-02-02
            • 2011-04-12
            • 2018-04-22
            • 1970-01-01
            • 1970-01-01
            • 2018-09-04
            相关资源
            最近更新 更多