首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >强制转换字节变量并将其转换为字符串。

强制转换字节变量并将其转换为字符串。
EN

Stack Overflow用户
提问于 2018-06-14 13:09:28
回答 1查看 51关注 0票数 0

我想在控制台中打印Byte变量的值。我创建了“Portata”类

代码语言:javascript
复制
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:

代码语言:javascript
复制
 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)); 

但当我尝试:

代码语言:javascript
复制
  System.out.println(piatti.get(0).getNome()+", "+piatti.get(0).getDescr()+", "+Byte.toString(piatti.get(0).getCottura())+" ");

它显示为:

代码语言:javascript
复制
 "Pasta al Sugo, Ottimo piatto di pasta al Sugo, 0"

如何显示字节值(15)而不是?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-14 13:11:42

您既不初始化构造函数中的cottura字段,也不使用setter,因此它始终保持为0,因为这是byte的默认值。

要解决这个问题,在构造函数中初始化cottura字段,如下所示:

代码语言:javascript
复制
this.cottura = cottura;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50858350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档