目录
泛型引入
先用传统方法解决
public class GenericDemo01 {
public static void main(String[] args) {
//传统方法解决
ArrayList list = new ArrayList();
list.add(new Dog("小黑",2));
list.add(new Dog("小白",3));
list.add(new Dog("小黄",4));
for (Object o :list) {
//向下转型获取get方法
Dog dog = (Dog) o;
System.out.println(dog.getName()+"__"+dog.getAge());
}
}
static class Dog{
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
会抛出异常
泛型入门
在遍历的时候可以直接取出Dog 省去了向下转型的问题。
泛型介绍
因为数组在new的时候不能确定key的类型就无法在内存中进行开辟空间
package generic;
public class T_tGenneric_ {
public static void main(String[] args) {
MyGnneric<String> stringMyGnneric = new MyGnneric<>();
stringMyGnneric.t = "张三";
stringMyGnneric.show();
System.out.println(stringMyGnneric.return_());
System.out.println("=========================");
MyGnneric<Integer> integerMyGnneric = new MyGnneric<>();
integerMyGnneric.t = 322;
integerMyGnneric.show();
System.out.println(integerMyGnneric.return_());
}
static class MyGnneric<T>{
T t;
public void show(){
System.out.println(t);
}
public T return_(){
return t;
}
}
}
泛型练习
package generic.Test;
import java.util.ArrayList;
import java.util.Comparator;
public class TestDemo01 {
public static void main(String[] args) {
ArrayList<Employee> ep = new ArrayList<>();
ep.add(new Employee("smith",20000,new MyDate(2000,5,23)));
ep.add(new Employee("tom",50000,new MyDate(1997,4,1)));
ep.add(new Employee("ann",10000,new MyDate(2001,2,7)));
ep.add(new Employee("ann",29900,new MyDate(1977,2,7)));
System.out.println("排序前====================");
for (Employee em :ep) {
System.out.println(em);
}
//对雇员进行排序
ep.sort(new Comparator<Employee>() {
@Override
public int compare(Employee e1, Employee e2) {
int i = e1.getName().compareTo(e2.getName());
if (i != 0){
return i;
}
return e1.getBirthday().compareTo(e2.birthday);
}
});
System.out.println("排序后=================");
for (Employee em :ep) {
System.out.println(em);
}
}
static 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;
}
}
static class MyDate implements Comparable<MyDate>{
private int year;
private int month;
private int day;
@Override
public String toString() {
return year + "年" + month + "月" + 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 int compareTo(MyDate o) {
int y = year - o.getYear();
if (y != 0 ){
return y;
}
int m = month - o.getMonth();
if (m != 0 ){
return m;
}
return day -o.getDay();
}
}
}
自定义泛型接口
继承接口时指定泛型的类型
类在实现接口时给泛型指定类型,在实现方法时会自动填充到指定位置
如果没有指定类型则默认为Object
自定义泛型方法
1.泛型方法可以定义在普通类·中也可以定义在泛型类中
//泛型方法可以定义在普通类·中也可以定义在泛型类中
class Dog{//Dog是否普通类
public void call(){ }//普通方法
public<T,E> void run(T t,E e){}//泛型方法
}
class Cat<E,R>{//泛型类
public <A,Z> void run(A a,Z z){}//泛型方法
}
方法被调用时类型会确定
public class Demo02 {
public static void main(String[] args) {
Dog dog = new Dog();
dog.run("喵喵",11);//方法被调用时类型会确定
}
}
//泛型方法可以定义在普通类·中也可以定义在泛型类中
class Dog{//Dog是否普通类
public void call(){ }//普通方法
public<T,E> void run(T t,E e){//泛型方法
System.out.println(t.getClass().getSimpleName());//t的类型
System.out.println(e.getClass().getSimpleName());//r的类型
}
}
泛型的继承和通配
jUnit
public class jUnit_ {
public static void main(String[] args) {
//传统方法 每次测试完为了防止后面测试被前面的影响,要注销非常不方便
//new jUnit_().m1();
//new jUnit_().m2();
}
@Test //alt+enter选择jUnit5.4 完成后就可以直接测试方法了
public void m1(){
System.out.println("m1被调用");
}
public void m2(){
System.out.println("m2被调用");
}
}