/*
*泛型的常见字符意义:
*T Type表示类型
*K,V分别代表键值对中的Key,Value
*E 代表Element
*?代表不确定的类型
*/
/**
* 泛型类:定义类是使用泛型
* 格式:
* class 类名<字母列表>{
* 修饰符 字母 属性;
* 修饰符 构造器(字母){
* }
* 修饰符 返回类型 方法(字母){
* }
* }
* */
/**
* 不能使用在静态属性和静态方法上
* 使用:指定具体类型
* 1。编译时会进行类型检查
* 2.获取数据是不需要强制类型转换
* 泛型使用时不能指定基本类型
* */
public class Student<T1,T2> {
public T1 getJavase() {
return javase;
}
public void setJavase(T1 javase) {
this.javase = javase;
}
public T2 getOracle() {
return oracle;
}
public void setOracle(T2 oracle) {
this.oracle = oracle;
}
private T1 javase;
private T2 oracle;
public static void main(String[] args) {
Student<String,Integer> stu=new Student<String,Integer>();
stu.setJavase("123");
String n=stu.getJavase();
System.out.println(n);
}
}
/*************************************************************************************************************************************/
package com.Test;
/**
* 接口中泛型字母只能使用在方法中,不能使用在全局常中
*
* */
public interface Comparator<T> {
void compare(T t);
}
/*************************************************************************************************************************************/
package com.Test;
import java.util.Date;
/**
* 泛型方法 <> 返回类型签名 只能访问对象的信息,不能修改对象的信息
* */
public class TestMethod {
public static void main(String[] args) {
test("a");
test(1);
test(new Date());
}
public static <T> void test(T a) {
System.out.println(a);
}
}