Can I write a literal initializer in Java for a no

2020-04-30 03:06发布

Java's literal initializers are mostly used for primitives (e.g. int i = 2 and double j = 4.2), but there are certain non-primitive classes that also have literal initializers (e.g. String a = "Hello" or Number z = 43). I am looking to implement an initializer similar to that in a class that I wrote. I have a class Numeric that extends Number, that I would like to be able to initialize as Numeric a = 43. Is there any way that this can be done in Java, and if so, how?

Here is the part of the source code for Numeric

public class Numeric extends Number {

    private HashMap<Primitive, Number> values;
    private Primitive origin;

    public Numeric(byte value) {
        values = new HashMap<>();
        values.put(Primitive.BYTE, value);
        values.put(Primitive.SHORT, (short) value);
        values.put(Primitive.INT, (int) value);
        values.put(Primitive.LONG, (long) value);
        values.put(Primitive.FLOAT, (float) value);
        values.put(Primitive.DOUBLE, (double) value);
        origin = Primitive.BYTE;
    }

    public Numeric(short value) {
        values = new HashMap<>();
        values.put(Primitive.BYTE, (byte) value);
        values.put(Primitive.SHORT, value);
        values.put(Primitive.INT, (int) value);
        values.put(Primitive.LONG, (long) value);
        values.put(Primitive.FLOAT, (float) value);
        values.put(Primitive.DOUBLE, (double) value);
        origin = Primitive.SHORT;
    }

    public Numeric(int value) {
        values = new HashMap<>();
        values.put(Primitive.BYTE, (byte) value);
        values.put(Primitive.SHORT, (short) value);
        values.put(Primitive.INT, value);
        values.put(Primitive.LONG, (long) value);
        values.put(Primitive.FLOAT, (float) value);
        values.put(Primitive.DOUBLE, (double) value);
        origin = Primitive.INT;
    }

    public Numeric(long value) {
        values = new HashMap<>();
        values.put(Primitive.BYTE, (byte) value);
        values.put(Primitive.SHORT, (short) value);
        values.put(Primitive.INT, (int) value);
        values.put(Primitive.LONG, value);
        values.put(Primitive.FLOAT, (float) value);
        values.put(Primitive.DOUBLE, (double) value);
        origin = Primitive.LONG;
    }

    public Numeric(float value) {
        values = new HashMap<>();
        values.put(Primitive.BYTE, (byte) value);
        values.put(Primitive.SHORT, (short) value);
        values.put(Primitive.INT, (int) value);
        values.put(Primitive.LONG, (long) value);
        values.put(Primitive.FLOAT, value);
        values.put(Primitive.DOUBLE, (double) value);
        origin = Primitive.FLOAT;
    }

    public Numeric(double value) {
        values = new HashMap<>();
        values.put(Primitive.BYTE, (byte) value);
        values.put(Primitive.SHORT, (short) value);
        values.put(Primitive.INT, (int) value);
        values.put(Primitive.LONG, (long) value);
        values.put(Primitive.FLOAT, (float) value);
        values.put(Primitive.DOUBLE, value);
        origin = Primitive.DOUBLE;
    }

Primitive is just a basic enum with all the primitive types.

标签: java literals
2条回答
地球回转人心会变
2楼-- · 2020-04-30 03:18

No, this is not possible. There is no way whatsoever to initialize a value other than a primitive or a String like you want.

查看更多
做个烂人
3楼-- · 2020-04-30 03:25

String literals as a special case in Java. Numbers don't really have literals - what you're seeing here is a primitive (such as 2) being autoboxed to its wrapper class (such as Integer). Since all the numeric wrappers extends Number, this assignments works (in the same way that Object o = 4; would be a legal statement).

You can not add your own literals to the language, though. The best you could do is to create some static function that produces a new instance of your class.

查看更多
登录 后发表回答