Java 我应该使用对象来避免方法重载还是方法重载更好?

Java 我应该使用对象来避免方法重载还是方法重载更好?,java,design-patterns,interface,Java,Design Patterns,Interface,我有两个接口结构 MyInterface1 MyInterface2 实现接口以保持代码效率的更好设计方法是什么?重载更好,因为您不希望有人用字符串或其他东西调用您的方法 如果您的案例中有一个数字,那么您可以使用一个通用的超类,如果您希望得到Long和Float的话。重载更好,因为您不希望有人用字符串或其他东西调用您的方法 你能做的,如果您的案例中有一个数字,则使用一个通用的超类-如果您希望得到Long和Float。用于安全代码方法重载的更好方法。用于安全代码方法重载的更好方法。第二种方法重载更

我有两个接口结构

MyInterface1 MyInterface2
实现接口以保持代码效率的更好设计方法是什么?

重载更好,因为您不希望有人用字符串或其他东西调用您的方法


如果您的案例中有一个数字,那么您可以使用一个通用的超类,如果您希望得到Long和Float的话。

重载更好,因为您不希望有人用字符串或其他东西调用您的方法


你能做的,如果您的案例中有一个数字,则使用一个通用的超类-如果您希望得到Long和Float。

用于安全代码方法重载的更好方法。

用于安全代码方法重载的更好方法。

第二种方法重载更受欢迎,因为它包含强类型的方法签名

考虑下面的代码

public class InterfaceImpl implements MyInterface2{

    public Object SUM(Object O,Object P){
        //Really what can I do here without casting?

        /* If I have to cast, I might as well define
         types in the method signature, guaranteeing
         the type of the arguments
        */

       //Lets cast anyway
       return (Integer) O + (Integer) P;
    }

    public static void main(String[] args) throws ParseException {
       System.out.println(SUM(1,2));  //Excellent Returns 3
       //Yikes, valid arguments but implementation does not handle these
       System.out.println(SUM(true,false)); //Class cast exception          
    }
}
结论

当遇到更多方法需要处理的类型时,将强制实现在执行必要的强制转换之前执行类型检查。理论上,需要对扩展对象的每个类进行类型检查,因为方法签名仅限制类型的参数。由于参数是对象,因此需要检查的类型将是无限的,这是不可能的


通过使用重载方法,可以表达方法的意图,并限制允许的类型集。这使得编写方法的实现更加容易和易于管理,因为参数将是强类型的。

第二种方法重载更为可取,因为它包含强类型的方法签名

考虑下面的代码

public class InterfaceImpl implements MyInterface2{

    public Object SUM(Object O,Object P){
        //Really what can I do here without casting?

        /* If I have to cast, I might as well define
         types in the method signature, guaranteeing
         the type of the arguments
        */

       //Lets cast anyway
       return (Integer) O + (Integer) P;
    }

    public static void main(String[] args) throws ParseException {
       System.out.println(SUM(1,2));  //Excellent Returns 3
       //Yikes, valid arguments but implementation does not handle these
       System.out.println(SUM(true,false)); //Class cast exception          
    }
}
结论

当遇到更多方法需要处理的类型时,将强制实现在执行必要的强制转换之前执行类型检查。理论上,需要对扩展对象的每个类进行类型检查,因为方法签名仅限制类型的参数。由于参数是对象,因此需要检查的类型将是无限的,这是不可能的


通过使用重载方法,可以表达方法的意图,并限制允许的类型集。这使得编写方法的实现更加容易和易于管理,因为参数将是强类型的。

正如前面提到的其他答案一样,重载更好

但我还要补充的是,您不需要4个版本,只需要2个:

public interface MyInterface2 {
  public int SUM(int O, int P);
  public double SUM(double O, double P);
}
如果使用int、double或double调用SUM,int将被升级为double,第二个方法将运行

例如,下面的代码编译并打印:

public class Test implements MyInterface2 {
  public int SUM(int o, int p) {
    System.err.println("hello");
    return o + p;
  }

  public double SUM(double o, double p) {
    System.err.println("goodbye");
    return o + p;
  }

  public static void main(String[] arg) {
    Test t = new Test();
    t.SUM(1.0, 2);
  }
}

正如前面提到的其他答案,重载更好

但我还要补充的是,您不需要4个版本,只需要2个:

public interface MyInterface2 {
  public int SUM(int O, int P);
  public double SUM(double O, double P);
}
如果使用int、double或double调用SUM,int将被升级为double,第二个方法将运行

例如,下面的代码编译并打印:

public class Test implements MyInterface2 {
  public int SUM(int o, int p) {
    System.err.println("hello");
    return o + p;
  }

  public double SUM(double o, double p) {
    System.err.println("goodbye");
    return o + p;
  }

  public static void main(String[] arg) {
    Test t = new Test();
    t.SUM(1.0, 2);
  }
}

在这种情况下,第二种选择是好的。但它因代码而异。范例

interface InterfaceFrequencyCounter
{
    int getCount(List list, String name);
}

interface AnotherInterfaceFrequencyCounter
{
    int getCount(ArrayList arrayList, String name);
    int getCount(LinkedList linkedList, String name);
    int getCount(Vector vector, String name);
}

所以,在上述给定的情况下,第二种选择并不是好的做法。第一个选项很好。

在这种情况下,第二个选项很好。但它因代码而异。范例

interface InterfaceFrequencyCounter
{
    int getCount(List list, String name);
}

interface AnotherInterfaceFrequencyCounter
{
    int getCount(ArrayList arrayList, String name);
    int getCount(LinkedList linkedList, String name);
    int getCount(Vector vector, String name);
}

所以,在上述给定的情况下,第二种选择并不是好的做法。第一个很好。

如上所述,重载更好


如果遇到AmitG描述的情况,那么应该使用接口,而不仅仅是最通用的对象类型。无论如何,您的方法几乎总是可以只处理对象的某些子集,而不是全部。在这种情况下,您需要找到一个公共接口并在方法签名中使用它,就像AmitG在他的示例中所做的那样。界面的使用清楚地显示了方法cliens的意图,它是类型安全的,并且不需要在方法内部进行强制转换。

如上所述,重载更好

如果遇到AmitG描述的情况,那么应该使用接口,而不仅仅是最通用的对象类型。无论如何,您的方法几乎总是可以只处理对象的某些子集,而不是全部。在这种情况下,您需要找到一个公共接口并在方法签名中使用它,就像AmitG在他的示例中所做的那样。界面的使用清楚地显示了您对方法cliens的意图,它是类型安全的,并且不需要在方法内部进行强制转换