繁体   English   中英

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

[英]How to use toString method to print two objects within a third object?

我在分配中遇到了我对 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 Oreville way 卡尔加里,艾伯塔省 C3p5o1

进程以退出代码 0 结束

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

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

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

以下为修正版。 请参阅此处的完整代码

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

[注意: MailingAddressFullName之间的关系是has-a而不是is aMailingAddressFullNameMailingAddress不是FullName 上面的解决方案是使用is-a关系。 请参阅此处使用has-a关系解决方案 ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM