Java - 模板中Byte,Integer,Long等的原始对应部分 [英] Java -- Primitive Counterpart of Byte, Integer, Long, etc. in template
问题描述
背景:
我正在尝试实现一个小模板,即泛型类,这将允许我实现传递 - 参考功能如下。
I am trying to implement a tiny template, i.e. generic class, which would allow me to achieve a pass-by-reference functionality as follows.
public static class Ref<T> {
T value;
public Ref(T InitValue) { this.set(InitValue); }
public void set(T Value) { this.value = Value; }
public T get() { return this.value; }
}
所以,我可以定义一个带'Ref'的函数值实际上可以更改,例如
So, I could define a function that takes a 'Ref' where the value can actually be changed, e.g.
public static void function(Ref<Byte> x)
{
x.set((byte)0x7E);
}
通过引用传递的变量的初始化看起来并不那么优雅。
The initialization of the variable to be passed by reference looks not so elegant.
Ref<Byte> to_be_changed = new Ref<Byte>((byte)0);
...
function(to_be_changed);
...
Byte result = to_be_changed.get()
问题:
Java有没有办法做得更好?构造函数可以根据与作为模板类型传递的包装类型相关的基元类型直接初始化'0'吗?即类似
Is there a way in Java to do it better? Can the constructor initialize directly a '0' according to the primitive type related to the wrapper type which is passed as template type? I.e something like
...
public Ref() { this.value = (T.relatedPrimitiveClass())0; }
...
其中 Integer.relatedPrimitiveClass()
将提供 int
; Byte.relatedPrimitiveClass()
发送字节
。
推荐答案
首先要了解的最重要的事情是java泛型不是模板。泛型是在类型上参数化的类/接口。我建议从oracle阅读泛型教程: https://docs.oracle.com /javase/tutorial/java/generics/types.html
First and the most important thing to understand is that java generics are not templates. Generics are classes/interfaces that are parameterized over types. I recommend reading generics tutorial from oracle: https://docs.oracle.com/javase/tutorial/java/generics/types.html
可以使用反射来获取Ref类的参数化类型T并使用它确定默认construtor的初始值,但我不建议这样做。
It is possible to use reflection to get the parametrized type T of your Ref class and use that to determinate the initial value for default construtor, but I would not recommend doing so.
您可以为类型创建子类,而不是反射,这需要默认构造函数(例如对象版本) of primitives):
Instead of reflection you can create subclasses for types, that require default constructors (e.g. object versions of primitives):
public static class ByteRef extends Ref<Byte> {
public ByteRef() {
super((byte)0);
}
public ByteRef(byte value) {
super(value);
}
// I'm not sure, if I like this one :-)
public void set(int value) {
super.set((byte)value);
}
}
您也可以添加新方法,而不是子类化Ref class:
Instead of subclassing, you can also add new methods to Ref class:
public static class Ref<T> {
T value;
public Ref(T initValue) {
this.set(initValue);
}
public void set(T Value) {
this.value = Value;
}
public T get() {
return this.value;
}
public static Ref<Byte> createByteRef() {
return new Ref<Byte>((byte)0);
}
public static Ref<Byte> createByteRef(byte value) {
return new Ref<Byte>(value);
}
}
或者您可以创建单独的工厂类:
public class Refs {
public static Ref createByteRef(){
return new Ref((byte)0);
}
Or you could create separate factory classes: public class Refs { public static Ref createByteRef() { return new Ref((byte)0); }
public static Ref<Byte> createByteRef(byte value) {
return new Ref<Byte>(value);
}
}
最后一个选项是使用反射来获取参数化类型。我个人不会使用这个解决方案,因为原始类的数量是有限的,你可以选择用子类化创建更多更简洁的接口
The last option is to use reflection to get the parameterized type. I personally would not use this solution, because number of primitive classes is finite and you have option to create much neater interfaces with subclassing
public abstract static class PrimitiveNumberRef<T extends Number> extends
Ref<T> {
private Class<T> type;
public PrimitiveNumberRef() {
// This requires default constructor for Ref class
type = getGenericType(getClass());
super.set((T) getInitialValue(type));
}
@Override
public void set(T value) {
if (value == null) {
throw new IllegalArgumentException(
"Null value is not allowed for PrimitiveNumerRef type: "
+ type);
}
if (!type.isInstance(value)) {
throw new IllegalArgumentException("Unsupported value type: "
+ value.getClass());
}
super.set(value);
}
@SuppressWarnings("unchecked")
private static <T> Class<T> getGenericType(Class<?> clz) {
return (Class<T>) ((ParameterizedType) clz.getGenericSuperclass())
.getActualTypeArguments()[0];
}
private static <T> T getInitialValue(Class<T> clz) {
if (clz == Byte.class) {
return clz.cast((byte) 0);
} else if (clz == Short.class) {
return clz.cast((short) 0);
} else if (clz == Integer.class) {
return clz.cast((int) 0);
} else if (clz == Double.class) {
return clz.cast((double) 0);
} else if (clz == Float.class) {
return clz.cast((float) 0);
} else if (clz == Long.class) {
return clz.cast((long) 0);
} else {
throw new IllegalArgumentException("Unsupported type: "
+ clz.getName());
}
}
}
PrimitiveNumberRef实例化如下:
PrimitiveNumberRef is instantiated as follows:
Ref<Long> val1 = new PrimitiveNumberRef<Long>() { };
这篇关于Java - 模板中Byte,Integer,Long等的原始对应部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!