java - toString函数返回两个字符串

标签 java spring-mvc

@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 函数中返回 nameprice ?在 toString 函数中返回多个字符串是正确的。如果您在其他实体的 ManyToMany 中建立关系?

请建议我是否做对或错误,因为我想在我建立关系的其他实体中显示这些字段。

谢谢!

最佳答案

通常,toString() 方法返回对象的字符串表示形式,而不是对象的成员本身。因此,如果您需要名称和价格的表示,您可以这样做

return "Name: " + name + ", Price: " + price;

如果您确实想接收成员 nameprice,您应该为它们生成 getter 并在调用者中使用它们。

另一种可能性是将两个字符串“包装”在某种数据类中。

关于java - toString函数返回两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33141724/

相关文章:

java.util.logging : file handler with append and count properties

java - 抽屉导航项目图标右侧

java - 使用 jaxb 将对象转换为 XML 的异常

java - Spring MVC : the best way to handle exception for Ajax Request and normal request?

java - Spring Security 使用 Web 服务进行验证并从数据库中检索角色

java - 如何将内容放入 HashMap 并使用具有相同字段值的新实例来获取相应的值

java - SpringBoot oracle.jdbc.OracleDatabaseException : ORA-00932: inconsistent datatypes: expected DATE got NUMBER

java - Spring:访问 JSP 下拉列表中的模型属性列表

java - Servlet session 超时

java - 如何在cardView上添加插页式广告?