为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过对象将日期值发送到构造函数以及如何获取该日期值?

如何通过对象将日期值发送到构造函数以及如何获取该日期值?

蓝山帝景 2022-09-14 15:37:26
当我想要编译下面的代码编译时错误说:构造函数 Person(字符串,int) 是未定义的,等等...人员 p = 新人(“阿比”,2/4/2019);我写了这段代码:import java.util.Date;class Person{    String name;    Date DOB;    public Person(String name, Date dOB) {        this.name = name;        DOB = dOB;    }    public String getName() {        return name;    }    public Date getDOB() {        return DOB;    }}class Student extends Person{    int StudentID;     public Student(String name, Date dOB, int studentID) {        super(name, dOB);        StudentID = studentID;    }    public int getStudentID() {        return StudentID;    }    public static void main(String[] args) {        Person p = new Person("abhi",2/4/2019); // i can't pass date values to constructor        student s = new Student("mark",2019-04-01,123456); //i can't pass date values to constructor        System.out.println("Name "+p.getName());        System.out.println("DOB  "+p.getDOB());        System.out.println("Name  "+s.getName());        System.out.println("DOB  "+s.getDOB());        System.out.println("StudID  "+s.getStudentID());    }}`
查看完整描述

1 回答

?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

在Java中,被解释为整数除法(2除以4除以2019),即0。但这绝对不是约会。2/4/2019

我建议不要使用对象,而是使用更易于使用的本地日期。然后,您的代码将变为:Date

Person p = new Person("abhi", LocalDate.of(2019, 4, 2));


查看完整回答
反对 回复 2022-09-14
  • 1 回答
  • 0 关注
  • 111 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号