Java 如何正确使用构造函数?
嗨,我是Java新手,构造器仍然在吸引我。我正在尝试创建和枚举,如下所示:Java 如何正确使用构造函数?,java,constructor,Java,Constructor,嗨,我是Java新手,构造器仍然在吸引我。我正在尝试创建和枚举,如下所示: 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 intVal
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);
}
}
公共枚举错误
{
SCARD_S_成功(0x0L),
疤痕共享违反(0x801000BL),
无可用的SCARD_E_阅读器(0x8010002EL),
SCARD_E_阅读器不可用(0x80100017L);
私有int值;
私有静态java.util.HashMap映射;
私有静态java.util.HashMap getMappings()
{
if(映射==null)
{
同步(SCardErrors.class)
{
if(映射==null)
{
mappings=new java.util.HashMap();
}
}
}
返回映射;
}
私人SCardErrors(整型值)
{
intValue=值;
getMappings().put(值,this);
}
public int getValue()
{
返回值;
}
公共静态SCardErrors forValue(int值)
{
返回getMappings().get(值);
}
}
从上面看,它给出了一个错误,即未为行定义构造函数SCardErrors(long):
SCARD_S_成功(0x0L),
疤痕共享违反(0x801000BL),
无可用的SCARD_E_阅读器(0x8010002EL),
SCARD_E_阅读器不可用(0x80100017L)
我已经试着加进去了
SCARD_S_成功(0x0L){
}
但它没有修复错误。我还尝试将它们添加为参数,但这不起作用。有人能帮忙吗?Java找不到构造函数的原因是您在所有数字文本上使用了
L
后缀,使它们长
,而不是int
。Java不允许在没有显式强制转换的情况下将long
转换为int
,因此编译器无法在这种情况下应用接受int
的构造函数(反之亦然,即将int
传递给需要long
的方法或构造函数会起作用)
您可以通过将编译器更改为采用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);
}
private long-value;
私有静态java.util.HashMap映射;
私有静态java.util.HashMap getMappings()
{
if(映射==null)
{
同步(SCardErrors.class)
{
if(映射==null)
{
mappings=new java.util.HashMap();
}
}
}
返回映射;
}
私人SCardErrors(长期价值)
{
longValue=价值;
getMappings().put(值,this);
}
公共长getValue()
{
返回长值;
}
公共静态SCardErrors forValue(长值)
{
返回getMappings().get(值);
}
Java找不到构造函数的原因是您在所有数字文本上使用了L
后缀,使它们长
,而不是int
。Java不允许在没有显式强制转换的情况下将long
转换为int
,因此编译器无法在这种情况下应用接受int
的构造函数(反之亦然,即将int
传递给需要long
的方法或构造函数会起作用)
您可以通过将编译器更改为采用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);
}
private long-value;
私有静态java.util.HashMap映射;
私有静态java.util.HashMap getMappings()
{
if(映射==null)
{
同步(SCardErrors.class)
{
if(映射==null)
{
mappings=new java.util.HashMap();
}
}
}
返回映射;
}
私人SCardErrors(长期价值)
{
longValue=价值;
getMappings().put(值,this);
}
公共长getValue()
{
返回长值;
}
公共静态SCardErrors forValue(长值)
{
返回getMappings().get(值);
}
Java找不到构造函数的原因是您在所有数字文本上使用了L
后缀,使它们长
,而不是int
。Java不允许在没有显式强制转换的情况下将long
转换为int
,因此编译器无法在这种情况下应用接受int
的构造函数(反之亦然,即将int
传递给需要long
的方法或构造函数会起作用)
您可以通过将编译器更改为采用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);
}
private long-value;
私有静态java.util.HashMap映射;
私有静态java.util.HashMap getMappings()
{
if(映射==null)
{
同步(SCardErrors.class)
{
if(映射==null)
{
mappings=new java.util.HashMap();
}
}
}
返回映射;
}
私人SCardErrors(长期价值)
{
longValue=价值;
getMappings().put(值,this);
}
公共长getValue()
{
返回长值;
}
公共静态SCardErrors forValue(长值)
{
返回getMappings().get(值);
}
Java找不到构造函数的原因是您在所有数字文本上使用了L
后缀,使它们长
,而不是int
。Java不允许在没有显式强制转换的情况下将long
转换为int
,因此编译器无法在这种情况下应用接受int
的构造函数(反之亦然,即将int
传递给需要long
的方法或构造函数会起作用)
您可以通过将编译器更改为采用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);
}
private long-value;
私有静态java.util.HashMap映射;
私有静态java.util。