为什么Java可以在Eclipse中出现编译时错误的情况下运行 [英] why java can run with a compile time error in eclipse

查看:85
本文介绍了为什么Java可以在Eclipse中出现编译时错误的情况下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    interface A
    {
        public void f();
        public void g();
    }


   class B implements A
   {
       public void f() 
       {
            System.out.println("B.f()");
       }
   }

   public class Main 
   {
       public static void main(String[] args) 
       {
            B tmp = new B();
            tmp.f();
            System.out.println("B.f()");
       }
   }

我没有在B的接口A中实现所有方法 并且有一个错误

I don't implement all the method in the interface A in B and it has a error that

    The type B must implement the inherited abstract method A.g()

但是为什么它可以获得这样的输出

but why it can get the output that

    B.f()
    B.f()

推荐答案

Eclipse可以修补"某些类的编译错误,即使存在错误也可以运行程序.通常,您会看到一个对话框,显示以下内容:

Eclipse can "patch" around certain classes of compile errors, and run a program even if errors exist. Normally, you get a dialog box that says the following:

所需的项目中存在错误:

Errors exist in required project(s):

(项目名称)

继续启动吗?

[X]总是在不询问的情况下启动

[X] Always launch without asking

如果选择继续,或者如果禁用了对话框,则Eclipse将在可能的情况下继续修复编译错误.如果尝试运行受编译错误影响的代码,则会得到运行时异常.

If you select Proceed, or if you have disabled the dialog, Eclipse will proceed to fix the compile errors if possible. If you try to run code impacted by the compile error, you'll get a runtime exception.

在这种情况下,Eclipse添加了一个仅包含以下内容的伪B.g()方法:

In this case, Eclipse adds a dummy B.g() method containing just the following:

throw new java.lang.Error("Unresolved compilation problem: \n"
"The type B must implement the inherited abstract method A.g()");

通过插入此虚拟方法,代码将正确编译并运行.如果您从不致电B.g,那么您将永远不会看到此错误.

By inserting this dummy method, the code will compile properly and it will run. If you never call B.g, then you'll never see this error.

这篇关于为什么Java可以在Eclipse中出现编译时错误的情况下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆