【问题标题】:How to pass an object as an argument in a constructor of another class?如何在另一个类的构造函数中将对象作为参数传递?
【发布时间】:2016-09-27 09:12:33
【问题描述】:

我有这个 java 程序,其中创建了一个人员类列表... dateofBirth 是人员类中的一个对象参数。现在我面临一个问题;如何初始化列表中的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?     

}

【问题讨论】:

  • Person person=new Person("John",23,new DateOfBirth(1990,10,10))
  • 热情,您应该添加您的评论作为答案
  • 顺便说一句,当你有 DOB 时为什么要通过年龄.. 根据 DOB 计算年龄
  • 顺便说一句,人也是在 30 日和 31 日出生的 :)
  • @Nevado 然而,你必须承认这段代码不会有闰年的问题。这是大多数“自己做”约会内容的一个非常常见的问题。 :P

标签: java oop constructor


【解决方案1】:

先创建一个日期,然后将其作为参数传递

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

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

}

希望这会有所帮助。

【讨论】:

  • 你搞错了,Person person=new Person("John", 23, date);但是+1 :)
【解决方案2】:

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

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

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

}

【讨论】:

  • @TeunvanderWijst 更快,抱歉
【解决方案3】:

作为@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);

【讨论】:

    【解决方案4】:

    如果该 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 这样的类名。 list 是集合,MyList 是 ???新的收藏类型?它有什么? 尝试命名,即使不知道程序在做什么的人也能理解其目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多