【问题标题】:Instantiate a class that extends an abstract class that implements an Interface实例化一个扩展实现接口的抽象类的类
【发布时间】:2014-06-26 13:07:00
【问题描述】:

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

public class CatalogueOfLife extends EDITPlatform {

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

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

然后是一个名为interface的接口,它具有这个功能:

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 对象,但问题是我需要不止一个类来扩展平台。

【问题讨论】:

  • 其实我不明白问题出在哪里...为什么只能使用COL 对象?为什么你需要有几个类扩展 Platform ?你想做COL col = new Platform();之类的事情,然后就可以做col.matchfound(aString)吗?
  • 因为我实际上将拥有除 COL 之外的其他类来扩展抽象类。例如,我将有WAPCOL 都扩展platform.. 并且每个都会向platform 函数发送不同的nomatchfound 字符串.. 我需要从平台中调用该函数main,而不必明确指定调用它的子类。
  • 我严重怀疑你的接口被称为interface,因为这是一个Java保留关键字。请张贴真实的类和接口声明,而不是用英文描述它们;这也告诉我们你的类层次结构是什么样的。
  • 接口被称为“WSInterface”..层次结构如下:COL extends platform //platform implements interface
  • 你能添加一个主要的问题吗?我没有看到问题,所以我想我不明白你的问题。如果 WAP 和 COL 扩展了 Platform,您可以拥有 Platform 对象的集合并调用 obj.matchFound(input)... 类似于 Platform[] platforms= {new WAP(),new COL()};对于每个(平台:平台){platform.matchFound("some string")}

标签: java inheritance abstract-class


【解决方案1】:

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

如果您需要在CatalogueOfLife 中定义该方法,那么您应该使用抽象方法:

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);

【讨论】:

  • 其实是Platform的子类COL!
  • 你为什么不发布你的类和接口声明
【解决方案2】:

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

if (noMatchString.contains(getNoResultsMessage()))

getNoResultsMessage() 无法在平台中访问。

而且既然你已经在Platform中实现了接口的所有方法,就不必是abstract

这是你要找的:

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"));
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 2010-10-21
    • 2014-08-18
    相关资源
    最近更新 更多