[英]How to add different class objects into ArrayList
I am trying to add 2 class objects in which they are an extension of another class into an ArrayList.我正在尝试将 2 个类对象添加到 ArrayList 中,其中它们是另一个类的扩展。 The codes are as follows:
代码如下:
Student class学生班
public class Student
{
private String firstName;
private String lastName;
public Student(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String getName()
{
return firstName + " " + lastName;
}
}
UndergradStudent class本科生班
public class UndergradStudent extends Student
{
private int examMark;
public UndergradStudent(String firstName, String lastName,
int examMark)
{
// parameters for Student class
super(firstName, lastName);
this.examMark = examMark;
}
}
PostgradStudent class研究生班
public class PostgradStudent extends Student
{
private int thesisMark;
public PostgradStudent(String firstName, String lastName,
int thesisMark)
{
// parameters for Student class
super(firstName, lastName);
this.thesisMark = thesisMark;
}
}
While I am able to add them into 2 different ArrayList
, is it possible to combine them into 1 ArrayList
?虽然我可以将它们添加到 2 个不同的
ArrayList
,但是否可以将它们组合成 1 个ArrayList
? If so, how should I define the type for it?如果是这样,我应该如何定义它的类型?
The reason I had wanted to have 1 ArrayList
is to minimize the amount of duplicated code lines.我想要 1 个
ArrayList
的原因是为了尽量减少重复代码行的数量。 Eg.例如。 in my Main class below, instead of writing the print statement twice, I can write once instead.
在下面的 Main 类中,我可以编写一次而不是编写两次打印语句。 And I will also be having more methods that access either of the 2 class objects and printing out the same statements...
而且我还将拥有更多方法来访问两个类对象中的任何一个并打印出相同的语句......
public class Main
{
public static void main(String[] args)
{
UndergradStudent ugs = new UndergradStudent("Day", "Parker", 65);
PostgradStudent pgs = new PostgradStudent("Pete", "Loomis", 78);
ArrayList<UndergradStudent> ugsArr = new ArrayList<UndergradStudent>();
ArrayList<PostgradStudent> pgsArr = new ArrayList<PostgradStudent>();
ugsArr.add(ugs);
pgsArr.add(pgs);
// For UndergradStudent
for (int i=0; i< ugsArr.size(); i++)
{
System.out.println("ugs: " + ugsArr.get(i).getName());
}
for (int j=0; i< pgsArr.size(); j++)
{
System.out.println("pgs: " + pgsArr.get(j).getName());
}
}
}
Also I tried creating an ArrayList as ArrayList allStudents = new ArrayList();
我也尝试创建一个 ArrayList 作为
ArrayList allStudents = new ArrayList();
, and I will get an error for the print statement as it cannot find getName()
.. ,并且我将收到打印语句的错误,因为它找不到
getName()
..
Appreciate for any advices.感谢任何建议。
Yes: Use the abstract type:是:使用抽象类型:
List<Student> allStudents = new ArrayList<>();
One of the fundamentals of OOP is the Liskov substitution principle , which is basically that all types may safely be identified and dealt with using their abstract type. OOP的基础之一是Liskov 替换原则,它基本上是所有类型都可以使用其抽象类型安全地识别和处理。
Indeed, using the type List<Student>
(as I have) instead of ArrayList<Student>
is another example, and best practice.事实上,使用
List<Student>
类型(就像我一样)而不是ArrayList<Student>
是另一个例子,也是最佳实践。
You can create a list of parent class as below.您可以创建一个父类列表,如下所示。 Which will allow you to add both child elements to single list.
这将允许您将两个子元素添加到单个列表中。 As below:
如下:
List<Student> students = new ArrayList<>();
You can create reference of ArrayList
as well, but it's always preferable to go with List
.您也可以创建
ArrayList
引用,但最好使用List
。
由于UndergradStudent 和PostgradStudent 是从Student 类继承的,因此可以使用student 类来引用它们,因此您只需使用Student 对象类型的ArrayList 即可接受Student、UndergradStudent、PostgradStudent 的所有条目。
List<Student> allStudentList = new ArrayList<>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.