您可以组合在两个单独的类中创建的对象以在第三类中创建第三个对象吗?

时间:2019-03-01 00:05:35

标签: java

我在Name类中创建了Name对象,在Address类中创建了Address对象。我可以结合使用这两个对象在MailingLabel类中创建MailingLabel对象吗?

头等舱

public class Name 
{
    private String firstName;
    private char middleInitial;
    private String lastName;

    public Name (String fn, char mi, String ln)
    {
        firstName = fn;
        middleInitial = mi;
        lastName = ln;
    }

第二堂课

public class Address 
{
    private int streetNumber;
    private String streetName;
    private String streetType;
    private String city;
    private String state;
    private int zip;

    public Address (int snum, String sn, String st, String c, String s, int z)
    {
        streetNumber = snum;
        streetName = sn;
        streetType = st;
        city = c;
        state = s;
        zip = z;

    }

目标是MailingLabel打印出看起来像的toString

            Millicent X Feniwick

            1600 Huckleberry Lane

            Chelsea, NJ 15483

1 个答案:

答案 0 :(得分:0)

public class MailingLabel{
    private Address addressname;
    private Name theName;

    public MailingLabel(Address addressname, Name theName){
        this.addressname = addressname;
        this.theName = theName;
    }
}

根据您的要求,我想这就是您所需要的。