gpt4 book ai didi

java - 使用 Number 子类的泛型创建翻转计数器

转载 作者:行者123 更新时间:2023-12-01 04:17:22 24 4
gpt4 key购买 nike

我正在尝试创建一个计数器,每当它达到预设上限时就会翻转,并在达到所述上限时重置回其下限值。我已经实现了该类(class)并且效果很好。然而,在我寻求解决方案的过程中,我想尝试一下 Java 泛型。我想尝试扩展我的计数器,以便它不仅使用整数,而且可以使用任何类型的数字。我知道计数器通常只需要使用整数,但我想看看是否可以做到。

我认为代码将类似于下面的代码。但是,java.lang.Number 没有获取/设置其值的“通用”方法。我是否需要创建自己的号码类才能启用此功能?另外,我知道如果我确实做到了这一点,我需要改变我的等于检查,以便它们有一个浮点值的错误阈值,这或多或少是我的 int 计数器的修改版本,我认为它可以工作泛型。

编辑:有人建议我采取一种映射方法,存储一个整数计数器并保留一个增量值,这样当我想吐出一个数字时,我只需将当前计数乘以增量值即可。但是,我不相信这会满足我的确切需求,因为我不想每次都增加相同的数量。该计数器的主要焦点更多的是一种拥有固定范围数字的方法,当添加或减去该数字时,知道如何处理回绕。

我想描述它的最佳方式(尽管可能不正确)就像一个自动处理上溢/下溢的Integer

package com.math;

public class GenericRolloverCounter<T extends Number> {

private T value;
private T lowValue;
private T highValue;

public GenericRolloverCounter(T l_startValue, T l_highValue) {
this.lowValue = l_startValue;
this.highValue = l_highValue;
this.value = l_startValue;
}

public T getValue() {
return value;
}

public void setValue(T value) {
this.value = value;
}

public void increment(T valToIncrementBy) {
this.value += valToIncrementBy;
if (this.value > this.highValue) {
this.value = (this.lowValue + (this.value - (this.highValue + 1)));
}
}

public void increment() {
this.increment(1);
}

public void decrement(T valToDecrementBy) {
this.value -= valToDecrementBy;
if (this.value < this.lowValue) {
this.value = ((this.value + this.highValue + 1) - this.lowValue);
}
}

public void decrement() {
this.decrement(1);
}

@Override
public String toString() {
return Integer.toString(this.value);
}
}

最佳答案

您可能还想指定计算的金额。默认值为 1

您可以通过使用 Number 方法 .doubleValue() 并进行 double 算术来解决一些问题。

这是转换为使用此想法的方法之一。

public void decrement(double valToDecrementBy) {
double work = this.value.doubleValue();
work -= valToDecrementBy;
// should use some value related to incrementing amount
if ((this.value.doubleValue() - this.lowValue.doubleValue()) < 0.1D) {
work = ((this.value.doubleValue() + this.highValue.doubleValue() + 1) - this.lowValue.doubleValue());
}
// ... no way to put it back
}

但是,仍然没有办法干净、简单地恢复值(value)。由于“Number”只有一些常用的非抽象子类,因此您可以执行一些丑陋的 instanceof 操作来存储回值。它看起来像这样:

if (theValue instanceof Double) { // depends on it having a non-null value prior
theValue = (T)(new Double(work));
}

或者,当您开始使用 double 时,您可以将起始值转换为 double

private double value;
private double lowValue;
private double highValue;

public GenericRolloverCounter(T l_startValue, T l_highValue) {
this.lowValue = l_startValue.doubleValue();
this.highValue = l_highValue.doubleValue();
this.value = l_startValue.doubleValue();
}

这确实引入了增加浮点值的问题以及那里的舍入/评估问题。

哦......你的toString()应该是:

return value.toString();

T 类上使用 native toString() 方法。

@Crusher 的评论提出了另一种方法。将所有内容映射到“int”并保留一个乘数。这是一些代码来说明我的意思。 (感谢 splinter 机)

private int value;
private int lowValue;
private int highValue;
private double incr;

public GenericRolloverCounter(T l_startValue, T l_highValue, T incrementAmount) {
double incr = incrementAmount.doubleValue();
this.lowValue = Math.round(l_startValue.doubleValue() / incr);
this.highValue = Math.round(l_highValue.doubleValue() / incr);
this.value = Math.round(l_startValue.doubleValue() / incr);
}

public void increment(int valToIncrementBy) {
this.value += valToIncrementBy;
if (this.value > this.highValue) {
this.value = (this.lowValue + (this.value - (this.highValue + 1)));
}
}

@Override
public String toString() {
return String.valueOf(incr * this.value);
}

关于java - 使用 Number 子类的泛型创建翻转计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19302332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com