Java 如何将对象作为参数传递给另一个类的构造函数?

Java 如何将对象作为参数传递给另一个类的构造函数?,java,oop,constructor,Java,Oop,Constructor,我有一个java程序,其中创建了person类的列表。。。dateofBirth是person类中的对象参数。现在我面临一个问题;如何初始化列表中person的对象,或者如何将DateOfBirth对象传递给person的构造函数 class DateOfBirth{ private int year; private int month; private int day; DateOfBirth(){ this.year = 0;

我有一个java程序,其中创建了person类的列表。。。dateofBirth是person类中的对象参数。现在我面临一个问题;如何初始化列表中person的对象,或者如何将DateOfBirth对象传递给person的构造函数

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?     

}
类出生日期{
私人国际年;
私人整数月;
私人国际日;
出生日期(){
本年=0;
本月=0;
这一天=0;
}
出生日期(整数y、整数m、整数d){
如果(y>1900){
年份=y;
}
否则{
System.err.println(“年份太小太旧”);
}

如果(0先做一个日期,然后作为参数传递

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

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

}
公共类MyList{
ArrayList Personlist=新的ArrayList();
出生日期=新的出生日期(2000,1,1);
个人=新人(“约翰”,16岁,日期);
}

希望这有帮助。

在需要的地方创建DateOfBirth对象,然后像这样简单地将其传递给构造函数

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

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

}
公共类MyList{
ArrayList Personlist=新的ArrayList();
出生日期dob=新的出生日期(1992年2月3日);
个人=新人(“约翰”,23岁,出生日期);
}

除了@TeunVanDerWijst的答案之外,您还可以在类
Person
中创建另一个构造函数,该构造函数将在
Person
类本身中创建实例

public Person(String name, int age, int y, int m, int d) {
    this(name, age, new DateOfBirth(y, m, d));
}
this
将只调用另一个构造函数,然后该构造函数将分配新生成的
DateOfBirth
实例

现在,只需将年、月和日传递为
int
,即可创建
Person
的实例

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

若dateofbirth属于您不需要创建新dateofbirth对象的人

做到简单:

Person person = new Person("John",23,new DateOfBirth(1985,5,5));
或者甚至像:

try
{
    Personlist.Add(new Person("John",23,new DateOfBirth(1985,5,5)));
}
catch(Exception ex)
{
   //
}
建议不要使用MyList之类的类名。 列表是集合,我的列表是???新集合类型?它有什么??
试着说出一些名字,即使有人不知道程序在做什么,也能理解其目的。

Person-Person=new-Person(“约翰”,23岁,新出生日期(1990,10,10))
Passion你应该添加你的评论作为答案顺便问一下,如果你有DOB,为什么要通过年龄。根据DOBbtw计算年龄,人们也出生在30和31岁:)@Nevado但是,你必须承认,这段代码在闰年不会有问题。这是大多数“自己动手”的常见问题约会材料:Pyou犯了一个错误,Person-Person=new-Person(“约翰”,23岁,约会);但是+1:)@TeunvanderWijst更快,对不起