How would I overload method in an interface?

前端 未结 4 967
一个人的身影
一个人的身影 2021-02-09 00:30

if I have this interface

public interface someInterface {
  // method 1
  public String getValue(String arg1);
  // method 2
  public String getValue(String arg1         


        
4条回答
  •  半阙折子戏
    2021-02-09 00:51

    If the second value can be considered optional in a sense and you always have the 2 arguments when calling you could create a wrapper class which implements the 2 parameter interface passing the 1 parameter implementation as a constructor parameter and calling that in the method, e.g. something like this:

    interface A{
      method1(P1)
    }
    
    interface B{
      method2(P1, P2)
    }
    
    class Wrap implements B{
      Wrap(A impl)
    
      override method2(P1, P2){
        call impl.method1(P1)
      }
    
    }
    

提交回复
热议问题