@Entity
public class Person {
private Integer id = null;
private String name = null;
private String price = null;
private Date created = null;
// String Representation:
@Override
public String toString() {
return name;
// I want to return name and price
}
我想在 toString 函数中返回 name
和 price
?在 toString 函数中返回多个字符串是正确的。如果您在其他实体的 ManyToMany 中建立关系?
请建议我是否做对或错误,因为我想在我建立关系的其他实体中显示这些字段。
谢谢!
最佳答案
通常,toString()
方法返回对象的字符串表示形式,而不是对象的成员本身。因此,如果您需要名称和价格的表示,您可以这样做
return "Name: " + name + ", Price: " + price;
如果您确实想接收成员 name
和 price
,您应该为它们生成 getter 并在调用者中使用它们。
另一种可能性是将两个字符串“包装”在某种数据类中。
关于java - toString函数返回两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33141724/