第六天错误记录

题目

//定义一个日期MyDate类。属性:
//year:年month:月day:日
//
//构造方法:
//满参构造方法成员方法:
//get/set方法
//showDate方法:打印日期。
//isBi方法:判断当前日期是否是闰年

package Test02;
//定义一个日期MyDate类。属性:
//year:年month:月day:日
//
//构造方法:
//满参构造方法成员方法:
//get/set方法
//showDate方法:打印日期。
//isBi方法:判断当前日期是否是闰年
public class MyDate {
	private  int year;
	private int month;
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	private int day;
	//定义满参构造方法
	public MyDate(int year,int month,int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
	//定义showDate方法,打印日期
	public static void showDate(int year,int month,int day) {
		System.out.println("日期:"+year+"年"+month+"月"+day+"日");
	}
	//定义isBi方法,判断当前日期是否是闰年
	public static void isBi(int year) {
		if ((year/4==0&&year/4!=0) ||(year/100==0&&year/400!=0)) {
			System.out.println(year+"是闰年");
		}else {
			System.out.println(year+"不是闰年");
		}
	}
}

package Test02;

public class MyDateTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyDate myDate = new MyDate(1900, 1, 1);
		
		myDate.showDate(190011);
		myDate.isBi(1900);
	}

}

该代码也可执行,
但是出现的问题是static为静态的
这样定义后会使new出的对象没有意义
直接执行的是showDate和IsBi方法,该方法采用直接对year,month,day进行赋值的原理

正确代码如下`

package Test02;
//定义一个日期MyDate类。属性:
//year:年month:月day:日
//
//构造方法:
//满参构造方法成员方法:
//get/set方法
//showDate方法:打印日期。
//isBi方法:判断当前日期是否是闰年
public class MyDate {
	private  int year;
	private int month;
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	private int day;
	//定义满参构造方法
	public MyDate(int year,int month,int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
	//定义showDate方法,打印日期
	public  void showDate() {
		System.out.println("日期:"+year+"年"+month+"月"+day+"日");
	}
	//定义isBi方法,判断当前日期是否是闰年
	public  void isBi() {
		if ((year/4==0&&year/4!=0) ||(year/100==0&&year/400!=0)) {
			System.out.println(year+"是闰年");
		}else {
			System.out.println(year+"不是闰年");
		}
	}
}

package Test02;

public class MyDateTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyDate myDate = new MyDate(1900, 1, 1);
		
		myDate.showDate();
		myDate.isBi();
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值