gpt4 book ai didi

java - 如何使用 toString 方法打印第三个对象中的两个对象?

转载 作者:行者123 更新时间:2023-12-02 01:10:47 24 4
gpt4 key购买 nike

我在作业时遇到问题,因为我对 java 对象的理解有缺陷。我创建了 3 个类。第一个类创建一个对象来保存组成全名的值,第二个类是 MailingAddress 类,它扩展 FullName 类并通过 super() 方法利用继承。第三类是运输标签类,它需要 2 个 MailingAddress 对象...一个对象表示为 ShipTo,另一个表示为 ShipFrom,并具有单一方法 printLabel()。所有对象都会重写 toString() 方法进行输出,下面是我的代码和输出:

public class FullName {

private String title;
private String firstName;
private String middleName;
private String lastName;

FullName(){
title = "";
firstName = "";
middleName = "";
lastName = "";
}
FullName(String title, String first, String middle, String last){
super();
this.title = title;
this.firstName = first;
this.middleName = middle;
this.lastName = last;
}
public String toString(){
return title + ". " + firstName + " " + middleName + " " + lastName + "\n";
}
}// end of class
public class MailingAddress extends FullName{

private String streetAddress, city, province, postal;
private FullName fn = new FullName();

MailingAddress(){
super("","","","");
streetAddress = "";
city = "";
province = "";
postal = "";
}

MailingAddress(String title, String first, String middle, String last, String street, String city, String prov, String postal){
super(title, first, middle, last);
this.streetAddress = street;
this.city = city;
this.province = prov;
this.postal = postal;
}

public String toString(){
return fn.toString() + " " + streetAddress + "\n" + city + ", " + province + "\n" + postal;
}
}// end of class
public class ShippingLabel extends MailingAddress{
private MailingAddress shipTo;
private MailingAddress shipFrom;

private ShippingLabel(MailingAddress shTo, MailingAddress shipFm){
this.shipTo = shTo;
this.shipFrom = shipFm;
}
private void printLabel(){
ShippingLabel label = new ShippingLabel(shipTo, shipFrom);
System.out.println(label.toString());
}
public String toString(){
return "Ship to:" + shipTo + "\n" + "\n" + "Ship From: " + shipFrom;
}
public static void main(String[] args){
MailingAddress shTo = new MailingAddress("Mr","Jonathan","Daniel", "O'Connor","109732","Edmonton", "Alberta", "T5m1K3");
MailingAddress shipFm = new MailingAddress("Mr","Piercy","Michael","Miller","23 Oreville way","Calgary","Alberta","C3p5o1");
ShippingLabel sL = new ShippingLabel(shTo,shipFm);
sL.printLabel();
} // end of main
}// end of class

输出:

运送至:。
109732艾伯塔省埃德蒙顿T5m1K3

发货地点: .
23 奥雷维尔路艾伯塔省卡尔加里C3p5o1

进程已完成,退出代码为 0

我不明白的是为什么使用 FullName 构造函数的值没有打印在输出中。我希望类 FullName 和 MailingAddress 中的 toString 方法能够协作组成 ShippingLabel。希望通过查看我的代码,您可以了解我的思维过程,并解释它的缺陷所在。

最佳答案

问题出在您的 MailingAddress.toString() 函数中。

您没有使用正确的对象来调用toString()函数。由于 MailingAddress 是从 FullName 继承的,因此您不需要创建 FullName 类型的新成员。您应该使用 super 关键字继承 FullNametoString()

以下是更正后的版本。请参阅the complete code working here :

public String toString()
{
return super.toString() + " " + streetAddress + "\n" + city + ", " +
province + "\n" + postal;
}

[注意:MailingAddressFullName 之间的关系是 has-a 而不是 is a MailingAddress 有一个 FullNameMailingAddress 不是 FullName。上面的解决方案是使用 is-a 关系。请参阅此处 solution using has-a relationship 。 ]

关于java - 如何使用 toString 方法打印第三个对象中的两个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59433834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com