阅读以下程序,根据需求在空缺位置补全代码
需求 1:print 方法中的参数只能是 List 集合,而 List 集合中只能存储 Person 的子类对象
填补位置 1:
填补位置 2:
需求 2:print 方法中的参数可以是 List 集合也可以是 Set 集合,而集合中存储的对象却只能是 Student 或
Person 对象
[[Java] 纯文本查看 复制代码
class Person4{
private String name;
private int age;
public Person4(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person4() {
super();
// TODO Auto-generated constructor stub
}
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 "Person4 [name=" + name + ", age=" + age + "]";
}
}
class Student2 extends Person4{
public Student2(String name,int age){
super(name,age);
}
@Override
public String toString() {
return "Student2 [getName()=" + getName() + ", getAge()=" + getAge()
+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
+ ", toString()=" + super.toString() + "]";
}
}
class Worker extends Person4{
public Worker(String name,int age){
super(name,age);
}
@Override
public String toString() {
return "Worker [getName()=" + getName() + ", getAge()=" + getAge()
+ ", toString()=" + super.toString() + ", getClass()="
+ getClass() + ", hashCode()=" + hashCode() + "]";
}
public class Test47 {
public static void print(填补位置1<填补位置2> c){
System.out.println(c);
}
}
填补位置 1:
填补位置 2: |