用抽象方法实现接口 [英] Implementing an Interface with an abstract method

查看:98
本文介绍了用抽象方法实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用公共类MyStringSet实现StringSet {} 来实现接口,但我不断收到错误消息,"MyStringSet不是抽象的,并且不覆盖StringSet中的抽象方法getCapacity()"..我如何能够在不出现此错误的情况下实现此界面?

I am trying to implement an interface using public class MyStringSet implements StringSet{} but I keep getting an error that "MyStringSet is not abstract and does not override abstract method getCapacity() in StringSet". How would I be able to impletment this interface without getting this error?

    public interface StringSet {
         public void resize(int larger);
         public void insert(String entry);
         public void remove(String target);                  
         public String getRandomItem ();           
         public String getFirstItem ();         
         public boolean contains(String target);                
         public boolean is_empty( );                   
         public int inventory( );                  
         public int getCapacity( );
    }

推荐答案

简短的回答:最有可能的是,您的实现并没有真正地实现".那些抽象方法.界面就像孔.该实现填补了这些漏洞,如果没有,您就必须将扩展类声明为抽象类,因为它让某些漏洞"成为可能.打开.它们仍然必须由不再是抽象的真实实现来填充.

Short answer: most likely, your implementation is not really "implementing" those abstract methods. An interface is like holes. The implementation fills those holes and if not, you have to declare your extending class as abstract, as it let some of those "holes" open. They still have to be filled by a real implementation which is not abstract anymore.

这是您的案例的简单实现:

Here is an easy implementation for your case:

public class MyStringSet implements StringSet {
  public void resize(int larger) {}
  public void insert(String entry) {}
  public void remove(String target) {}
  public String getRandomItem () {return null;}
  public String getFirstItem () {return null;}
  public boolean contains(String target) {return false;}
  public boolean is_empty( ) {return false;}
  public int inventory( ) {return 0;}
  public int getCapacity( ) {return 0;}
}

这篇关于用抽象方法实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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