简体   繁体   中英

Can you combine objects created in two separate classes to create a third object in a third class?

I have a Name object created in a Name class and an Address object created in a Address class. Can I combine these two objects to create a MailingLabel Object in a MailingLabel class?

First class

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;
    }

Second class

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;

    }

The goal is for MailingLabel to print out a toString that looks like

            Millicent X Feniwick

            1600 Huckleberry Lane

            Chelsea, NJ 15483
public class MailingLabel{
    private Address addressname;
    private Name theName;

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

Based on what you asked, I guess this is what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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