码迷,mamicode.com
首页 > 编程语言 > 详细

java之 ------ 类的封装、继承和多态(一)

时间:2015-05-02 13:57:13      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:

问题:① 完善set()方法,保证获得正确的日期值(合法性)

② 将tomorrow()yestoday()方法合并为以下daysAfter()方法,并增加一些方法:

public int getWeek()   //返回当前日期对应的星期几,范围为06

public String toWeekString()   //返回当前日期对应星期几的中文字符串

public boolean before(MyDate d)  //判断是否在指定日期之前

public int daysBetween(MyDate d) //返回当前日期与日期d之间相距的天数

public boolean equals(Object obj)  //比较两个对象是否相等


import java.util.Calendar;
import java.util.Scanner;
public class MyDate2 { 
    int year,month,day;    
    boolean isCorrect=true;
    private static int thisYear;  
    static {
       thisYear=Calendar.getInstance().get(Calendar.YEAR); //获得当前日期对象中的年份值
    }
    public MyDate2(int year, int month, int day) {
        this.set(year, month, day);  
    } 
    public MyDate2()  {
        this(1970,1,1);                          //调用本类已声明的其他构造方法
    } 
    public MyDate2(MyDate2 d)  {
    	   this.set(d);
    }
    public void set(int y, int m, int d) {
    	if(y<1){
    		System.out.println("Input Year Is Error !!!");
    		this.isCorrect=false;
    		return ;
    	}if(m<1||m>12){
    		System.out.println("Input Month Is Error !!!");
    		this.isCorrect=false;
    		return ;
    	}
    	if(day<=0 || day>daysOfMonth() ){
    		System.out.println("Input Day Is Error !!!");
    		this.isCorrect=false;
    		return ;
    	}
        this.year = y;                         
        this.month = m;
        this.day = d ;
    } 
    public void set(MyDate2 d) {
        set(d.year, d.month, d.day);             //调用同名成员方法,不能使用this()
    } 
    public String toString(){
        return this.year+"年"+this.month+"月"+this.day+"日";
    }
    public static int getThisYear(){
        return thisYear;                         //访问静态成员变量
    }
    public static boolean isLeapYear(int year)  {
        return year%400==0 || year%100!=0 && year%4==0;
    }
    public boolean isLeapYear() {
        return isLeapYear(this.year);            //调用静态方法
    }        
    public boolean equals(MyDate2 d)  {                                            //this指代调用本

方法的当前对象
        return this==d || d!=null && this.year==d.year && this.month==d.month && this.day==d.day;
    } 
    public boolean equals(Object obj)  {
        if (this==obj)                           //this指代调用当前方法的对象
            return true;
        if (obj instanceof MyDate2) {
            MyDate2 d = (MyDate2)obj;              //类型强制转换
            return this.year==d.year && this.month==d.month && this.day==d.day;
        }
        return false;
    }    
    public static int daysOfMonth(int year, int month) {
        switch (month)                           //计算每月的天数
        {
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:  return 31; 
            case 4: case 6: case 9: case 11:  return 30;
            case 2:  return isLeapYear(year)?29:28;
            default: return 0;
        }
    }
    public int daysOfMonth()     {
        return daysOfMonth(this.year, this.month);
    }    
    public void tomorrow() {
        this.day++;                              //通过this改变当前对象的值,没有返回值
        if (day>this.daysOfMonth()){
            day=1;
            month++;
            if (month>12)
            {
                month=1;
                year++;
            }
        }
    }
    public MyDate2 yestoday()     {
        MyDate2 yes=new MyDate2(this);             //执行拷贝构造方法,创建新实例,没有改变当前对象

this
        yes.day--;
        if (yes.day==0) {
            yes.month--;
            if (yes.month==0){
                yes.month=12;
                yes.year--;
            }
            yes.day = daysOfMonth(yes.year, yes.month);
        }
        return yes;                              //返回对象引用
    }
    public MyDate2 dayAfter(int n)     {
        MyDate2 yes=new MyDate2(this);             //执行拷贝构造方法,创建新实例,没有改变当前对象

this
        yes.day+=n;
        if (yes.day==0) {
            yes.month--;
            if (yes.month==0){
                yes.month=12;
                yes.year--;
            }
            yes.day = daysOfMonth(yes.year, yes.month);
        	//System.out.println("**"+yes.year+" "+yes.month+" "+yes.day);
        	return yes;
        }
         if (yes.day>this.daysOfMonth()){
            yes.day=1;
            yes.month++;
            if (yes.month>12)
            {
                yes.month=1;
                yes.year++;
            }
        }
        return yes;                              //返回对象引用
    }
    public int getSumDays(MyDate2 d){
    	int  sum=0;
    	for(int i=1;i<d.year;i++){
    		sum+=d.isLeapYear(i) ? 366 : 365;
    	}
    	for(int i=1;i<d.month;i++){
    		sum+=d.daysOfMonth(d.year,i);
    	}
    	sum+=d.day;
    	return sum;
    }
    public int getWeek(){
    	long sum=0;
    	int week;
    	for(int i=1;i<this.year;i++){
    		sum+=this.isLeapYear(i) ? 366 : 365;
    	}
    	for(int i=1;i<this.month;i++){
    		sum+=this.daysOfMonth(this.year,i);
    	}
    	sum+=this.day;
    	week=(int)sum%7;
    	return week;
    }
    public String toWeekString(){
    	switch (getWeek()){
    		case 0 : return "Sunday" ;
    		case 1 : return "Monday" ;
    		case 2 : return "Tuesay" ;
    		case 3 : return "Wednesday" ;
    		case 4 : return "Thursday" ;
    		case 5 : return "Friday" ;
    		default : return "Saturday" ;
    	}
    }
    public boolean before(MyDate2 d){
    	if(this.year<d.year){
    		return true;
    	}else if(this.year==d.year){
    		if(this.month<d.month){
    			return true;
    		}else if(this.month==d.month){
    			if(this.day<d.day){
    				return true;
    			}else{
    				return false;
    			}
    		}else{
    			return false;
    		}
    	}else{
    		return false;
    	}
    }
    public int daysBetween(MyDate2 d){
    	int a=getSumDays(d);
    	int b=getSumDays(this);
    	return Math.abs(a-b);
    }
}
class MyDate_ex
{
	public static void main(String args[])
    {
    	Scanner sc=new Scanner(System.in);
	   	System.out.println("Please input date : ");
    	while(sc.hasNext()){
	        MyDate2 d= new MyDate2(sc.nextInt(),sc.nextInt(),sc.nextInt()); 
	        if(d.isCorrect){
	        	System.out.println("If you want to konw yestoday date,please input -1 ");
	        	System.out.println("or tomorrow date,please input 1");
	        	System.out.println(d.dayAfter(sc.nextInt()));
	        	System.out.println("Week : "+d.getWeek()+" "+d.toWeekString());
	        	System.out.println("Input a cmparative date : ");
	        	MyDate2 r=new MyDate2(sc.nextInt(),sc.nextInt(),sc.nextInt());
	        	System.out.println(d.before(r));
	        	System.out.println("Date_d between Date_r have "+d.daysBetween(r)+" days");
	        }
	        System.out.println();
	   		System.out.println("Please input date : ");
    	}
    }
}




  

java之 ------ 类的封装、继承和多态(一)

标签:

原文地址:http://blog.csdn.net/u011479875/article/details/45438691

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!