Java中的重写

Java中的重写,java,overriding,Java,Overriding,有没有一种方法可以重写一个Java方法,而唯一改变的是ArrayList 例如: public void quickSortMethod(ArrayList<Integer> comparison, int start, int end) {} public void quickSortMethod(ArrayList<Double> comparison, int start, int end) {} 公共void quickSortMeth

有没有一种方法可以重写一个Java方法,而唯一改变的是ArrayList

例如:

public void quickSortMethod(ArrayList<Integer> comparison, int start, int end)   
{}   

public void quickSortMethod(ArrayList<Double> comparison, int start, int end)   
{}   
公共void quickSortMethod(ArrayList比较、int开始、int结束)
{}   
公共void quickSortMethod(ArrayList比较、int开始、int结束)
{}   
这当前给了我以下错误:

Method quickSortMethod(ArrayList<Integer>, int, int) has the same erasure quickSortMethod(ArrayList<E>, int, int) as another method in type QuickSort   
方法quickSortMethod(ArrayList,int,int)与类型QuickSort中的另一个方法具有相同的擦除quickSortMethod(ArrayList,int,int)
顺便说一句,我正在使用我在ArrayList中创建的其他对象作为参数

ArrayList<MyObject>...    
ArrayList。。。

错误告诉您答案:您不能执行您的建议


不幸的是,没有。因为经过编译,两者之间没有区别

在这种情况下,您可以尝试将方法本身设置为泛型,并将该类型用作ArrayList类型

public <T> void quickSortMethod(ArrayList<T> comparison, int start, int end)
公共void quickSortMethod(ArrayList比较、int开始、int结束)

您可以使用

ArrayList<Object>,int,int
ArrayList,int,int

然后根据arraylist的对象类型使用不同的分析

因为您将使用这些数组列表作为参数对象,所以您可以定义自己的自定义对象,这些对象继承自arraylist。这样,编译器将识别您的重载具有不同的签名

当然,这意味着任何客户端代码都需要知道要传递哪个子类型。但这应该是一个相对容易解决的问题,因为如果您知道您正在输入参数对象int,那么就初始化integerParamList。双打也是如此

如果您对代码的其余部分没有更多的了解,这可能是一个合适的解决方案,也可能不是,但它可能很接近?(我无法对此进行测试,因此可能存在未知问题…)

公共类mySortingClass{
公共void quickSortMethod(IntegerParamList比较、int开始、int结束)
{}    
public void quickSortMethod(双参数列表比较、int start、int end)
{} 
}
公共类IntegerParamList扩展了ArrayList
{}
公共类DoubleParameterList扩展了ArrayList
{}

这是一个有效的重写示例,下面也是 我们可以重写静态方法吗? 答案是否定的。

下面是示例代码

  package com.sample.test;
类动物{

public static void showMessage() {
    System.out.println("we are in Animal class");
}
public void DogShow() {
    System.out.println("we are in Dog show class");
}

public static void showMessage() {
    System.out.println("we are in static overridden method of dog class");
}
}

狗类动物{

public static void showMessage() {
    System.out.println("we are in Animal class");
}
public void DogShow() {
    System.out.println("we are in Dog show class");
}

public static void showMessage() {
    System.out.println("we are in static overridden method of dog class");
}
}

猫科动物{

public static void showMessage(){
    System.out.println("we are in overridden cat class");
}
}

公营动物测试{

public static void main(String[] args) {
    Animal animal = new Animal();
    animal.showMessage();

    Dog dog = new Dog();
    dog.showMessage();

    Animal animal2 = new Dog();
    animal2.showMessage();

    Animal animal3 = new Cat();
    animal3.showMessage();

}
}

输出:

我们在动物课 我们在dog类的静态重写方法中 我们在动物课
我们在动物类中

我是否必须创建两个方法(使用不同的名称),它们中唯一改变的是参数?有什么建议吗?你可以走那条路,是的。你在这个方法中用这个
ArrayList
做什么?如果我有ArrayList,MyObject有很多变量,其中一个是数字(double)。我想根据该变量对ArrayList进行排序。问题是:我有MyObject2和MyObject3,它们不同于MyObject2,但我也想对它们进行排序。一个想法是让所有对象实现
可比较的
接口,然后将其设置为
ArrayList
。在每个类中,您都实现了
compareTo()
方法,该方法允许您根据所需的条件对每个类进行排序。