Person.java
public class Person {
public String firstname;
public String lastname;
public Date dob;
public boolean sex;
public Person(String firstname, String lastname, Date dob, boolean sex){
this.firstname = firstname;
this.lastname = lastname;
this.dob = dob;
this.sex = sex;
}
public Person(String firstname, String lastname, Date dob, String s){
this.firstname = firstname;
this.lastname = lastname;
this.dob = dob;
if (s.charAt(0)=='f' || s.charAt(0)=='F') sex = true; else sex = false;
}
Date.java
public class Date {
public int day;
public int month;
public int year;
public Date(int day, int month, int year)
{
this.day = day;
this.month = month;
this.year = year;
}
}
为什么这是错的?如何正确创建对象?这是一篇论文,所以上面的类不能改变。
public static void main(String[] args) {
Person person1 = new Person("Adeline", "Wells", (12,4,1992), false);
}
答案 0 :(得分:1)
问题是你的字段日期进入人类类也是一个类,所以你这样做:
public static void main(String[] args) {
Date d = new Date(12,4,1992);
Person person1 = new Person("Aaron", "Wells", d, false);
}
否则,如果你想直接过去一个月和一年,你可以这样做:\ / p>
public Person(String firstname, String lastname, int d, int m, int y, boolean sex){
this.firstname = firstname;
this.lastname = lastname;
this.dob = new Date(d, m, y);
this.sex = sex;
}
现在你可以做到
public static void main(String[] args) {
Person person1 = new Person("Adeline", "Wells", 12,4,1992, false);
}
答案 1 :(得分:0)
您可以在以下一行中执行此操作:
public static void main(String[] args) {
Person person1 = new Person("Aaron", "Wells", new Date(12,4,1992), false);
}