Java:如何定义基于Integer的自定义数据类型?

编程入门 行业动态 更新时间:2024-10-25 00:34:00
本文介绍了Java:如何定义基于Integer的自定义数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一个与Integer完全相同的数据类型,我希望它能溢出并下溢到某些值。换句话说,我想设置Integer类的对象/实例的MAX_VALUE和MIN_VALUE。问题是MAX_VALUE和MIN_VALUE是常量,最后是Integer类。我该怎么办?

I need a data type that behaves completely like Integer with this difference that I want it to overflow and underflow to certain values. On the other words, I'd like to set the MAX_VALUE and MIN_VALUE of an object/instance of the Integer class. The problem is MAX_VALUE and MIN_VALUE are constant and Integer class in final. How should I approach?

推荐答案

你必须创建自己的包装类:

You'll have to create your own wrapper class:

public class CustomInteger { public static final int MAX_VALUE = 5000; public static final int MIN_VALUE = -5000; private final int value; public CustomInteger(int value) { // TODO: Validation this.value = value; } // Add all the methods you want - e.g. integer operations etc // performing custom overflow/underflow on each operation }

您需要决定是否需要为整个类型设置一对固定限制,或者每个实例是否可以具有不同的限制(以及将两个具有不同限制的值加在一起时的含义等)。

You need to decide whether you want one fixed pair of limits for the whole type, or whether each instance can have a different limit (and what that means when adding together two values with different limits etc).

更多推荐

Java:如何定义基于Integer的自定义数据类型?

本文发布于:2023-10-19 14:43:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507829.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   数据类型   定义   Java   Integer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!