可以用在哪里:
代码:
公共 班级 日期 {
私人 int年;
私人int月;
私人int日;
公共 日期(int年份,int月,int日 ){
这个.年=年;
这个.月=月;
这个.日=日;
}
公开 日期(){
/*今年.year = 1971;
这个月= 9;
这一天 = 1;*/
//上面的代码可以通过调用另一个构造函数来完成//但是前提是不能创建新的对象。以下代码表示创建了一个全新的对象
//需要使用如下语法完成构造方法的调用
//该方法不会创建新的java对象。但同时,你也可以调用其他构造方法
这个(1971,9,1);
}
公共 int getYear() {
回归年;
}
公共 void setYear(int年份) ){
//设置关卡(有时间可以设置关卡)
这个.年=年;
}
公共 int getMonth(){
回归月;
}
public void setMonth(int月) ){
//设置关卡(有时间可以设置关卡)
这个.月=月;
}
公共 int getDay() {
返回日;
}
公共 void setDay(int日){//设置关卡(有时间可以设置关卡)
这个.日=日;
}
公共 void 打印(){
系统.输出.println(这个.年 + “年”+这个.月+“月”+这个.日 + "日");
}
}
公共 班级 日期测试{
公共 静态 void 主(Str) ing[] args) {
//创建日期对象1
日期时间1 = 新 日期();
时间1.打印();
//创建日期对象2
日期时间2 = 新 日期(2022,8,1);
时间2.打印();
}
}