java泛型不是计算运行时的数据类型

本文介绍了一个基于Java泛型的实体类设计案例,通过具体代码示例展示了如何实现带有泛型的实体基类,并探讨了Java泛型在运行时的行为特性及BeanUtils.copyProperties方法在处理泛型时的限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.ada.data.entity;

import java.io.Serializable;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public class NumEntity<NUM extends Number> implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private NUM id;

	public NUM getId() {
		return id;
	}

	public void setId(NUM id) {
		this.id = id;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		NumEntity other = (NumEntity) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

}



package com.tongna.rest.apps;

import com.ada.data.entity.NumEntity;

public class A extends NumEntity<Long> {

}
A a = new A();
		a.setId(1l);
		Class<?> ccc = a.getClass().getMethod("getId").getReturnType();
		System.out.println(ccc);
		System.out.println(a.getId().getClass());

java泛型不是计算运行时的数据类型。

所以在使用BeanUtils.copyProperties(source, result);的时候id不能拷贝。




转载于:https://my.oschina.net/cng1985/blog/653334

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值