4. 泛型
1. 泛型的好处



代码示例
package com.baidu.www;
import java.util.ArrayList;
import java.util.List;
/*
泛型一般只和集合类相结合使用。
泛型是JDK5的新特性,从JDK7开始,后边的泛型不用写具体的类型(菱形泛型)
*/
public class Test {
public static void main(String[] args) {
// 泛型
// List<String> list1 = new ArrayList<String>();
List<String> list1 = new ArrayList<>();
list1.add("abc");
list1.add("bcd");
list1.add("cde");
for (String s : list1) {
System.out.println(s);
}
}
}
2. 泛型的说明

3. 泛型的语法


代码示例
package com.baidu.www;
public class Test {
public static void main(String[] args) {
xi<Animal> x = new xi<>(new Animal());
x.f();
xi<Animal> x1 = new xi<>(new Cat());
x1.f();
}
}
class Animal {}
class Cat extends Animal {}
class xi<T>{
T t;
public xi(T t) {
this.t = t;
}
public void f(){
System.out.println(t.getClass());
}
}
练习题

代码示例
MyData类
package com.baidu.www;
public class MyDate implements Comparable<MyDate>{
private int month;
private int day;
private int year;
@Override
public String toString() {
return "MyDate{" +
"month=" + month +
", day=" + day +
", year=" + year +
'}';
}
public MyDate(int year, int month, int day) {
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 int compareTo(MyDate o) {
int yearMinus = this.year - o.year;
if(yearMinus != 0)
return yearMinus;
int monthMinus = this.month - o.month;
if(monthMinus != 0)
return monthMinus;
int dayMinus = this.day - o.day;
if(dayMinus != 0)
return dayMinus;
return 0;
}
}
Employee类
package com.baidu.www;
public class Employee {
private String name;
private int sal;
private MyDate birthday;
public Employee(String name, int sal, MyDate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
}
Test类
package com.baidu.www;
import java.util.ArrayList;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
ArrayList<Employee> arrayList = new ArrayList<>();
arrayList.add(new Employee("shuashua", 12, new MyDate(2002, 10, 23)));
arrayList.add(new Employee("ahuaahua", 12, new MyDate(2002, 10, 23)));
arrayList.add(new Employee("shuashua", 12, new MyDate(2012, 10, 23)));
System.out.println(arrayList);
arrayList.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
if(!(o1 instanceof Employee && o2 instanceof Employee))
return 0;
if(o1.getName().compareTo(o2.getName()) != 0)
return o1.getName().compareTo(o2.getName());
return o1.getBirthday().compareTo(o2.getBirthday());
}
});
System.out.println(arrayList);
}
}
4. 自定义泛型类

5. 自定义泛型接口

6. 自定义泛型方法
调用方法传入参数,编译器自动确定类型。

7. 泛型的继承和通配符

8. JUnit 单元测试类

代码示例
package com.baidu.www;
import org.junit.jupiter.api.Test;
public class JUnit_ {
public static void main(String[] args) {
// JUnit测试
}
@Test
public void m1(){
System.out.println("1");
}
@Test
public void m2(){
System.out.println(2);
}
}

浙公网安备 33010602011771号