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 A("lz", 20));
list.add(new B("LZ", 20));
System.out.println(list.toString());
}
}
pasting
[AB [name=lz, age=20], AB [name=LZ, age=20]]