泛型

泛型

  1. Java泛型本质是参数化类型,把类型作为参数传递。
  2. 常见形式有泛型类、泛型接口、泛型方法。
  3. 语法:<T, …> T称为类型占位符,表示一种引用类型。如果编写多个使用逗号隔开。
  4. 好处:
    • 提高代码的重要性;
    • 防止类型转换异常,提高代码的安全性。
参考代码(创建泛型)
//泛型类  语法:类名<T>
//T是类型占位符,表示一种引用类型,如果编写多个使用逗号隔开
public class MyGeneric<T> {
    //使用泛型T
    //1.创建变量
    T t;

    //2.泛型作为方法的参数
    public void show(T t) {
        //T t1 = new T(); //不能实例化
        System.out.println(t);
    }

    //3.泛型作为方法的返回值

    public T getT() {
        return t;
    }

}

泛型类

注意
  1. 泛型只能使用引用类型;
  2. 不同泛型对象类型不能相互赋值。
参考代码(使用泛型创建对象测试)
public class TestGeneric {
    public static void main(String[] args) {
        //使用泛型类创建对象
        //注意:1.泛型只能使用引用类型;2.不同泛型对象类型不能相互赋值
        MyGeneric<String> myGeneric = new MyGeneric<String>();
        myGeneric.t = "hello";
        myGeneric.show("泛型");
        String string = myGeneric.getT();

        MyGeneric<Integer> myGeneric1 = new MyGeneric<>(); //后面尖括号里可以不写类型
        myGeneric1.t = 123;
        myGeneric1.show(666);
        Integer integer = myGeneric1.getT();

        //MyGeneric<String> myGeneric2 = myGeneric1; //不同泛型对象不能相互赋值
    }
}

泛型接口

//创建泛型接口

//泛型接口  语法:接口名<T>
//注意:不能使用泛型创建静态常量
public interface MyInterface<T> {
    String name = "王二";

    T server(T t);
}

//实现泛型接口

//第一种方法:写实现类时确定类型
public class MyInterfaceImpl1 implements MyInterface<String> {
    @Override
    public String server(String s) {
        System.out.println(s);
        return s;
    }
}

//实现泛型接口

//第二种方法:实现类也是一个泛型类,实现接口时不确定
public class MyInterfaceImpl2<T> implements MyInterface<T> {
    @Override
    public T server(T t) {
        System.out.println(t);
        return t;
    }
}

//测试

public class TestGeneric {
    public static void main(String[] args) {

        MyInterfaceImpl1 impl1 = new MyInterfaceImpl1();
        impl1.server("泛型接口");

        MyInterfaceImpl2<Integer> impl2 = new MyInterfaceImpl2<>();
        impl2.server(66);
    }
}

泛型方法

//创建泛型方法

//泛型方法  语法:<T>返回值类型
public class MyGenericMethod {
    //泛型方法
    public <T> T show(T t) { //可以是静态
        System.out.println("非静态泛型方法:" + t);
        return t;
    }

    public static <W> W old(W w) {
        System.out.println("静态泛型方法:" + w);
        return w;
    }
}

//测试

package com.lb.demo2;

public class TestGeneric {
    public static void main(String[] args) {

        //泛型方法
        //类型不需要传递,类型是由传递的数据决定
        
        MyGenericMethod myGenericMethod = new MyGenericMethod();
        myGenericMethod.show("非静态泛型方法");
        myGenericMethod.show(6);
        myGenericMethod.show(3.14);

        MyGenericMethod.old("静态泛型方法");
        MyGenericMethod.old(8);
        MyGenericMethod.old(3.1415926);

    }
}

泛型集合

  1. 概念:参数化类型、类型安全的集合,强制集合元素的类型必须一致。
  2. 特点:
    • 编译时即可检查,而非运行时抛出异常;
    • 访问时,不必类型转换(拆箱);
    • 不同泛型之间引用不能相互赋值,泛型不存在多态。
参考代码
import com.lb.demo1.Student;

import java.util.ArrayList;
import java.util.Iterator;

public class Demo03 {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("泛型集合");
        //arrayList.add(10); //已定义为String类型,所以不能添加其他类型

        for (String string : arrayList) {
            System.out.println(string);
        }

        ArrayList<Student> arrayList1 = new ArrayList<Student>();
        Student s1 = new Student("小王", 30);
        Student s2 = new Student("小刘", 21);
        Student s3 = new Student("小帅", 53);
        arrayList1.add(s1);
        arrayList1.add(s2);
        arrayList1.add(s3);

        Iterator<Student> it = arrayList1.iterator();
        while (it.hasNext()) {
            Student s = it.next(); //不用强制转换
            System.out.println(s.toString());
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值