workplace-blog

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  30 随笔 :: 0 文章 :: 0 评论 :: 701 阅读

泛型

1.泛型介绍

  • 泛型是JDK1.5引入的新特性,本质是参数化类型,把类型作为参数传递

  • 常见形式有:泛型类,泛型接口,泛型方法

  • 语法:

    • <T,....>:T称为类型占位符,表示一种引用数据类型
  • 好处:

    • 提高代码重用性
    • 防止类型转换错误,提高代码安全性

2.泛型类

/*
泛型类
语法:类名<T>
T:类型占位符,表示一种引用数据类型,创建多个需要用逗号隔开
 */
public class Demo01<T> {
    //1.创建泛型变量,不能实例化(不确定构造方法)
    T t;

    //2.泛型作为方法的参数
    public void show(T t){
        System.out.println(t);
    }

    //3.泛型作为方法的返回值
    public T getT(){
        return t;
    }
}
public class Test {
    public static void main(String[] args) {
        //只能使用引用类型填充T
        Demo01<String> s = new Demo01<String>();
        s.t="world";
        s.show("hello");
        System.out.println(s.getT());

        Demo01<Integer> integer = new Demo01<>();
        integer.t = 10;
        integer.show(22);
        System.out.println(integer.getT());
    }
}

//输出结果:
hello
world
22
10

3.泛型接口

/*
泛型接口:接口名<>
不可创建泛型静态常量
 */
//定义接口MyInterface
public interface MyInterface<T> {
    String name = "zhangsan";

    //泛型方法,传入值和返回值均为泛型
    T serve(T t);
}
//可以确定T的类型,继承接口时写明类型
public class MyInterfaceImpl implements MyInterface<String> {
    @Override
    public String serve(String s) {
        System.out.println(s);
        return s;
    }
}
//不确定接口泛型的具体类型,实现类使用泛型。实例化的时候再传入类型
public class MyInterfaceImplFan<T> implements MyInterface<T> {
    @Override
    public T serve(T t) {
        System.out.println(t);
        return t;
    }
}
//泛型接口的实现类
MyInterfaceImpl impl = new MyInterfaceImpl();
impl.serve("discount");

//泛型接口的泛型实现类
MyInterfaceImplFan<Integer> implFan = new MyInterfaceImplFan<>();
implFan.serve(100);

//输出结果:
discount
100

4.泛型方法

//泛型方法
//语法:<T> 返回值类型
public class GenericMethod {

   public  <T> void show(T t){
       System.out.println(t);
   }
}
//泛型方法
System.out.println("--------3.泛型方法-------");
GenericMethod method = new GenericMethod();
method.show(10);
method.show("string");

//输出结果:
10
string

5.泛型集合

  • 概念:参数化类型,类型安全的集合,强制集合元素的类型必须一致
  • 特点:
    • 编译时即可检查,而非运行时抛出异常‘
    • 访问时不必类型转换(拆箱)
    • 不同泛型之间引用不能相互赋值,泛型不存在多态。
//泛型集合
public class GenericCollection {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();//Object类型
        arrayList.add("xxx");
        arrayList.add("yyy");
        arrayList.add(111);
        arrayList.add(222);

//        for (Object object:arrayList
//             ) {
//            //类型转换时报错,原arrayList数组含多种类型的数据,不能全部转换为同一种
//            String s = (String)object;
//            System.out.println(s);
//        }

        //使用泛型,规定集合内元素均为student类型
        ArrayList<student> arrayList1 = new ArrayList<>();
        student s1 = new student("zmy",26);
        student s2 = new student("pjh",32);
        student s3 = new student("ssw",29);
        arrayList1.add(s1);
        arrayList1.add(s2);
        arrayList1.add(s3);

        //迭代器·遍历(遍历时不需要类型转换)
        ListIterator<student> lit = arrayList1.listIterator();
        while (lit.hasNext()){
            student student = lit.next();
            System.out.println(student);
        }

    }
}

//输出结果:
student{name='zmy', age=26}
student{name='pjh', age=32}
student{name='ssw', age=29}
posted on   多巴胺LLL  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
点击右上角即可分享
微信分享提示