Ich habe dieses Java-Programm, in dem eine Liste der Personenklasse erstellt wird ... Das dateofBirth ist ein Objekt-Argument in der Personenklasse. Jetzt stehe ich vor einem Problem; Wie initialisiert man ein Objekt der Person in der Liste oder wie man das DateOfBirth-Objekt in Person's Constructor übergibt?Wie übergibt man ein Objekt als Argument in einem Konstruktor einer anderen Klasse?
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?
}
'Person Person = neue Person (" John ", 23, neue DateOfBirth (1990,10,10))' – passion
Passion sollten Sie Ihren Kommentar als Antwort hinzufügen –
übrigens, um das Alter zu vererben, wenn Sie DOB in haben dort .. berechnen Alter basierend auf DOB – Sanjeev