[英]How to Add an Object Inside Another List Object
I have an object class A, and a list object for class B 我有一个对象类A,以及一个类B的列表对象
A a = a.findById(aId);
List<B> b = b.findById(bId);
Class B
has A
class B
班有A
班
public class B {
private int id;
private A a;
...getters setters
}
Is it possible to add object a
in object list b
? 是否可以添加对象
a
在对象列表b
?
I get error when I try to add a
in object list. 我得到的错误,当我尝试添加
a
的对象列表。 b.add(a);
This tells me to change object a
as B a...
这告诉我将对象
a
更改为B a...
How can I add object a
in the list object b
? 如何添加对象
a
列表中的对象b
?
You can add object a
if you wrap it in object B
. 如果将对象
a
包装在对象B
则可以添加它。 Otherwise, you will not be able to add object a
of type A
to List<B>
of B
objects because of the type mismatch: you are trying to add object of type A
to the list of B
objects which is not allowed in Java. 否则,由于类型不匹配,您将无法将类型
A
对象a
添加到B
对象的List<B>
:您正在尝试将类型A
对象添加到B
对象的列表中,这在Java中是不允许的。
Is it possible to add object
a
in object listb
?是否可以添加对象
a
在对象列表b
?
Simply, no because they're not the same types. 简而言之,不,因为它们不是相同的类型。
How can I add object a in the list object b?
如何在列表对象b中添加对象a?
in order to add both instances of class A
and class B
into one collection, one approach you can take is to make an interface in which both class A
and class B
implement. 为了将
A
类和B
类A
两个实例都添加到一个集合中,可以采用的一种方法是创建一个同时实现A
类和B
类的接口。
Example: 例:
public interface ISomeInterface {
// define some methods in which both class `A` and `B` share
}
Then make the classes implement the ISomeInterface
interface: 然后使这些类实现
ISomeInterface
接口:
public class A implements ISomeInterface {
}
public class B implements ISomeInterface {
private int id;
private A a;
}
then you can do something like this: 那么您可以执行以下操作:
List<ISomeInterface> b = new ArrayList<>();
b.add(new A());
b.add(new B());
// note you can only call methods through which the interface defines
// another solution is to cast the types, that way you can invoke the methods that belong to each type
// make use of instanceof keyword to prevent exception
or simply without the need of making interfaces: 或根本不需要建立接口:
List<Object> b = new ArrayList<>();
b.add(new A()); //if you want to call the methods of A or B you need to cast it first, remember to use instanceof keyword prior to casting
b.add(new B());
The syntax to add a A to B would be first get B, then add A to B, such as 将A添加到B的语法将是首先获取B,然后将A添加到B,例如
A a = new A();
B b = new B();
b.add(A);
if you wish to add both to the same List<>
then they need to implement the same or inherit from the same class, example interface IAB
如果您希望将它们都添加到相同的
List<>
则它们需要实现相同或从相同的类继承,例如接口IAB
List<IAB> list = new ArrayList<>();
list.add(new A());
list.add(new B());
given that 鉴于
public interface IAB{}
public class A implements IAB{}
public class B implements IAB();
note that in this case you can only call the methods that are available on the Interface unless you cast it to the class (but if you mix A and B that might be complicated to handle) 请注意,在这种情况下,除非将其强制转换为类,否则只能调用接口上可用的方法(但是,如果将A和B混合使用,可能很难处理)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.