惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
WordPress大学
WordPress大学
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
Project Zero
Project Zero
博客园 - 聂微东
Recorded Future
Recorded Future
MongoDB | Blog
MongoDB | Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Stack Overflow Blog
Stack Overflow Blog
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
C
CERT Recently Published Vulnerability Notes
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta
美团技术团队
Microsoft Azure Blog
Microsoft Azure Blog
C
Cisco Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
小众软件
小众软件
N
News and Events Feed by Topic
D
DataBreaches.Net
量子位
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
F
Fortinet All Blogs
博客园 - 司徒正美
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
AI
AI
腾讯CDC
I
InfoQ
P
Palo Alto Networks Blog
G
Google Developers Blog
T
Threat Research - Cisco Blogs
T
Tenable Blog
Vercel News
Vercel News
Google Online Security Blog
Google Online Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

微风静语

用 codebase-memory-mcp 给 AI 编程加一层“代码记忆” EchoCraft - 专为独立开发者和小型团队打造的产品官网主题 Halo 站长必备:外链安全与流量分析神器——“链接工具箱”重磅升级! Halo插件开发:java17旧项目迁移至java21项目记录 Halo插件|一个面向创作者的多功能媒体处理工具集 分布式事务详解:从入门到精通 让你的网站初步适配PWA Halo主题 - 微浸:以轻量构建美学,用细节定义体验 深入理解Python爬虫技术:原理、实现与实践 Halo-2.20 新特性:自定义登录页面模版 vue3 中父子组件数据传输踩坑 将你的服务运维面板从宝塔替换为1panel Python实战项目:外星人入侵(源码分享) theme-hao主题适配友链自助提交插件 plugin-artalk 插件保姆级使用教程 plugin-blog-hao 插件部分功能演示 halo 插件开发踩坑记录(一) Thymeleaf 模版引擎语法浅记 信息洪流中的自我救赎 mac 系统里如何管理多个jdk版本 memso API 不完全指南 基础语法:从CPU角度看变量、数组、类型、运算、跳转、函数等语法 halo-theme-hao主题:artalk评论区美化 衡量个人成长 halo-theme-hao 主题魔改教程 分享一个魔改的 Artalk 评论系统邮件模版 halo-theme-hao 主题标签记录 开发一个 Halo2.0 的安全条跳转中台插件! Mac 软件推荐:使用 Mac 一年半来,我一直在用的软件! TypeScript教程---面向对象编程 软件版本命名规范 TypeScript教程---基础语法及编译设置 2024年家乡春季的第一场大雪 提问的智慧 基础篇:容器化部署技术 -—docker,从此摆脱多环境配置的苦恼! 普通本科的四年大学教育,给我带来了什么? 因果与平衡 程序的本质:代码是如何被执行的 软件开发者应该具备的基本提问素质 如何编写 Restful 风格的接口 探索力 Springboot入门基础篇 springboot中如何使用注解来实现aop Springboot项目生成接口文档方法 Java 实体代码生成器 lombok 的使用 分布式版本控制工具 git 的基本使用 如何搭建前后端分离的项目 初识Vue3--令人焕然一新的使用逻辑和代码组织方式! 浅谈前端发展史 为什么要学习数据结构和算法 关于富文本编辑器 wangeditor 在 vue2 项目中的使用
深入 Java 泛型
webjing · 2022-10-25 · via 微风静语

Java的泛型是 jdk1.5 引入的一个新特性,其特点就是将类型进行一个参数化,把类型作为参数传递。
一些常见的泛型类型有:

E 元素(Element),多用于 Java集合框架
K 关键字(key)
V 值(value)
T 类型(Type)
N 数字(Number)

语法:

<T,...> //T称为类型占位符,表示一种引用类型

好处:

  1. 提高代码的重用性
  2. 防止类型转换异常,提高代码的安全性

二、泛型的常见使用形式

1、泛型类

/**
 * 泛型类
 * 语法:类名<T>
 * T是类型占位符,表示一种引用类型,如果编写多个使用逗号隔开
 * @author: wb
 */
public class MyGeneric<T> {
    //使用泛型T
    //1创建变量
    T t;

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

    //3使用泛型作为方法的返回值
    public T getT(){
        return t;
    }
}

/**
 * @author: wb
 */
public class TestGeneric {
    public static void main(String[] args) {
        //使用泛型类创建对象
        //注意:1 泛型只能使用引用类型 2 不同泛型对象之间不能相互赋值
        MyGeneric<String> myGeneric = new MyGeneric<>();
        myGeneric.t = "hello world";
        myGeneric.show("字符串泛型");
        String string = myGeneric.getT();
        System.out.println(string);

        MyGeneric<Integer> myGeneric2 = new MyGeneric<>();
        myGeneric2.t = 1024;
        myGeneric2.show(65536);
        Integer integer = myGeneric2.getT();
        System.out.println(integer);

    }
}

打印结果:

字符串泛型
hello world
65536
1024

2、泛型接口

/**
 * 泛型接口
 * 语法:接口名<T>
 * 注意:不能创建泛型静态常量
 * @author: wb
 */
public interface MyInterface<T> {
    String name = "张三";

    T server(T t);
}

泛型接口的实现方式一:

/**
 * @author: wb
 */
public class MyInterfaceImpl implements MyInterface<String> {
    @Override
    public String server(String s) {
        System.out.println(s);
        return s;
    }
}

泛型接口的实现方式二:

/**
 * @author: wb
 */
public class MyInterfaceImpl2<T> implements MyInterface<T> {
    @Override
    public T server(T t) {
        System.out.println(t);
        return t;
    }
}

测试

/**
 * @author: wb
 */
public class TestGeneric {
    public static void main(String[] args) {
        //泛型接口
        MyInterfaceImpl impl = new MyInterfaceImpl();
        impl.server("hello world");

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

控制台打印结果:

hello world
1024

3、泛型方法

/**
 * 泛型方法
 * 语法:<T>返回值类型
 * @author: wb
 */
public class MyGenericMethod {
    //泛型方法
    public <T> T show(T t){
        System.out.println("泛型方法" + t);
        return t;
    }
}

调用方法:

public class TestGeneric {
    public static void main(String[] args) {
        //泛型方法
        MyGenericMethod myGenericMethod = new MyGenericMethod();
        myGenericMethod.show("我的祖国");
        myGenericMethod.show(123);
        myGenericMethod.show(3.14);
    }
}
/**
* 控制台打印结果:
* 泛型方法我的祖国
* 泛型方法123
* 泛型方法3.14
*/

3、泛型集合

概念:参数化类型的集合、类型安全的集合,强制集合元素的类型必须一致。
优点:

  • 编译时检查,而非运行时抛出异常。
  • 访问时,不必类型转换
  • 不同泛型引用之间不能相互赋值,Java集合中没有

示例:

/**
 * @author: wb
 */
public class Demo3 {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("xxx");
        arrayList.add("yyy");

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

        ArrayList<Student> arrayList2 = new ArrayList<Student>();
        Student s1 = new Student("张三", 21);
        Student s2 = new Student("里斯", 23);
        Student s3 = new Student("王五", 24);
        arrayList2.add(s1);
        arrayList2.add(s2);
        arrayList2.add(s3);
        Iterator<Student> it = arrayList2.iterator();
        while (it.hasNext()) {
            Student next = it.next();
            System.out.println(next);
        }

    }
}
/**
* 学生类
* @author wb
*/
class Student{
   private String name;
   private String age;
   public Student(String name, int age){
       this.name = name;
       this.age = age;
   }
   public void setName(String name){
       this.name =name;
   }
   public Stirng getName(String name){
        return this.name;
   }
}