繁体   English   中英

如何更改从实现到我正在使用的具体类的接口覆盖的方法的签名?

[英]How do I change the signature of a method I have overriden from my interface that I implemented onto the concrete class I'm using?

例如

//This is part of Comparable Interface:
public int compareTo(T other);//T being any class/type of parameter

//This is part of my own interface: 
public void beeper(Object what);

//This is part of my own concrete class which implements both of the above interfaces
public int compareTo(Country other)//Java allows this...
{
  //code stuffs....
}
public void beeper(String what)//This does not work...
{
  //Code stuffs....
}

您将如何制作一个抽象方法,使您可以像compareTo一样更改方法签名?

使用参数化类型。

父界面:

public interface ParentClass<T>{
  void beeper(T what);
}

儿童班:

public class ChildClass implements ParentClass<String>{
  public void beeper(String what){ 
    // your impl
  }
}

Comparable是使用泛型。 您也可以:

public interface Beepable<T> {
    void beeper(T what);
}

您的代码将执行以下操作:

public class StringBeeper implements Beepable<String> {
    public void beeper(String what) { // implement here }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM