Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我是java新手。有一种方法可以返回 xxx 类型的值。
public Cost getTotalCost() { return getCost(); }
我想将 getCost() 转换为类型字符串。我该怎么做?
最通用的方法是:
String.valueOf(getCost());
这将处理空值,或调用.toString()对象的方法。
.toString()
最好为您的类实现该.toString()方法。Cost
Cost
请注意,以编程方式依赖该toString()方法被认为是不好的做法 - 它应该仅用于人眼而不是锁定在特定实现中。
toString()