java 不同list 添加元素

public class A extends AB{
String name;
int age;
public A (String name,int age){
super(name, age);
}
}
public class B extends AB{
String name;
int age;
public B(String name, int age) {
super(name, age);
}
}


public class AB {
String name;
int age;
public AB(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "AB [name=" + name + ", age=" + age + "]";
}
}

 

import java.util.ArrayList;

 

import java.util.List;
public class TestArrayList {
public static void main(String[] args) {
List<AB> list = new ArrayList<AB>();
list.add(new AB("lz", 20));
list.add(new AB("LZ", 20));
System.out.println(list.toString());
}
}

 pasting

[AB [name=lz, age=20], AB [name=LZ, age=20]]

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。