认识泛型
1.泛型是在JDK1.5之后增加的新功能 Generic
2.泛型可以解决数据类型的安全性问题,主要原理是在类声明的时候,通过一个标识表示类中某个
属性的类型或者是某个方法的返回值及参数类型, 但仅在编译时刻起作用,在运行时是没有区别的,例如List和List<String>,通过反射,它们的类类型是一样的
3.格式:访问权限 class 类名称<泛型,泛型...>{
属性
方法
}
4.对象的创建
类名称<具体类型> 对象名称 = new 类名称<具体类型>();
/**
* 经纬度 int Float String
*
* @author guo
*
*/
public class GenericType {
public static void main(String[] args) {
Point<String> point = new Point<String>();
point.setX("经度为:109");
point.setY("经度为:100");
System.out.println(point.getX() + " " + point.getY());
}
}
class Point<T> {
private T x;
private T y;
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
}
构造方法中使用泛型
构造方法可以为类中的属性初始化,那么如果类中的属性通过泛型指定,而又需要通过构造方法
设置属性内容的时候,那么构造方法的定义与之前并无不同,不需要像声明类那样指定泛型
public class GenericDemo2 {
public static void main(String[] args) {
Constructor<String> constructor = new Constructor<String>("guo");
System.out.println(constructor.getValue());
}
}
class Constructor<T> {
private T value;
public Constructor(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
设置多个泛型
public class GenericDemo3 {
public static void main(String[] args) {
Gen<String, Integer> gen = new Gen<String, Integer>();
gen.setTake(10);
gen.setKeyK("key");
System.out.println(gen.getTake() + " " + gen.getKeyK());
}
}
class Gen<K, T> {
private T take;
private K keyK;
public T getTake() {
return take;
}
public void setTake(T take) {
this.take = take;
}
public K getKeyK() {
return keyK;
}
public void setKeyK(K keyK) {
this.keyK = keyK;
}
}
通配符
public class GenericDemo4 {
public static void main(String[] args) {
Info<String> info = new Info<String>();
info.setKey("guo");
tell(info);
}
public static void tell(Info<?> i) {
System.out.println(i);
}
}
class Info<T> {
private T key;
public T getKey() {
return key;
}
public void setKey(T key) {
this.key = key;
}
@Override
public String toString() {
return this.getKey().toString();
}
}
泛型接口
在JDK1.5之后,不仅仅可以声明泛型类,也可以声明泛型接口,声明泛型接口和声明泛型类的语法类似
也是在接口名称后面加上<T>
public class GenericDemo5 {
public static void main(String[] args) {
Gin<String> gin = new Gin<String>("guo");
System.out.println(gin.getInfo());
}
}
interface GenInter<T> {
void say();
}
class Gin<T> implements GenInter<T> {
private String info;
public Gin(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public void say() {
}
}
泛型方法
public class GenericDemo6 {
public static void main(String[] args) {
Genr genr = new Genr();
System.out.println(genr.tell("guo"));
System.out.println(genr.tell(24));
}
}
class Genr {
public <T> T tell(T t) {
return t;
}
}
方形数组
public class GenericDemo7 {
public static void main(String[] args) {
String[] arr = { "a", "b", "c" };
tell(arr);
}
public static <T> void tell(T[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
1.泛型是在JDK1.5之后增加的新功能 Generic
2.泛型可以解决数据类型的安全性问题,主要原理是在类声明的时候,通过一个标识表示类中某个
属性的类型或者是某个方法的返回值及参数类型, 但仅在编译时刻起作用,在运行时是没有区别的,例如List和List<String>,通过反射,它们的类类型是一样的
3.格式:访问权限 class 类名称<泛型,泛型...>{
属性
方法
}
4.对象的创建
类名称<具体类型> 对象名称 = new 类名称<具体类型>();
/**
* 经纬度 int Float String
*
* @author guo
*
*/
public class GenericType {
public static void main(String[] args) {
Point<String> point = new Point<String>();
point.setX("经度为:109");
point.setY("经度为:100");
System.out.println(point.getX() + " " + point.getY());
}
}
class Point<T> {
private T x;
private T y;
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
}
构造方法中使用泛型
构造方法可以为类中的属性初始化,那么如果类中的属性通过泛型指定,而又需要通过构造方法
设置属性内容的时候,那么构造方法的定义与之前并无不同,不需要像声明类那样指定泛型
public class GenericDemo2 {
public static void main(String[] args) {
Constructor<String> constructor = new Constructor<String>("guo");
System.out.println(constructor.getValue());
}
}
class Constructor<T> {
private T value;
public Constructor(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
设置多个泛型
public class GenericDemo3 {
public static void main(String[] args) {
Gen<String, Integer> gen = new Gen<String, Integer>();
gen.setTake(10);
gen.setKeyK("key");
System.out.println(gen.getTake() + " " + gen.getKeyK());
}
}
class Gen<K, T> {
private T take;
private K keyK;
public T getTake() {
return take;
}
public void setTake(T take) {
this.take = take;
}
public K getKeyK() {
return keyK;
}
public void setKeyK(K keyK) {
this.keyK = keyK;
}
}
通配符
public class GenericDemo4 {
public static void main(String[] args) {
Info<String> info = new Info<String>();
info.setKey("guo");
tell(info);
}
public static void tell(Info<?> i) {
System.out.println(i);
}
}
class Info<T> {
private T key;
public T getKey() {
return key;
}
public void setKey(T key) {
this.key = key;
}
@Override
public String toString() {
return this.getKey().toString();
}
}
泛型接口
在JDK1.5之后,不仅仅可以声明泛型类,也可以声明泛型接口,声明泛型接口和声明泛型类的语法类似
也是在接口名称后面加上<T>
public class GenericDemo5 {
public static void main(String[] args) {
Gin<String> gin = new Gin<String>("guo");
System.out.println(gin.getInfo());
}
}
interface GenInter<T> {
void say();
}
class Gin<T> implements GenInter<T> {
private String info;
public Gin(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public void say() {
}
}
泛型方法
public class GenericDemo6 {
public static void main(String[] args) {
Genr genr = new Genr();
System.out.println(genr.tell("guo"));
System.out.println(genr.tell(24));
}
}
class Genr {
public <T> T tell(T t) {
return t;
}
}
方形数组
public class GenericDemo7 {
public static void main(String[] args) {
String[] arr = { "a", "b", "c" };
tell(arr);
}
public static <T> void tell(T[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}