简体   繁体   English

如何正确使用构造函数?

[英]How to correctly use constructors?

Hi I am new to Java and constructors are still consfusing me. 嗨,我是Java的新手,构造函数仍然困扰我。 I am trying to create and enum as below: 我正在尝试创建和枚举如下:

public enum SCardErrors
{
    SCARD_S_SUCCESS(0x0L),
    SCARD_E_SHARING_VIOLATION(0x8010000BL),
    SCARD_E_NO_READERS_AVAILABLE(0x8010002EL),
    SCARD_E_READER_UNAVAILABLE(0x80100017L);

    private int intValue;
    private static java.util.HashMap<Integer, SCardErrors> mappings;
    private static java.util.HashMap<Integer, SCardErrors> getMappings()
    {
        if (mappings == null)
        {
            synchronized (SCardErrors.class)
            {
                if (mappings == null)
                {
                    mappings = new java.util.HashMap<Integer, SCardErrors>();
                }
            }
        }
        return mappings;
    }

    private SCardErrors(int value)
    {
        intValue = value;
        getMappings().put(value, this);
    }

    public int getValue()
    {
        return intValue;
    }

    public static SCardErrors forValue(int value)
    {
        return getMappings().get(value);
    }
}

From above it gives the error that constructor SCardErrors(long) is undefined for the lines: SCARD_S_SUCCESS(0x0L), SCARD_E_SHARING_VIOLATION(0x8010000BL), SCARD_E_NO_READERS_AVAILABLE(0x8010002EL), SCARD_E_READER_UNAVAILABLE(0x80100017L); 从上面给出的错误是未为以下行定义构造函数SCardErrors(long):SCARD_S_SUCCESS(0x0L),SCARD_E_SHARING_VIOLATION(0x8010000BL),SCARD_E_NO_READERS_AVAILABLE(0x8010002EL),SCARD_E_READER_UNAVAILLABLE(0x80100017)

I have tried adding in SCARD_S_SUCCESS(0x0L){ } for each of them but it did not fix the error. 我尝试为它们中的每个添加SCARD_S_SUCCESS(0x0L){},但是它没有解决错误。 I also tried adding them as a parameter but this didn't work. 我也尝试将它们添加为参数,但这没有用。 Can someone please help?? 有人可以帮忙吗?

You've only got a constructor accepting int : 您只有一个接受int的构造函数:

private SCardErrors(int value)

... but you're trying to call it with an argument of type long : ...但是您尝试使用long类型的参数来调用它:

SCARD_S_SUCCESS(0x0L)

You either need to change the constructor to accept a long - and probably change the field type as well - or change the arguments to be int values instead. 您要么需要更改构造函数以接受long (可能还更改了字段类型),要么将参数更改为int值。 Not all of your arguments can be represented as int (eg 0x80100017L) - but if you only care about the bits involved, and don't mind the values becoming negative, you could cast to int and still have the same 32 bits... 并非所有的参数都可以表示为int (例如0x80100017L)-但是,如果您只关心所涉及的位,并且不介意值变为负数,则可以将其强制转换为int并仍然具有相同的32位...

The reason Java does not find the constructor is that you used L suffix on all your numeric literals, making them long , not int . Java找不到构造函数的原因是,您在所有数字文字上使用了L后缀,使它们变long ,而不是int Java does not allow conversion of long to int without an explicit cast, so the compiler cannot apply the constructor that takes an int in this situation (the other way around, ie passing an int to a method or a constructor expecting a long , would have worked). Java不允许在没有显式强制转换的情况下将long转换为int ,因此编译器无法在这种情况下应用采用int的构造函数(反之亦然,即将int传递给期望long的方法或构造函数,工作)。

You can fix this by changing the compiler to take long , and using longValue in place of intValue : 您可以通过将编译器更改为long并使用longValue代替intValue来解决此问题:

private long longValue;
private static java.util.HashMap<Long, SCardErrors> mappings;
private static java.util.HashMap<Long, SCardErrors> getMappings()
{
    if (mappings == null)
    {
        synchronized (SCardErrors.class)
        {
            if (mappings == null)
            {
                mappings = new java.util.HashMap<Long, SCardErrors>();
            }
        }
    }
    return mappings;
}

private SCardErrors(long value)
{
    longValue = value;
    getMappings().put(value, this);
}

public long getValue()
{
    return longValue;
}

public static SCardErrors forValue(long value)
{
    return getMappings().get(value);
}

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

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