【发布时间】:2015-06-14 02:27:09
【问题描述】:
我有一个类扩展了另一个类:
public class PDFCrawler extends WebCrawler
我正在重写我的 PDFCrawler 类中的一个方法,如下所示:
@Override
public boolean shouldVisit(Page page, WebURL url) {...}
它在 Eclipse 上给了我错误。为项目设置了 Java 8:
PDFCrawler 类型的方法 shouldVisit(Page, WebURL) 必须覆盖 或实现超类型方法。
但是,在同一个 PDFCrawler 类中,当我如下覆盖不同的方法时,不会显示错误:
@Override
public void visit(Page page) {...}
这两个方法都来自名为 WebCrawler 的超类。超类方法如下:
public boolean shouldVisit(Page page, WebURL url) {
return true;
}
public void visit(Page page) {
// Do nothing by default
// Sub-classed should override this to add their custom functionality
}
超类取自Crawler4j project。由于我无法覆盖第一个方法,因此超类中的方法总是被执行。
有什么线索吗?
编辑 我的类 PDFCrawler 的导入语句:
package com.example.ict;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.regex.Pattern;
import com.google.common.io.Files;
import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.crawler.WebCrawler;
import edu.uci.ics.crawler4j.url.WebURL;
【问题讨论】:
-
相同的
Page类型?相同的WebURL类型? -
也许你正在使用来自不同包的
WebURL.. -
嗨@SotiriosDelimanolis 我的方法和超类方法的参数是相同的。即使我保持数据类型和名称与超类中的相同
-
@CommuSoft 我想你误会了。
WebCrawler声明了一个带有edu.uci.ics.crawler4j.crawler.Page和edu.uci.ics.crawler4j.url.WebURL类型参数的shouldVisit方法。如果 OP 在与PDFCrawler相同的包中具有com.example.Page类型,则在编译PDFCrawler时将使用该Page类型。 -
@CommuSoft 另外,我不相信你可以扩大 Java 中的参数类型。
标签: java inheritance subclass superclass overriding