简体   繁体   English

如何将对象列表转换为接口列表?

[英]How to convert list of objects to list of interfaces?

I have some class that works with interfaces: 我有一些适用于接口的类:

Here is the interface: 这是界面:

public interface Orderable
{
    int getOrder()
    void setOrder()
}

Here is the worker class: 这是工人阶级:

public class Worker
{
   private List<Orderable> workingList;

   public void setList(List<Orderable> value) {this.workingList=value;}

   public void changePlaces(Orderable o1,Orderable o2)
   {
     // implementation that make o1.order=o2.order and vice versa
   }
}

Here is an object that implements the interface: 这是一个实现接口的对象:

public class Cat implements Orderable
{
    private int order;

    public int getOrder()
    {
      return this.order;
    }

    public void setOrder(int value)
    {
      this.order=value;
    }

    public Cat(String name,int order)
    {
       this.name=name;
       this.order=order;
    }
}

In the main procedure I create a list of cats. 在主要程序中,我创建了一个猫列表。 I use glazed lists to dynamically update controls when the list is changed and when a control model is created with this list. 我使用glazed列表在列表更改时以及使用此列表创建控件模型时动态更新控件。

The goal is to transfer this list to a worker object, so I can add some new cat to the list in the main procedure, and the worker will know about it without setting its list property again (list is same object in main proc and in worker). 目标是将此列表传输到工作对象,因此我可以在主过程中向列表中添加一些新的cat,并且工作人员将在不重新设置其list属性的情况下知道它(list是主proc和in中的相同对象工人)。 But when I call worker.setList(cats) it alerts about expecting an Orderable, but getting a Cat... but Cat implements Orderable. 但是,当我调用worker.setList(cats)它会发出关于期望Orderable但是获得Cat ...的警报,但是Cat实现了Orderable。 How do I solve this? 我该如何解决这个问题?

Here is the main code: 这是主要代码:

void main()
{
   EventList<Cat> cats=new BasicEventList<Cat>();

   for (int i=0;i<10;i++)
   {
      Cat cat=new Cat("Maroo"+i,i);
      cats.add(cat);
   }

   Worker worker=new Worker(); 
   worker.setList(cats); // wrong!
   // and other very useful code
}

You need to change the Worker class so that it accepts List<? extends Orderable> 您需要更改Worker类,以便它接受List<? extends Orderable> List<? extends Orderable>

public class Worker
{
   private List<? extends Orderable> workingList;

   public void setList(List<? extends Orderable> value) {this.workingList=value;}

   public void changePlaces(Orderable o1,Orderable o2)
   {
     // implementation that make o1.order=o2.order and vice verca  
   }
}

If you really would like a new collection of the interface type. 如果你真的想要一个新的接口类型集合。 When for instance you don't own the methods you are calling. 例如,您不拥有您正在调用的方法。

//worker.setList(cats); 
worker.setList( new ArrayList<Orderable>(cats)); //create new collection of interface type based on the elements of the old one
void main()
{
    EventList<Orderable> cats = new BasicEventList<Orderable>();

    for (int i=0;i<10;i++)
    {
        Cat cat=new Cat("Maroo"+i,i);
        cats.add(cat);
    }

    Worker worker=new Worker(); 
    worker.setList(cats); // should be fine now!
    // and other very usefull code
}

Mostly, just construct a list of Orderables right away, since cat implements Orderable, you should be able to add a cat to the list. 大多数情况下,只需构建一个Orderables列表,因为cat实现Orderable,您应该能够将cat添加到列表中。

note: this is me quickly guessing 注意:这是我很快猜到的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM