java javac 编译错误-“...抽象;无法实例化”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2073156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:16:10  来源:igfitidea点击:

javac compile error - "...abstract; cannot be instantiated"

java

提问by tom smith

New to java... Trying to get a sample app to run. I'm including the section of code that appears to be relevant to the issue. I can post the entire test app if needed. I

Java 新手...正在尝试运行示例应用程序。我包括了似乎与问题相关的代码部分。如果需要,我可以发布整个测试应用程序。一世

I'm trying to implement the errorhandler to handle the css warnings that are generated when the app is parsing targeted websites via the htmlunit lib/test that the app runs.

我正在尝试实现错误处理程序来处理应用程序通过应用程序运行的 htmlunit lib/test 解析目标网站时生成的 css 警告。

I'm not sure exactly how the MycssErrorHandler class should be implemented in order to invoke the ErrorHandler. I'm also not exactly sure how to instantiate the object in the main body of the code/test class.

我不确定应如何实现 MycssErrorHandler 类以调用 ErrorHandler。我也不确定如何在代码/测试类的主体中实例化对象。

Thoughts/Comments/Code chunks would be helpful..

想法/评论/代码块会有所帮助..

Thanks!

谢谢!

I'm getting the following error when I compile:

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

[root@toshiba parseapp2]# javac -Xlint -classpath '/opt/htmlunit/lib/*:/parseapp2/' sjsu_classes.java

warning: [path] bad path element "/opt/htmlunit/lib/xml-apis.jar": no such file or directory
warning: [path] bad path element "/opt/htmlunit/lib/xercesImpl.jar": no such file or directory
warning: [path] bad path element "/opt/htmlunit/lib/serializer.jar": no such file or directory
sjsu_classes.java:92: sjsu_classes.MycssErrorHandler is abstract; cannot be instantiated
        ErrorHandler ierr = new MycssErrorHandler(); 
                        ^
1 error
3 warnings

====================================

The test code chunk is:

测试代码块是:

import org.w3c.css.sac.ErrorHandler;
    import com.gargoylesoftware.htmlunit.DefaultCssErrorHandler;
import org.xml.sax.SAXParseException;

public class sjsu_classes {

    //==handle the warnings thrown from the js functions..
    public static class MyIncorrectnessListener implements IncorrectnessListener
    {
      @Override
      public void notify(String arg0, Object arg1)
      {
        //System.err.println("Argument : " + arg0.toString() + ", Object : ");
      }
    }     

    //==handle the warnings thrown from the css functions..
//  public static class MycssErrorHandler implements DefaultCssErrorHandler
//  public static class MycssErrorHandler implements ErrorHandler
//  public class MycssErrorHandler implements ErrorHandler
    public abstract class MycssErrorHandler implements ErrorHandler
//  protected class MycssErrorHandler implements ErrorHandler
    {
      //@Override
      public void notify(String arg0, Object arg1)
      {
        //System.err.println("Argument : " + arg0.toString() + ", Object : ");
      }
      //@Override
        public void fatalError(SAXParseException ex)
        {
            //fatals.add(ex);
        }
    } 

    //public static void main(String[] args) throws Exception {
    public void main(String[] args) throws Exception {

        // Create and initialize WebClient object
//     WebClient webClient = new WebClient(BrowserVersion.EXPLORER_7);
       WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
           //WebClient webClient = new WebClient();

        IncorrectnessListener ilisten = new MyIncorrectnessListener(); 
        ErrorHandler ierr = new MycssErrorHandler(); 
        webClient.setIncorrectnessListener(ilisten); 
        webClient.setCssErrorHandler(ierr); 

采纳答案by SLaks

Your MycssErrorHandlerclass is abstract, meaning that you can't use it directly.

你的MycssErrorHandler类是abstract,这意味着你不能直接使用它。

An abstractclass cannot be instantiated; it can only be inherited from.

一个abstract类不能被实例化;它只能继承自。

You need to remove the abstractkeyword from the class definition.

您需要abstract从类定义中删除关键字。

Once you remove the abstractkeyword, you will also need to make your class implement all three method in the ErrorHandlerinterface. (error, fatalError, and warning)

删除abstract关键字后,您还需要让您的类在ErrorHandler接口中实现所有三个方法。( error, fatalError, 和warning)

回答by futureelite7

As the error says, you have declared MycssErrorHandler as abstract. You cannot create a class that is abstract. You will have to either remove the abstract keyword or implement an additional class that is derived from MycssErrorHandler.

正如错误所说,您已将 MycssErrorHandler 声明为抽象的。你不能创建一个抽象的类。您将必须删除抽象关键字或实现从 MycssErrorHandler 派生的附加类。

(Why do you need to declare MycssErrorHandler as abstract anyway? You're supposed to implement a concrete class using the interface :))

(为什么你需要将 MycssErrorHandler 声明为抽象的?你应该使用接口实现一个具体的类:))