我想在控制台中打印Byte变量的值。我创建了“Portata”类
public class Portata {
String nome,descr,portata;
double prezzo;
byte qt,cottura;
String[] ingr,allerg;
public Portata(String nome, String descr, String[] ingr, String[] allerg, double prezzo, byte cottura, String portata, byte qt) {
this.nome=nome;
this.descr=descr;
this.ingr=ingr;
this.allerg=allerg;
this.prezzo=prezzo;
this.portata=portata;
this.qt=qt;
}
/** All getters and setters **/
public byte getCottura() {
return cottura;
}
public byte getQt() {
return qt;
}
/** All getters and setters **/
}我在另一个类中创建了一个ArrayList:
ArrayList<Portata> piatti=(new Portata("Pasta al Sugo","Ottimo piatto di pasta al Sugo",elenco_ingredienti,elenco_allergeni,25.00,(byte)15,"Primo",(byte)0)); 但当我尝试:
System.out.println(piatti.get(0).getNome()+", "+piatti.get(0).getDescr()+", "+Byte.toString(piatti.get(0).getCottura())+" ");它显示为:
"Pasta al Sugo, Ottimo piatto di pasta al Sugo, 0"如何显示字节值(15)而不是?
发布于 2018-06-14 13:11:42
您既不初始化构造函数中的cottura字段,也不使用setter,因此它始终保持为0,因为这是byte的默认值。
要解决这个问题,在构造函数中初始化cottura字段,如下所示:
this.cottura = cottura;https://stackoverflow.com/questions/50858350
复制相似问题