package com.francis.generic;
public class Generic03 {
public static void main(String[] args) {
//注意,特别强调: E 具体的数据类型在定义 Person 对象的时候指定,即在编译期间,就确定 E 是什么类型
Person person = new Person("你好,北京!");
person.show(); //String
Person person2 = new Person(100);
person2.show();//Integer
}
}
//泛型的作用是:可以在类声明时通过一个标识表示类中某个属性的类型,
// 或者是某个方法的返回值的类型,或者是参数类型
class Person {
E s;//E 表示 s 的数据类型, 该数据类型在定义 Person 对象的时候指定,即在编译期间,就确定 E 是什么类型
public Person(E s) {//E 也可以是参数类型
this.s = s;
}
public E f() {//返回类型使用 E
return s;
}
public void show() {
System.out.println(s.getClass());//显示 s 的运行类型
}
}
3、泛型的语法
3.1、泛型的声明
接口:
interface 接口名{}
类:
class 类名{}
比如List和ArrayList的声明
其中的T、E、F、G不代表值,而是代表类型,任何字母都可以
3.2、泛型的实例化
要在类名的后面指定类型参数的值。 比如List list = new ArrayList ();
3.3、泛型使用的注意事项和细节
(1)interface List{},public class HashSet{}等等中的T,E只能是引用类型,例如List list2 = new ArrayList()编译不通过 (2)在实例化时指定具体的泛型类型后,可以传入该类型或者其子类类型 (3)如果带有泛型的接口或类在实例化时不指定泛型类型,则默认泛型为Object
package com.francis.generic.exercise;
import java.util.ArrayList;
import java.util.Comparator;
@SuppressWarnings({"all"})
public class GenericExercise02 {
public static void main(String[] args) {
ArrayList employees = new ArrayList<>();
employees.add(new Employee("tom", 20000, new MyDate(1980, 12, 11)));
employees.add(new Employee("jack", 12000, new MyDate(2001, 12, 12)));
employees.add(new Employee("tom", 50000, new MyDate(1980, 12, 10)));
System.out.println("employees=" + employees);
employees.sort(new Comparator() {
@Override
public int compare(Employee emp1, Employee emp2) {
//先按照 name 排序,如果 name 相同,则按生日日期的先后排序。【即:定制排序】
//先对传入的参数进行验证
if (!(emp1 instanceof Employee && emp2 instanceof Employee)) {
System.out.println("类型不正确..");
return 0;
}
//比较 name
int i = emp1.getName().compareTo(emp2.getName());
if (i != 0) {
return i;
}
//下面是对 birthday 的比较,因此,我们最好把这个比较,放在 MyDate 类完成
//封装后,将来可维护性和复用性,就大大增强.
return emp1.getBirthday().compareTo(emp2.getBirthday());
}
});
System.out.println("==对雇员进行排序==");
System.out.println(employees);
}
}
package com.francis.generic.exercise;
public 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;
}
@Override
public String toString() {
return "nEmployee{" +
"name='" + name + ''' +
", sal=" + sal +
", 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;
}
}
package com.francis.generic.exercise;
public class MyDate implements Comparable {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
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;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
@Override
public int compareTo(MyDate o) {
int yearMinus=this.year-o.getYear();
if (!(yearMinus==0)){
return yearMinus;
}
int monthMinus=this.month-o.getMonth();
if (!(monthMinus==0)){
return monthMinus;
}
int dayMinus=this.day-o.getDay();
return dayMinus;
}
}
package com.francis.generic.custom;
import java.util.ArrayList;
@SuppressWarnings({"all"})
public class CustomMethodGeneric {
public static void main(String[] args) {
Car car = new Car();
car.fly("宝马", 100);//当调用方法时,传入参数,编译器,就会确定类型
System.out.println("=======");
car.fly(300, 100.1);//当调用方法时,传入参数,编译器,就会确定类型
//测试
//T->String, R-> ArrayList
Fish fish = new Fish<>();
fish.hello(new ArrayList(), 11.3f);
}
}
//泛型方法,可以定义在普通类中, 也可以定义在泛型类中
class Car {//普通类
public void run() {//普通方法
}
//说明 泛型方法
//1. 就是泛型
//2. 是提供给 fly 使用的
public void fly(T t, R r) {//泛型方法
System.out.println(t.getClass());//String
System.out.println(r.getClass());//Integer
}
}
class Fish {//泛型类
public void run() {//普通方法
}
public void eat(U u, M m) {//泛型方法
}
//说明
//1. 下面 hi 方法不是泛型方法
//2. 是 hi 方法使用了类声明的 泛型
public void hi(T t) {
}
//泛型方法,可以使用类声明的泛型,也可以使用自己声明泛型
public void hello(R r, K k) {
System.out.println(r.getClass());//ArrayList
System.out.println(k.getClass());//Float
}
}
5、泛型的继承和通配符
5.1、泛型的继承和通配符说明
(1)泛型不具备继承性,例如 List list = new ArrayList()是不对的 (2)>支持任意泛型类型 (3) extends A>支持A类及A类的子类,规定了泛型的上限 (4) super A> 支持A类及A类的父类,规定了A类的上限 这里是起到规范的作用,注意,是在实例化时,给类型参数传值时使用,不能在定义泛型类时使用
package com.francis.generic;
import java.util.ArrayList;
import java.util.List;
public class GenericExtends {
public static void main(String[] args) {
Object o = new String("xx");
//泛型没有继承性
//List list = new ArrayList();
//举例说明下面三个方法的使用
List list1 = new ArrayList<>();
List list2 = new ArrayList<>();
List list3 = new ArrayList<>();
List list4 = new ArrayList<>();
List list5 = new ArrayList<>();
//如果是 List> c ,可以接受任意的泛型类型
printCollection1(list1);
printCollection1(list2);
printCollection1(list3);
printCollection1(list4);
printCollection1(list5);
//List extends AA> c: 表示 上限,可以接受 AA 或者 AA 子类
// printCollection2(list1);//×
// printCollection2(list2);//×
printCollection2(list3);//√
printCollection2(list4);//√
printCollection2(list5);//√
//List super AA> c: 支持 AA 类以及 AA 类的父类,不限于直接父类
printCollection3(list1);//√
//printCollection3(list2);//×
printCollection3(list3);//√
//printCollection3(list4);//×
//printCollection3(list5);//×
}
// ? extends AA 表示 上限,可以接受 AA 或者 AA 子类
public static void printCollection2(List extends AA> c) {
for (Object object : c) {
System.out.println(object);
}
}
//说明: List> 表示 任意的泛型类型都可以接受
public static void printCollection1(List> c) {
for (Object object : c) { // 通配符,取出时,就是 Object
System.out.println(object);
}
}
// ? super 子类类名 AA:支持 AA 类以及 AA 类的父类,不限于直接父类,
//规定了泛型的下限
public static void printCollection3(List super AA> c) {
for (Object object : c) {
System.out.println(object);
}
}
}
class AA {
}
class BB extends AA {
}
class CC extends BB {
}