簡體   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