Java – return interface list

I have this problem. I want to return a list of interfaces and implementation classes only after the if block

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;
}

Solution

public interface Lotto { }
public interface Lotto { }

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

public class MyClass {
   public List<? extends Lotto> getLotto(Integer number){
       List<? extends Lotto> listaLotto;
       if(number==1) listaLotto=new ArrayList<LottoImplFirst>();
       else if(numeber==1) listaLotto=new ArrayList<LottoImplSecond>();
       return listaLotto;
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>