泛型方法的概述和使用
- A:泛型方法概述
- B:定义格式
- public <泛型类型> 返回类型 方法名(泛型类型 变量名)
- C:案例演示
package com.heima.bean;
public class Tool<Q> {
private Q q;
public Q getObj() {
return q;
}
public void setObj(Q q) {
this.q = q;
}
public<T> void show(T t) { //方法泛型最好与类的泛型一致
System.out.println(t); //如果不一致,需要在方法上声明该泛型
}
public static<W> void print(W w) { //静态方法必须声明自己的泛型
System.out.println(w);
}
}
package com.heima.generic;
import com.heima.bean.Student;
import com.heima.bean.Tool;
import com.heima.bean.Worker;
public class Demo03_Generic {
public static void main(String[] args) {
Tool<String> t = new Tool<>();
//t.show("abc");
t.show(true);
}
}