集合框架-泛型-泛型類
阿新 • • 發佈:2021-10-24
1 package cn.itcast.p4.generic.definedemo; 2 3 import cn.itcast.p2.bean.Student; 4 import cn.itcast.p2.bean.Worker; 5 6 public class GenericDefineDemo3 { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 Tool<Student> tool = new Tool<Student>();GenericDefineDemo311 12 tool.setObject(new Student()); 13 // tool.setObject(new Worker());//編譯時就發現型別不同 14 Student stu = (Student)tool.getObject(); 15 // Tool tool = new Tool(); 16 // tool.setObject(new Worker()); 17 // Student stu = (Student)tool.getObject();18 } 19 20 }
1 package cn.itcast.p4.generic.definedemo; 2 3 import cn.itcast.p2.bean.Object; 4 //定義一個工具類操作所有物件 5 /* 6 * public class Tool { private Object object; 7 * 8 * public Object getObject() { return object; } 9 * 10 * public void setObject(Object object) { this.object = object; }Tool泛型類11 * 12 * } 13 */ 14 //在jdk1.5後,使用泛型來接收類中要操作的引用資料型別。 15 //泛型類。什麼時候用?當類中的操作的引用資料型別不確定的時候,就使用泛型來表示。 16 public class Tool<QQ>{ 17 private QQ q; 18 19 public QQ getObject() { 20 return q; 21 } 22 23 public void setObject(QQ object) { 24 this.q = object; 25 } 26 27 }
1 package cn.itcast.p2.bean; 2 3 import java.util.Comparator; 4 5 public class Person implements Comparable<Person>{ 6 private String name; 7 private int age; 8 9 10 public Person() { 11 super(); 12 // TODO Auto-generated constructor stub 13 } 14 15 public Person(String name, int age) { 16 super(); 17 this.name = name; 18 this.age = age; 19 } 20 public int compareTo(Person p) {//指定比較型別,傳入比較型別 21 // Person p= (Person)obj; 22 int temp = this.age-p.age; 23 return temp==0?this.name.compareTo(p.name):temp; 24 } 25 26 27 // @Override 28 // public boolean equals(Object obj) {//不能把Object改為Person,equals方法來自Object,Object沒有定義泛型 29 // // TODO Auto-generated method stub 30 // if (this == obj) { 31 // return true; 32 // } 33 // if (!(obj instanceof Person)) { 34 // throw new RuntimeException() 35 // } 36 // Person p = (Person)obj; 37 // return super.equals(obj); 38 // } 39 /* 40 * @Override public int hashCode() { final int prime = 31; int result = 1; 41 * result = prime * result + age; result = prime * result + ((name == null) ? 0 42 * : name.hashCode()); return result; } 43 * 44 * @Override public boolean equals(Object obj) { if (this == obj) return true; 45 * if (obj == null) return false; if (getClass() != obj.getClass()) return 46 * false; Person other = (Person) obj; if (age != other.age) return false; if 47 * (name == null) { if (other.name != null) return false; } else if 48 * (!name.equals(other.name)) return false; return true; }//alt+shift+s 49 * hashCode和equals 50 */ 51 public String getName() { 52 return name; 53 } 54 public void setName(String name) { 55 this.name = name; 56 } 57 public int getAge() { 58 return age; 59 } 60 public void setAge(int age) { 61 this.age = age; 62 } 63 64 }Person
1 package cn.itcast.p2.bean; 2 3 public class Student extends Person { 4 5 public Student() { 6 super(); 7 // TODO Auto-generated constructor stub 8 } 9 10 public Student(String name, int age) { 11 super(name, age); 12 // TODO Auto-generated constructor stub 13 } 14 15 }Student
1 package cn.itcast.p2.bean; 2 3 public class Worker extends Person { 4 5 public Worker() { 6 super(); 7 // TODO Auto-generated constructor stub 8 } 9 10 public Worker(String name, int age) { 11 super(name, age); 12 // TODO Auto-generated constructor stub 13 } 14 15 }Worker