Java泛型详解

一、泛型简介1、简介①泛型是一种未知的数据类型;(不知道用什么数据类型的时候,用泛型)

②泛型也可以看做是一个变量,用来接收数据类型;

2、使用泛型的好处没有使用泛型的好处:

集合不使用泛型,默认是Object类型。可以存储任意类型的数据;

没有使用泛型的弊端:

集合不使用泛型,是不安全的,容易引发异常;

使用泛型的好处:

①避免了类型转换的麻烦,存取的类型是一致的;

②将代码运行期的异常提升到了编译器上;

使用泛型的弊端:

泛型是什么类型就只能存储什么类型;

二、使用1、定义和使用一个含有泛型的类还有泛型的学生类:代码语言:javascript代码运行次数:0运行复制public class Student {

private E name;

public Student() {

}

public Student(E name) {

this.name = name;

}

public E getName() {

return name;

}

public void setName(E name) {

this.name = name;

}

}Test测试类:代码语言:javascript代码运行次数:0运行复制public class Test {

public static void main(String[] args) {

//用整数

Student student = new Student<>();

student.setName(100);

System.out.println(student.getName());//100

//用字符串

Student student1 = new Student<>();

student1.setName("小明");

System.out.println(student1.getName());//小明

}

}2、定义和使用一个含有泛型的方法格式:代码语言:javascript代码运行次数:0运行复制修饰符 <泛型> 返回值类型 方法名(参数列表(这个地方传递泛型参数)){

方法体;//返回值类型也可以是泛型,跟传入的泛型数据类型一致

}使用:学生类:

代码语言:javascript代码运行次数:0运行复制public class Student{

public E get(E e){

return e;

}

}Test测试类:

代码语言:javascript代码运行次数:0运行复制public class Test {

public static void main(String[] args) {

//用整数

Student student1 = new Student();

int i = student1.get(100);//100

System.out.println(i);

//用字符串

Student student = new Student();

String string = student.get("100");

System.out.println(string);//100

}

}3、定义和使用一个含有泛型的接口接口:

代码语言:javascript代码运行次数:0运行复制public interface genericInterface {

public abstract void get(E e);

}实现类1(实现接口的时候确定数据类型):

代码语言:javascript代码运行次数:0运行复制public class GenericInterfaceImpl implements genericInterface {

@Override

public void get(String s) {

System.out.println(s);

}

}实现类2(实现接口的时候不确定数据类型,那么实现类也要用泛型):

代码语言:javascript代码运行次数:0运行复制public class GenericInterfaceImpl1 implements genericInterface {

@Override

public void get(E e) {

System.out.println(e);

}

}Test测试类:

代码语言:javascript代码运行次数:0运行复制public class Test {

public static void main(String[] args) {

//实现类1

GenericInterfaceImpl genericInterface = new GenericInterfaceImpl();

genericInterface.get("必须是字符串");//必须是字符串

//实现类2-字符串

GenericInterfaceImpl1 genericInterfaceImpl1 = new GenericInterfaceImpl1<>();

genericInterfaceImpl1.get("字符串");//字符串

//实现类2-数字

GenericInterfaceImpl1 genericInterfaceImpl2 = new GenericInterfaceImpl1<>();

genericInterfaceImpl2.get(100);//100

}

}4、泛型通配符泛型通配符:

?:代表任意的数据类型;

使用方式:

不能创建对象使用;

只能作为方法的参数使用;

使用:

代码语言:javascript代码运行次数:0运行复制package study.generic;

import java.util.ArrayList;

import java.util.Iterator;

public class Test {

public static void main(String[] args) {

//字符串-定义的时候不能用泛型通配符?

ArrayList arrayList = new ArrayList<>();

arrayList.add("小明");

arrayList.add("小李");

arrayList.add("小兰");

printArray(arrayList);//小明 小李 小兰

//整数-定义的时候不能用泛型通配符?

ArrayList arrayList1 = new ArrayList<>();

arrayList1.add(100);

arrayList1.add(200);

arrayList1.add(300);

printArray(arrayList1);//100 200 300

}

public static void printArray(ArrayList arrayList){

for(Iterator iterator = arrayList.iterator();iterator.hasNext();){

System.out.println(iterator.next());

}

}

}高级使用----受限泛型:(不常用,知道就行)

之前设置泛型的时候,实际上是可以任意设置的,只要是类就可以设置。但是在JAVA的泛型中可以指定一个泛型的上限和下限。

泛型的上限:

格式: 类型名称 对象名称意义: 只能接收该类型及其子类泛型的下限:

格式: 类型名称 对象名称意义: 只能接收该类型及其父类型

Copyright © 2088 世界杯预选赛中国_1994年世界杯冠军是谁 - nywk120.com All Rights Reserved.
友情链接
Top