First off, sorry for my english...
I'm doing a Enum type but I can't do, because I'm using tipo(String nombre, short valor)
Why must I use tipo(String nombre, int valor)
? Using int instead of short ?
public enum Tipo {
// The constructor (String, int) is undefined
DAT ("DAT", -2);
private String nombre;
private short valor;
tipo(String nombre, short valor){
this.nombre = nombre;
this.valor = valor;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public short getValor() {
return valor;
}
public void setValor(short valor) {
this.valor = valor;
}
}
Try DAT ("DAT", (short)-2);
You are passing an int
to a constructor that takes a short
. Java doesn't auto-cast from int
to short
because of the potential loss of data.
A very good explanation of this can be found here - primitive type short casting in java
There are two changes you need to make to the above:
1) Fix typo with your field 'Valor' to be lower case
private short valor;
2) Cast the int to a short:
DAT ("DAT", (short)-2);
Additionally, you should also rename your enum type to be 'Tipo' which is the recommended naming format for enums.
By default a primitive number will be treated as an integer, and going from an integer to a short requires a cast. The compiler wont perform this type casting automatically - this is because going from a larger (an int) to a smaller (a short) introduces the risk of losing data (by truncating the number) and so the compiler forces you to cast it.
So you have two choices:
DAT("DAT", (short)-1);
or
private short valor;
Tipo(String nombre, int valor){
this.valor= (short)valor;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.