Java 实例化扩展实现接口的抽象类的类

Java 实例化扩展实现接口的抽象类的类,java,inheritance,abstract-class,Java,Inheritance,Abstract Class,我有一个名为COL的类,它包含以下函数: public class CatalogueOfLife extends EDITPlatform { private static final String nomatchfound = "there is nothing"; protected String getNoResultsMessage(){ return COL.nomatchfound; } public interface WSInterface { public

我有一个名为COL的类,它包含以下函数:

public class CatalogueOfLife extends EDITPlatform {

private static final String nomatchfound = "there is nothing";

protected String getNoResultsMessage(){
      return COL.nomatchfound;
}
public interface WSInterface {

public boolean matchfound(String noMatchString);
}
然后是一个名为Interface的接口,该接口具有以下功能:

public class CatalogueOfLife extends EDITPlatform {

private static final String nomatchfound = "there is nothing";

protected String getNoResultsMessage(){
      return COL.nomatchfound;
}
public interface WSInterface {

public boolean matchfound(String noMatchString);
}
最后是一个名为platform的抽象类,它使用以下两个函数:

public abstract class EDITPlatform implements WSInterface{

public boolean matchfound(String noMatchString){
   if (noMatchString.contains(getNoResultMessage())){
        return true;
   }
   return false; }
现在我想从一个主函数调用matchfound函数。我只能使用COL对象,但问题是我需要不止一个类来扩展平台

更新:您的问题是EDITPlatform没有getNoResultMessage方法。所以你不能叫它

如果需要在catalogeoflife中定义该方法,则应使用抽象方法:

public abstract class EDITPlatform implements WSInterface{

    protected abstract String getNoResultMessage();

    public boolean matchfound(String noMatchString){
        if (noMatchString.contains(getNoResultMessage())){
            return true;
        }
        return false; 
    }
}
然后在solid类中重写它

public class CatalogueOfLife extends EDITPlatform {

    private static final String nomatchfound = "there is nothing";

    @Override
    protected String getNoResultsMessage(){
        return CatalogueOfLife.nomatchfound;
    }
}
最后

CatalogueOfLife col = new CatalogueOfLife();
boolean matched = col.matchFound(myString);

如果COL是Platform的子类,那么在代码中

if (noMatchString.contains(getNoResultsMessage()))
无法在平台中访问getNoResultsMessage

而且,由于您已经在平台中实现了接口的所有方法,所以它不必是抽象的

这就是你想要的吗:

class CatalogueOfLife // extends EDITPlatform
{
    private static final String nomatchfound = "there is nothing";

    protected String getNoResultsMessage() {
        return CatalogueOfLife.nomatchfound;
    }
}

interface WSInterface {
    public boolean matchfound(String noMatchString);
}

class EDITPlatform extends CatalogueOfLife implements WSInterface {
    public boolean matchfound(String noMatchString) {
        if (noMatchString.contains(getNoResultsMessage()))
            return true;
        return false;
    }
}

public class MainClass {

    public static void main(String[] args) {
        EDITPlatform plat = new EDITPlatform();
        System.out.println(plat.getNoResultsMessage());
        System.out.println(plat.matchfound("there is nothing"));
    }

}

事实上,我不明白问题在哪里。。。为什么只能使用COL对象?为什么需要有几个扩展平台的类?你想做一些像COL=新平台的事情吗;然后能够进行col.matchfoundaString?因为实际上我将拥有除col之外的其他类来扩展抽象类。例如,我将WAP和COL都扩展平台。。并且每个都将向平台函数发送不同的nomatchfound字符串。。我需要从主平台调用函数,而不必明确指定子类调用它。我严重怀疑您的接口是否如您所说被称为接口,因为这是Java保留关键字。请张贴真实的类和接口声明,而不是用英语描述它们;这也告诉我们类层次结构的外观。该接口称为WSInterface。。层次结构如下:COL extends platform//platform implements interface您能为问题添加一个main吗?我看不出有什么问题,所以我想我不明白你的问题。如果WAP和COL扩展了平台,您可以拥有一组平台对象并调用obj.matchFoundinput。。。类似于平台[]平台={new WAP,new COL};对于每个平台:platforms{platform.matchFoundsome string}实际上,COL是platform的一个子类!为什么不发布类和接口声明呢