基础——泛型小常识0527
基础——泛型小知识0527
package cn.mmc.day15; import java.util.*; /* 泛型:JDK1.5版本以后出现新特性。用于解决安全问题,是一个类型安全机制。 好处 1.将运行时期出现问题ClassCastException,转移到了编译时期。, 方便于程序员解决问题。让运行时问题减少,安全。, 2,避免了强制转换麻烦。 泛型格式:通过<>来定义要操作的引用数据类型。 在使用java提供的对象时,什么时候写泛型呢? 通常在集合框架中很常见, 只要见到<>就要定义泛型。 其实<> 就是用来接收类型的。 当使用集合时,将集合中要存储的数据类型作为参数传递到<>中即可。 */ public class GenericTest { /** * @param args */ public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("abc01"); al.add("abc0991"); al.add("abc014"); //al.add(4);//al.add(new Integer(4));//如果没有<String>限定则,这两句编译时不报错,会在运行时报错 Iterator<String> it = al.iterator(); while(it.hasNext()) { String s = it.next(); System.out.println(s+":"+s.length()); } } } /*----------------------------------*/ /* class Demo<T> { public void show(T t) { System.out.println("show:"+t); } public void print(T t) { System.out.println("show:"+t); } } */ //泛型类定义的泛型,在整个类中有效。如果被方法使用, //那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了。 // //为了让不同方法可以操作不同类型,而且类型还不确定。 //那么可以将泛型定义在方法上。 /* 特殊之处: 静态方法不可以访问类上定义的泛型。 如果静态方法操作的应用数据类型不确定,可以将泛型定义在方法上。 */ //1.泛型定义在类上,在整个类上有效。 //如果被方法使用,那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了 class Demo<T> { public void show(T t) { System.out.println("show:"+t); } //2.泛型定义在方法上,(为了让不同方法可以操作不同类型,而且类型还不确定。)例如:打印输出不同类型的数据 public <Q> void print(Q q) { System.out.println("print:"+q); } //3.静态方法不可以访问类上定义的泛型。如果静态方法操作的应用数据类型不确定,可以将泛型定义在方法上。 public static <W> void method(W t) { System.out.println("method:"+t); } } 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); */ } } /*----------------------------------*/ //泛型定义在接口上。 interface Inter<T> { void show(T t); } /* class InterImpl implements Inter<String> { public void show(String t) { System.out.println("show :"+t); } } */ class InterImpl<T> implements Inter<T> { public void show(T t) { System.out.println("show :"+t); } } class GenericDemo5 { public static void main(String[] args) { InterImpl<Integer> i = new InterImpl<Integer>(); i.show(4); //InterImpl i = new InterImpl(); //i.show("haha"); } } /*----------------------------------*/ /* ? 通配符。也可以理解为占位符。 泛型的限定; ? 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); //使用方法重用后 } //? extends E: 可以接收E类型或者E的子类型 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); } } //<? super E>比较器使用代码复用后 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")); /*----------------------------------*/ 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集合的构造方法里面有个参数是<? super E>这种形式的 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 Worker extends Person { Worker(String name) { super(name); } } /*----------------------------------*/ /*----------------------------------*/