泛型测试,通过例子简单理解java泛型

本文档展示了如何在Java中使用泛型类进行数据操作。通过创建`GenericTest1`泛型类,分别实例化为Integer、String、Double和Map类型,演示了泛型在存储不同类型数据时的灵活性。代码中包括了设置和获取数据的方法,并在main方法中打印了不同类型的实例数据。
摘要由CSDN通过智能技术生成
package com.atguigu.apitest.generic;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhanganjie
 * @version 1.0
 * @description: 泛型类测试
 * @date 2021/10/4 15:41
 */
public class GenericTest1<Z> {
    private Z data;

    public GenericTest1() {

    }

    public Z getData() {
        return data;
    }

    public void setData(Z data) {
        this.data = data;
    }

    public GenericTest1(Z data) {
        this.data = data;
    }

    public static void main(String[] args) {
        GenericTest1<Integer> setInteger = new GenericTest1<>();
        setInteger.setData(1);

        GenericTest1<String> setString = new GenericTest1<>();
        setString.setData("我是字符串");

        GenericTest1<Double> setDouble = new GenericTest1<>();
        setDouble.setData(123.123);

        GenericTest1<Object> setMap = new GenericTest1<>();
        Map<String,String> map = new HashMap<>();
        map.put("map集合","123");
        setMap.setData(map);


        System.out.println(setInteger.getData());
        System.out.println(setString.getData());
        System.out.println(setDouble.getData());
        System.out.println(setMap.getData());
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值