public String toString() { return "OrderRecord idCode: " + idCode + "\n food:" + food.getName() + "\n price:" + food.getPrice() + " RMB\n customer:" + customer + "\n deliveryMan: " + deliveryMan.toString() + "\n creationDate:" + creationDate; }
时间: 2024-03-28 07:36:18 浏览: 21
这段代码是一个Java类OrderRecord的定义,其中包含了OrderRecord类的公有成员方法toString。
公有成员方法toString是一个重载了Object类的toString方法的方法,用于返回当前对象的字符串表示形式,一般用于输出对象的信息或调试。在该方法中,将订单记录的相关信息转换成一个字符串,包括订单编号、订购食品的名称和价格、订购客户信息、配送人员信息和订单创建时间。
其中,使用了food对象的getName方法和getPrice方法,分别用于获取订购食品的名称和价格。customer和deliveryMan对象都重载了toString方法,用于返回字符串形式的客户信息和配送人员信息。
该方法的实现非常简单,将订单记录的相关信息拼接成一个字符串,并返回该字符串。toString方法的重载是面向对象编程中的常见操作,可以方便地输出对象的信息,便于调试和测试程序。
相关问题
import java.util.Date; public class OrderRecord { public OrderRecord(String idCode, Food food, Customer customer, DeliveryMan deliveryMan, Date creationDate) { this.idCode = idCode; this.food = food; this.customer = customer; this.deliveryMan = deliveryMan; this.creationDate = creationDate; } private String idCode; private Food food; private Customer customer; private DeliveryMan deliveryMan; private Date creationDate; public String getIdCode() { return idCode; } public Food getFood() { return food; } public Customer getCustomer() { return customer; } public DeliveryMan getDeliveryMan() { return deliveryMan; } public Date getCreationDate() { return creationDate; } public void setIdCode(String idCode) { this.idCode = idCode; } public void setFood(Food food) { this.food = food; } public void setCustomer(Customer customer) { this.customer = customer; } public void setDeliveryMan(DeliveryMan deliveryMan) { this.deliveryMan = deliveryMan; } public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } public String toString() { return "OrderRecord idCode: " + idCode + "\n food:" + food.getName() + "\n price:" + food.getPrice() + " RMB\n customer:" + customer + "\n deliveryMan: " + deliveryMan.toString() + "\n creationDate:" + creationDate; } }
这是一个Java类,名为OrderRecord,包含了一些属性和方法。该类表示订单记录,记录了客户购买的食品、配送员、创建时间等信息。
其中,构造方法OrderRecord()用于初始化订单记录的各个属性,包括订单号、食品、顾客、配送员、创建时间。getIdCode()、getFood()、getCustomer()、getDeliveryMan()、getCreationDate()方法用于获取各个属性的值。setIdCode()、setFood()、setCustomer()、setDeliveryMan()、setCreationDate()方法用于设置各个属性的值。toString()方法用于返回该订单记录的字符串表示形式,包括订单号、食品名称、价格、顾客信息、配送员信息和创建时间。
总的来说,该类可以用于表示订单记录,并且提供了一些方法用于设置和获取订单信息。
public class Shop { public Shop(String name, String idCode) { this.name = name; this.idCode = idCode; } private String name; private String idCode; private FoodList foodList = new FoodList(); public String getName() { return name; } public String getIdCode() { return idCode; } public void setName(String name) { this.name = name; } public void setIdCode(String idCode) { this.idCode = idCode; } public FoodList getFoodList() { return foodList; } public void setFoodList(FoodList foodList) { this.foodList = foodList; } public String toString() { return " Restaurant name: " + name + ", shop idCode:" + idCode; } }
这是一个店铺类Shop,它拥有两个私有属性name、idCode,以及一个FoodList类型的属性foodList表示店铺中的食品列表。这个类的作用是为其他类提供一个店铺信息模板,其他类可以通过创建Shop类的实例,来表示不同的店铺。
这个类中有一个构造方法,可以初始化name和idCode属性,以及一个getter/setter方法用于获取和设置name、idCode、foodList属性。还有一个toString方法,用于返回店铺的名称和idCode属性值的字符串表示形式。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)