简体   繁体   中英

How to pass an object as an argument in a constructor of another class?

I have this java program in which a list of person class is created... the dateofBirth is an object argument in person's class. Now i am facing a problem; how to initialize an object of person in list or how to pass the DateOfBirth object into Person's Constructor?

class DateOfBirth{
    private int year;
    private int month;
    private int day;

    DateOfBirth(){
        this.year = 0;
        this.month = 0;
        this.day = 0;
    }

    DateOfBirth(int y, int m, int d){
        if(y>1900){
            year = y;
        }
        else{
            System.err.println("the year is too small too old" );           
        }
        if(0<month && month<13){
            month = m;
        }
        else{
            System.err.println("month should be within 1 to 12.");
        }
        if(0<day && day<30){            
            day = d;
        }           

    }


    public int getYear() {
        return year;
    }

    public int getMonth(){
         return month;
     }
    public int getDay(){
        return day;
    }

}
class Person{
    private String name;
    private int age;
    private DateOfBirth Dob;

    public Person(String name, int age, DateOfBirth dob){
    this.name = name;
    this.age = age;
    this.Dob = dob;
    }

    public String getName() {
        return name;
    }

    public DateOfBirth getDob() {
        return Dob;
    }    

    public int getAge() {
        return age;
    }

}

public class MyList {
    ArrayList<Person> Personlist = new ArrayList<Person>();

    Person person=new Person("John",23,...) // how to pass the DateOfBirth object here?     

}

Make a date first and then pass that as parameter

 public class MyList {
        ArrayList<Person> Personlist = new ArrayList<Person>();

DateOfBirth date = new DateOfBirth(2000, 1, 1);
    Person person = new Person("John", 16, date);

}

hope this helps.

Make object of DateOfBirth where you need and then simply pass it to constructor like this

public class MyList {
        ArrayList<Person> Personlist = new ArrayList<Person>();

DateOfBirth dob= new DateOfBirth(1992, 2, 3);
    Person person=new Person("John",23,dob);

}

As an addition to the answer of @TeunVanDerWijst you could also create another constructor in the class Person which would create the instance in the Person class itself. The constructor could look like the following.

public Person(String name, int age, int y, int m, int d) {
    this(name, age, new DateOfBirth(y, m, d));
}

The this would just call the other constructor which would then assign the freshly generated DateOfBirth instance.

Now you could create an instance of Person by just passing the year, the month and the day as int .

 Person person=new Person("John", 23, 2000, 9, 12);

if that dateofbirth is belongs to person you didn't need to create new DateOfBirth object

do it simple:

Person person = new Person("John",23,new DateOfBirth(1985,5,5));

or even like:

try
{
    Personlist.Add(new Person("John",23,new DateOfBirth(1985,5,5)));
}
catch(Exception ex)
{
   //
}

for advice never use class names like MyList. list is collection, MyList is ??? new collection type ? what it has ?? try to make names that even if someone that didn't know what the program doing will understand the purpose.

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