在这段代码中。子类继承了父类,如何打印子类和父类中各自的全部内容?
package com.heima.my_test;
import java.util.ArrayList;
import java.util.Iterator;
class Animal{
private String name;
private int foots;
Animal(String name,int foots){
this.name = name;
this.foots = foots;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getFoots() {
return foots;
}
public void setFoots(int foots) {
this.foots = foots;
}
}
class Cat extends Animal{
Cat(String name, int foots) {
super(name, foots);
}
}
public class GenericDemoTest03 {
public static void main(String[] args){
ArrayList<Animal> al01 = new ArrayList<Animal>();
al01.add(new Animal("爬行", 4));
al01.add(new Animal("飞禽", 2));
pp_AC(al01);
ArrayList<Cat> al02 = new ArrayList<Cat>();
al02.add(new Cat("Tom", 4));
pp_AC(al02);
}
public static void pp_AC(ArrayList<? extends Animal> al){
Iterator<? extends Animal> it = al.iterator();
while(it.hasNext()){
//如何打印对象中的所有内容
System.out.println(it.next());//如何打印对象中的所有内容
}
}
} |
|