2017-02-13 176 views
-2

重载方法必须具有相同的返回类型(或子类型)吗? (作为压倒一切的方法)重载方法必须具有相同的返回类型(或子类型)吗?

如果是,那么为什么在以下B类方法“findmax”可以重载?

public class A { 
    public Integer findmax(ArrayList<Integer> list, int start){...} 
    } 
public class B extends A { 
    public Float findmax(List<Integer> mylist, int start){...} 

while while following one did not compile? (有没有提到的规则:重载方法不能返回原始类型)

public class A { 
    public Integer findmax(ArrayList<Integer> list, int start){...} 
    } 
public class B extends A { 
    public float findmax(List<Integer> mylist, int start){...} 

回答

0

肯定的,因为重载方法不能被他们的返回类型区分。 但您可以使用子类型作为返回的类型。

+1

答案似乎是否定的 – tami

相关问题