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

时间:2022-09-10 23:23:11

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). But when I call worker.setList(cats) it alerts about expecting an Orderable, but getting a Cat... but Cat implements Orderable. How do I solve this?

目标是将此列表传输到工作对象,因此我可以在主过程中向列表中添加一些新的cat,并且工作人员将在不再设置其list属性的情况下知道它(list是主proc和in中的相同对象工人)。但是,当我调用worker.setList(cats)时,它会发出关于期望Orderable但是获得Cat ...的警报,但Cat实现Orderable。我该如何解决这个问题?

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
}

4 个解决方案

#1


39  

You need to change the Worker class so that it accepts List<? extends Orderable>

您需要更改Worker类,以便它接受List

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

#2


7  

It should work if you just change the declaration of cats:

如果你只是改变猫的声明它应该工作:

List<? extends Orderable> cats = new BasicEventList<? extends Orderable>();

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

Worker worker = new Worker(); 
worker.setList(cats);

See:

#3


4  

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

#4


1  

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

注意:这是我很快猜到的

#1


39  

You need to change the Worker class so that it accepts List<? extends Orderable>

您需要更改Worker类,以便它接受List

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

#2


7  

It should work if you just change the declaration of cats:

如果你只是改变猫的声明它应该工作:

List<? extends Orderable> cats = new BasicEventList<? extends Orderable>();

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

Worker worker = new Worker(); 
worker.setList(cats);

See:

#3


4  

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

#4


1  

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

注意:这是我很快猜到的