其他分享
首页 > 其他分享> > 集合框架-泛型-泛型限定(上限的体现)

集合框架-泛型-泛型限定(上限的体现)

作者:互联网

 1 package cn.itcast.p5.generic.advance.demo;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collection;
 5 import java.util.Iterator;
 6 
 7 import com.sun.crypto.provider.PBEKeyFactory.PBEWithSHA1AndRC2_40;
 8 
 9 import cn.itcast.p2.bean.Person;
10 import cn.itcast.p2.bean.Student;
11 import cn.itcast.p2.bean.Worker;
12 
13 public class GenericAdvanceDemo4 {
14 
15     public static void main(String[] args) {
16         // TODO Auto-generated method stub
17         ArrayList<Person> al1 = new ArrayList<Person>();
18         
19         al1.add(new Person("abc",30));
20         al1.add(new Person("abc4",34));
21         
22         ArrayList<Student> al2 = new ArrayList<Student>();
23         al2.add(new Student("stu1",11));
24         al2.add(new Student("stu2",22));
25         
26         ArrayList<Worker> al3 = new ArrayList<Worker>();
27         al3.add(new Worker("stu1",11));
28         al3.add(new Worker("stu2",22));
29         
30         ArrayList<String>  al4 = new ArrayList <String>();
31         al4.add("abcsaasd");
32         
33 //        al1.addAll(al4);//类型不匹配 取出来类型肯定出错,不知道是转成Person还是字符串,类型会有安全隐患
34 //        al1.add(new Person("xixi",20));
35         
36 
37         al1.addAll(al2);
38         System.out.println(al1.size());
39 //        printCollection(al3);
40     
41     }
42 
43 
44 }
45 /*
46  * 一般在存储元素的时候都是用上限,因为这样取出都是按照上限的类型来运算的,不会出现类型安全隐患。
47  */
48 class MyCollection<E>{
49     public void add(E e) {
50         
51     }
52     public void addAll(MyCollection<? extends E> e) {//解释这段,存元素通常用上限。
53         
54     }
55 }
GenericAdvanceDemo4
 1 package cn.itcast.p2.bean;
 2 
 3 public class Person implements Comparable<Person>{
 4     private String name;
 5     private int age;
 6     
 7     
 8     public Person() {
 9         super();
10         // TODO Auto-generated constructor stub
11     }
12     
13     public Person(String name, int age) {
14         super();
15         this.name = name;
16         this.age = age;
17     }
18     public int compareTo(Person p) {//指定比较类型,传入比较类型
19 //        Person p= (Person)obj;
20         int temp = this.age-p.age;
21         return temp==0?this.name.compareTo(p.name):temp;
22     }
23     
24     
25 //    @Override
26 //    public boolean equals(Object obj) {//不能把Object改为Person,equals方法来自Object,Object没有定义泛型
27 //        // TODO Auto-generated method stub
28 //        if (this == obj) {
29 //            return true;
30 //        }
31 //        if (!(obj instanceof Person)) {
32 //            throw new RuntimeException()
33 //        }
34 //        Person p = (Person)obj;
35 //        return super.equals(obj);
36 //    }
37     /*
38      * @Override public int hashCode() { final int prime = 31; int result = 1;
39      * result = prime * result + age; result = prime * result + ((name == null) ? 0
40      * : name.hashCode()); return result; }
41      * 
42      * @Override public boolean equals(Object obj) { if (this == obj) return true;
43      * if (obj == null) return false; if (getClass() != obj.getClass()) return
44      * false; Person other = (Person) obj; if (age != other.age) return false; if
45      * (name == null) { if (other.name != null) return false; } else if
46      * (!name.equals(other.name)) return false; return true; }//alt+shift+s
47      * hashCode和equals
48      */
49     public String getName() {
50         return name;
51     }
52     public void setName(String name) {
53         this.name = name;
54     }
55     public int getAge() {
56         return age;
57     }
58     public void setAge(int age) {
59         this.age = age;
60     }
61     
62 }
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     @Override
16     public String toString() {
17         // TODO Auto-generated method stub
18         return "Student:"+getName()+":"+getAge();
19     }
20     
21 }
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     @Override
16     public String toString() {
17         // TODO Auto-generated method stub
18         return "Worker:"+getName()+":"+getAge();
19     }
20     
21     
22 }
Worker

 

标签:return,name,Person,限定,age,集合,泛型,new,public
来源: https://www.cnblogs.com/doremi429/p/15451947.html