Java中泛型的使用

来源:互联网 发布:设计师个人主页源码 编辑:程序博客网 时间:2024/06/04 22:47

Java中静态方法的泛型使用

/*class Demo<T>{public void show(T t){System.out.println("show:"+t);}public void print(T t){System.out.println("show:"+t);}}*///泛型类定义的泛型,在整个类中有效。如果被方法使用,//那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了。////为了让不同方法可以操作不同类型,而且类型还不确定。//那么可以将泛型定义在方法上。class Demo<T>{public  void show(T t){System.out.println("show:"+t);}public <Q> void print(Q q){System.out.println("print:"+q);}//该方法不可用 因为静态方法会在类被创建前运行,而类只有在建立对象时才能被创建,所以无法确定T//public static void method(T t)//{//System.out.println("method:"+t);//}/*特殊之处:静态方法不可以访问类上定义的泛型。如果静态方法操作的应用数据类型不确定,可以将泛型定义在方法上。*/public  static <W> void method(W w){System.out.println("method:"+w);}}class GenericDemo4 {public static void main(String[] args) {Demo <String> d = new Demo<String>();d.show("haha");//d.show(4);d.print(5);d.print("hehe");Demo.method("hahahahha");/*Demo d = new Demo();d.show("haha");d.show(new Integer(4));d.print("heihei");*//*Demo<Integer> d = new Demo<Integer>();d.show(new Integer(4));d.print("hah");Demo<String> d1 = new Demo<String>();d1.print("haha");d1.show(5);*/}}


Java中泛型限定

import java.util.*;/*? 通配符。也可以理解为占位符。泛型的限定;? extends E: 可以接收E类型或者E的子类型。上限。? super E: 可以接收E类型或者E的父类型。下限*/class  GenericDemo6{public static void main(String[] args) {/*ArrayList<String> al = new ArrayList<String>();al.add("abc1");al.add("abc2");al.add("abc3");ArrayList<Integer> al1 = new ArrayList<Integer>();al1.add(4);al1.add(7);al1.add(1);printColl(al);printColl(al1);*/ArrayList<Person> al = new ArrayList<Person>();al.add(new Person("abc1"));al.add(new Person("abc2"));al.add(new Person("abc3"));//printColl(al);ArrayList<Student> al1 = new ArrayList<Student>();al1.add(new Student("abc--1"));al1.add(new Student("abc--2"));al1.add(new Student("abc--3"));printColl(al1);  //ArrayList<? extends Person> al = new ArrayList<Student>();error}public static void printColl(Collection<? extends Person> al){Iterator<? extends Person> it = al.iterator();while(it.hasNext()){System.out.println(it.next().getName());}}/*public static void printColl(ArrayList<?> al)//ArrayList al = new ArrayList<Integer>();error{Iterator<?> it = al.iterator();while(it.hasNext()){System.out.println(it.next().toString());}}*/}class Person{private String name;Person(String name){this.name = name;}public String getName(){return name;}}class Student extends Person{Student(String name){super(name);}}/*class Student implements Comparable<Person>//<? super E>{public int compareTo(Person s){this.getName()}}*/class Comp implements Comparator<Person>{public int compare(Person s1,Person s2){//Person s1 = new Student("abc1");return s1.getName().compareTo(s2.getName());}}TreeSet<Student> ts = new TreeSet<Student>(new Comp());ts.add(new Student("abc1"));ts.add(new Student("abc2"));ts.add(new Student("abc3"));

又一个泛型限定的例子(<? super E>):

import java.util.*;class GenericDemo7 {public static void main(String[] args) {TreeSet<Student> ts = new TreeSet<Student>(new Comp());ts.add(new Student("abc03"));ts.add(new Student("abc02"));ts.add(new Student("abc06"));ts.add(new Student("abc01"));Iterator<Student> it = ts.iterator();while(it.hasNext()){System.out.println(it.next().getName());}/**/TreeSet<Worker> ts1 = new TreeSet<Worker>(new Comp());ts1.add(new Worker("wabc--03"));ts1.add(new Worker("wabc--02"));ts1.add(new Worker("wabc--06"));ts1.add(new Worker("wabc--01"));Iterator<Worker> it1 = ts1.iterator();while(it1.hasNext()){System.out.println(it1.next().getName());}}}/*class StuComp implements Comparator<Student>{public int compare(Student s1,Student s2){return s1.getName().compareTo(s2.getName());}}class WorkerComp implements Comparator<Worker>{public int compare(Worker s1,Worker s2){return s1.getName().compareTo(s2.getName());}}*/class Comp implements Comparator<Person>{public int compare(Person p1,Person p2){return p2.getName().compareTo(p1.getName());}}class Person{private String name;Person(String name){this.name = name;}public String getName(){return name;}public String toString(){return "person :"+name;}}class Student extends Person{Student(String name){super(name);}}class Worker extends Person{Worker(String name){super(name);}}







0 0
原创粉丝点击