JAVA-返回接口的列表

JAVA-返回接口的列表,java,list,interface,implements,Java,List,Interface,Implements,我有这个问题,我只想在if块之后返回接口和实现类的列表 public interface Lotto { } public class LottoImplSecond implements Lotto { } public class LottoImplFirst implements Lotto { } public class MyClass { public List<Lotto> getLotto(Integer number){ if(number=

我有这个问题,我只想在
if
块之后返回接口和实现类的列表

public interface Lotto { }

public class LottoImplSecond implements Lotto { }
public class LottoImplFirst implements Lotto { }

public class MyClass {
   public List<Lotto> getLotto(Integer number){
       if(number==1) List<Lotto> listaLotto=new ArrayList<LottoImplFirst>();
       else if(number==2) List<Lotto> listaLotto=new ArrayList<LottoImplSecond>();
   return listaLotto;
}
公共接口乐透{}
公共类LottoImplSecond实现Lotto{}
公共类LottoImplFirst实现Lotto{}
公共类MyClass{
公共列表getLotto(整数){
if(number==1)List listaLotto=new ArrayList();
如果(number==2)List listaloto=new ArrayList(),则为else;
返回Listaloto;
}
公共接口乐透{}
公共类LottoImplSecond实现Lotto{}
公共类LottoImplFirst实现Lotto{}
公共类MyClass{
public List如果返回
List
,则内部是否使用特定类型并不重要,实际上,您不能这样做,因为
ArrayList
不是
List
的子类。您实际上可以做:

public List<Lotto> getLotto(Integer number){
   return new ArrayList<Lotto>();
}
公共列表getLotto(整数){
返回新的ArrayList();
}

(还要注意,您在
if-else
语句中声明了变量(
listaLotto
),因此返回语句超出了它们的范围。

条件在if和else中都是相同的。泛型继承不同于Java继承

 public List<Lotto> getLotto(Integer number)
{
    return ((List<Lotto>)(number == 1 ? new ArrayList<LottoImplFirst>() : new ArrayList<LottoImplSecond>());
}
公共列表getLotto(整数)
{
return((List)(number==1?new ArrayList():new ArrayList());
}

您的代码中有许多错误,但我认为您要做的是将
getLotto
方法签名更改为:

public List<? extends Lotto> getLotto(Integer number);
你的台词

if(number==1) List<Lotto> listaLotto=new ArrayList<LottoImplFirst>();
else if(numeber==1) List<Lotto> listaLotto=new ArrayList<LottoImplSecond>();
if(number==1)List listaLotto=new ArrayList();
如果(numeber==1)List listaLotto=new ArrayList(),则为else;
就调用您的方法的代码而言,您什么也做不到。对于调用方来说,这两个列表是绝对无法区分的。我建议您只编写

listaLotto = new ArrayList<Lotto>();
listaLotto=newarraylist();
相应地调整返回类型并使用它


旁注:您的代码不是可编译的Java,但这是一个次要问题。

试试这个。我刚刚编译了它:

class MyClass {
   public List<? extends Lotto> getLotto(Integer number){

       List<? extends Lotto> listaLotto = null; 

       if(number==1) {
           listaLotto= new ArrayList<LottoImplFirst>();
       }           
       else if(number ==1) {
           listaLotto=new ArrayList<LottoImplSecond>();
       }
   return listaLotto;
}
}
class-MyClass{

public ListFirst
List
不是
List
List
的超类型,因此您需要返回一个
List实际上Java甚至不允许这种语句。@MarkoTopolnik-哪种语句是不允许的?从您的注释中看不清楚。您在结束语中引用的变量声明语句请注意,通配符类型是由调用站点解决的,而不是被调用方解决的。这使得整个结构完全无效,即使编译。调用方只需要处理<代码> >代码>,得到零收益。因此,我会考虑OP的这个坏建议。事实上可能有一个小的好处。您的签名(但前提是OP需要):编译器将不允许调用任何涉及泛型类型参数的
List
方法,例如
add
。问题是:如果调用方不知道他得到的是哪一个,为什么要在一种情况下返回
ArrayList
,在另一种情况下返回
ArrayList
。您确定不应该这样做吗st在这两种情况下都返回一个
new ArrayList()
?@herman因为每次乐透(第一次和第二次)具有不同的mysql db路径。相反,行为是same@AmitD如果这是OP的真实方法,没有进一步的代码,那么你是绝对正确的。然而,由于该示例中的编译器错误,在我看来,为了这个问题,它像是简化的代码。
listaLotto = new ArrayList<Lotto>();
class MyClass {
   public List<? extends Lotto> getLotto(Integer number){

       List<? extends Lotto> listaLotto = null; 

       if(number==1) {
           listaLotto= new ArrayList<LottoImplFirst>();
       }           
       else if(number ==1) {
           listaLotto=new ArrayList<LottoImplSecond>();
       }
   return listaLotto;
}
}