简体   繁体   English

数据类型原始对象未正确初始化布尔值

[英]Data type primitive object not initializing boolean correctly

I have to create a wrapper class to hold any primitive data type. 我必须创建一个包装类来保存任何原始数据类型。 I created the class, and it works as intended for everything except the boolean. 我创建了这个类,除了布尔值之外,它的所有内容都可以正常工作。 This is my code: 这是我的代码:

byte a; // the byte in primitive
short b; // the short
int c, j; // int c is is int, j is the counter for Primitive to tell what dataType it is
long d; // the long
float e; //the float
double f; // the double
boolean g; // the boolean, with which I am having problems
char h; // the char
...;
public Primitive(boolean i) {
    g = i; 
    j = 6;
}

The top method works as intended, however, when I try my copy method, it makes the boolean value equal to true, regardless of whether i is true or false. top方法按预期工作,但是,当我尝试复制方法时,它使布尔值等于true,无论i是true还是false。

public Primitive(Primitive i){
    switch (i.j){
    case 0: a = i.a; break;
    case 1: b = i.b; break;
    case 2: c = i.c; break;
    case 3: d = i.d; break;
    case 4: e = i.e; break;
    case 5: f = i.f; break;
    case 6: g = i.g; break;
    case 7: h = i.h; break;
    }
}

Any help would be greatly appreciated. 任何帮助将不胜感激。

EDIT** 编辑**

After testing the copy constructor for all the cases, I have discovered that the copy constructor does not work for the data types short, boolean, and char. 在测试了所有情况的复制构造函数之后,我发现复制构造函数不适用于short,boolean和char数据类型。 It works for everything else. 它适用于其他一切。 This is my modified code: 这是我修改过的代码:

public class Primitive {
    private byte bytes;
    private short shorts;
    private int integer;
    private final int DATATYPE;
    private long longs;
    private float floaters;
    private double doubles;
    private boolean bools;
    private char character;
    Compare compare = new Compare();
    /**************************************************|
    |*                  Constructors                  *|
    |**************************************************/
    public Primitive(byte i) {
        bytes = i;
        DATATYPE = 0;
    }
    public Primitive(short i) {
        shorts = i;
        DATATYPE = 1;
    }
    public Primitive(int i) {
        integer = i;
        DATATYPE = 2;
    }
    public Primitive(long i) {
        longs = i;
        DATATYPE = 3;
    }
    public Primitive(float i) {
        floaters = i;
        DATATYPE = 4;
    }
    public Primitive(double i) {
        doubles = i;
        DATATYPE = 5;
    }
    public Primitive(boolean i) {
        bools = i; 
        DATATYPE = 6;
    }
    public Primitive(char i) {
        character = i;
        DATATYPE = 7;
    }
    public Primitive(Primitive i){
        switch (i.DATATYPE){
        case 0: bytes = i.bytes; break;
        case 1: shorts = i.shorts; break;
        case 2: integer = i.integer; break;
        case 3: longs = i.longs; break;
        case 4: floaters = i.floaters; break;
        case 5: doubles = i.doubles; break;
        case 6: bools = i.bools; break;
        case 7: character = i.character; break;
        }
        DATATYPE = i.DATATYPE;
    }
    ...;
}

I was going to attempt an ENUM, but I have forgotten how to use it. 我打算尝试使用ENUM,但我忘记了如何使用它。 That, and I think that an integer would be easier to manipulate than an ENUM. 那,我认为整数比ENUM更容易操作。

You're forgetting to set the j value of the new Primitive. 您忘记设置新基元的j值。 Add the following after your switch: 切换后添加以下内容:

this.j = i.j

Without this, the second time you try to make a constructor copy, it falls back to case 0. I suspect that it's working for the other values only because you haven't tested this case yet, but it's hard to say without seeing that code. 如果没有这个,第二次尝试制作构造函数时,它会回到案例0.我怀疑它只适用于其他值,因为你还没有测试过这个案例,但是如果没有看到代码就很难说。

As mentioned above, the key error is failing to initialize j . 如上所述,关键错误是无法初始化j Recognizing that j has to be set every time in every constructor, you might mark j "final". 认识到每次在每个构造函数中都必须设置j ,你可以标记j “final”。

Additionally the variables you have are designed to confuse. 此外,您拥有的变量旨在混淆。 This is especially the case for j . 对于j来说尤其如此。 first off, burying a control variable j as a 1 letter variable name with your domain data c is very confusing. 首先,将控制变量j作为1字母变量名称与您的域数据c非常混乱。 No need to cram them so tight (the compiler does that). 不需要这么紧凑(编译器这样做)。 ONE LETTER VARIABLE NAMES ARE CONFUSING. 一个字母变量名称令人困惑。

Using an int to know the type is really old-school. 使用int来了解类型真的是老派。 An Enum would be more appropriate. Enum会更合适。

(and as another responder noted; this must be homework, since the built-in primitive wrapper classes (Integer for int, Boolean for boolean, ...) are designed for this and baked into the language.) (并且正如另一个响应者指出的那样;这必须是家庭作业,因为内置的原始包装类(Integer for int,Boolean for boolean,...)是为此设计的并且被烘焙成语言。)

If you decide to keep the code mostly as is, at least rename j and separate it from c . 如果您决定保持代码大部分原样,至少重命名j并将其与c分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM