简体   繁体   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?

For example 例如

//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....
}

How would you make an abstract method that allows you to change the method signature like compareTo does? 您将如何制作一个抽象方法,使您可以像compareTo一样更改方法签名?

Use parameterized type. 使用参数化类型。

Parent interface : 父界面:

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

Child class : 儿童班:

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

Comparable is using generics; Comparable是使用泛型。 you can, too: 您也可以:

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

Your code would do this: 您的代码将执行以下操作:

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

暂无
暂无

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

相关问题 如何使用Byte-Buddy从界面构建Java类的具体实现? - How do I build an concrete implementation of a Java Class from an interface using Byte-Buddy? 如何将实现的接口对象传递给其实现的类中的方法? - How do I pass an implemented Interface object to a method in the class its implemented in? 从类调用实现的接口覆盖方法 - Call implemented interface overriden method from a class 如何从Java类中的另一个方法调用我的方法? - How do I call my method from another method I have within my class in Java? 如何将onClickLIstener添加到我的代码中以执行已经实现的方法 - How do I add an onClickLIstener to my code to carry out a method I already have implemented 如何在接口中参数化然后在实现的类中指定泛型 - How do I parameterise in an interface and then specify generic type in implemented class 如何找到在给定类中实现其方法的Java接口? - How do I find the Java interface whose method is implemented in a given class? 如何找到我的类用作某个实现接口的用法? - How do I find usages of my class where it is used as a certain implemented interface? 当接口方法的泛型返回使用 Java 中的具体类型实现时,如何避免“未检查覆盖”警告? - How can I avoid an "Unchecked overriding" warning when interface method's generic return is implemented with a concrete type in Java? 如何将 HashMap 转换为具体类? - How do I cast a HashMap to a concrete class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM