import java.util.*;
public class CollectionTest1{
Collection c1 = new ArrayList();
Collection c2 = new HashSet();
public CollectionTest1(){
Student s = new Student("李四",25000);
c1.add(s);
c1.add("张三");
c2.add("王武");
c2.add(new Student("赵柳",30000));
printCollection(c1);
printCollection(c2);
}
public void printCollection(Collection d){
System.out.println("-----------------");
Iterator it = d.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void main(String args[]){
new CollectionTest1();
}
}
class Student {
public Student(String a,int b) {
this.name = a;
this.slary =b;
}
public String name;
public int slary;
public String toString(){
String s="Name "+name+"\n"+"GongZI "+slary;
return s;
}
}
Tags -
课本习题
文章来源:
http://www.tt-shopping.com/kevinlau/read.php/108.htm