泛(广泛)型(类型)
1)泛型又称参数化类型,是JDK5.0出现的新特性,解决数据类型的安全性问题
2)在类声明或实例化时只要指定好需要的具体的类型即可
3)JAVA泛型可以保证如果程序在编译时没有发出警告,运行时就不会产生
4)泛型的作用是:可以在类声明时通过一个标识表示类中某个属性的类型,或者是某个方法的返回值的类型,
或者是参数类型
public static void main(String[] args) { Personcity01 = new Person ("成都"); Person city02 = new Person (100); city01.play(); city02.play(); }
/*泛型的作用: * 1.可以在类声明时通过一个标识表示类中某个属性的类型; * 2.同时也可以是某个方法的返回值的类型,或者是参数类型 * */ class Person{//*E表示h的数据类型,该数据类型在定义Person对象时候指定;在编译期间,就确定E的类型 E h; public Person(E h) {//E也可以是参数类型 this.h = h; } public E fun(){//E也可以作为返回值 return h; } public void play(){ System.out.println(h.getClass()); } }
泛型的声明
interface接口
//例如:List,ArrayList
说明:
1)其中;T,K,V不代表值,而是表示类型;并且任意字母都可以是
泛型的实例化
要在类名后面指定类型参数的值(类型),
例如:
1)List
2). Iterator
练习:
public static void main(String[] args) { //给泛型指定具体类型后,可以传入该类型或者其子类类型 AnimalcatAnimal = new Animal (new Cat()); catAnimal.what(); Animal catAnimal1 = new Animal (new Tiger()); catAnimal1.what(); }
class Cat{}; class Tiger extends Cat{}; class Animal{ E h; public Animal(E h) { this.h = h; } public void what(){ System.out.println("Animal的类型:" + h.getClass()); } }
泛型使用的注意事项和细节
1.interface List
说明:T,E只能是引用类型
List
•List
2.在给泛型指定具体类型后,可以传入该类型或者其子类类型
3.泛型使用形式
List
List
如果:List list3 = new ArrayList(); 默认给它的泛型是
public static void main(String[] args) { //给泛型指定具体类型后,可以传入该类型或者其子类类型 AnimalcatAnimal = new Animal (new Cat()); catAnimal.what(); Animal catAnimal1 = new Animal (new Tiger()); catAnimal1.what(); }
class Cat{}; class Tiger extends Cat{}; class Animal{ E h; public Animal(E h) { this.h = h; } public void what(){ System.out.println("Animal的类型:" + h.getClass()); } }
泛型的练习 定义Employee类 1)该类包含: private成员变量name.sal,birthday,其中 birthday 为 MyDate类的对象; 2)为每一个属性定义 getter, setter 方法; 3)重写toString 方法输出name, sal, birthday .4) MyDate类包含:private成员变量month,day,year;并为每一个属性定义 getter,.setter方法; 5)创建该类的3个对象,并把这些对象放入ArrayList 集合中(ArrayList需使用泛型来定义),对集合中的元素进行排序,并遍历输出: 排序方式:调用ArrayList 的sort方法,传入Comparator对象[使用泛型],先按照name排序,如果name相同,则按生日日期的先后排序。【即:定制排序】
public static void main(String[] args) { ArrayListemployees = new ArrayList<>(); //定义三个对象 employees.add(new Employee("jack",7000.80, new MyDate(12,25,2015))); employees.add(new Employee("jack",8000.80, new MyDate(6,12,2015))); employees.add(new Employee("hmith",7500.80, new MyDate(9,20,2016))); //进行进行名字比较;在名字一样时进行年月日的比较 employees.sort(new Comparator () { @Override public int compare(Employee o1, Employee o2) { //判断比较的两个数是否类型是Employee类型 if(!(o1 instanceof Employee && o2 instanceof Employee)){ System.out.println("类型不匹配..."); return 0; } //比较名字的大小(比较的方法是自然比较法,[value方法中用的字符比较]) int name = o1.getName().compareTo(o2.getName()); if(name != 0){ return name; } //返回年月日比较 return o1.getBirthday().compareTo(o2.getBirthday()); } }); //遍历ArrayList集合 System.out.println("employees:" + employees); }
//定义员工类 class Employee{ private String name; private double sal; private MyDate birthday; public Employee(String name, double sal, MyDate birthday) { this.name = name; this.sal = sal; this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSal() { return sal; } public void setSal(double sal) { this.sal = sal; } public MyDate getBirthday() { return birthday; } public void setBirthday(MyDate birthday) { this.birthday = birthday; } @Override public String toString() { return "\n" + name + "\t" + sal +"\t" + birthday; } } //定义日月年;并实现Comparable接口 class MyDate implements Comparable{ private int month; private int day; private int year; public MyDate(int month, int day, int year) { this.month = month; this.day = day; this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } @Override public String toString() { return month + "-" + day + "-"+ year; } //对compareTo的重写 @Override public int compareTo(MyDate date) { //比较年份 int years = this.year - date.getYear(); if (years != 0){ return years; } //比较月份 int months = this.month - date.getMonth(); if (months != 0){ return months; } //比较天 int days = 0; return days = this.day - date.getDay(); } }