如何在没有抽象的情况下将正确的 isLeapYear 函数添加到我的日期类中? (Java)

问题描述 投票:0回答:1

您好,我正在做一项家庭作业,作业是创建一个日期类,并将公历日期和儒略日期作为子类。我正在尝试编写一个正确的 isLeapYear() 函数,因为两个日历计算闰年的方式不同。此代码不会运行,因为调用闰年函数的 return 语句中的参数不需要参数,即使它调用需要参数的公共类。如果有人可以向我展示在不使用抽象的情况下执行此操作的正确方法(因为我们尚未涵盖它),我将很高兴。当你也在做的时候,请随时帮助修复其他问题,哈哈

日期.java

public class Date {
    private int year;
    private int day;
    private int month;

    Date(){

    }

    Date(int year, int day, int month){
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear(){
        return this.year;
    }

    public int getMonth(){
        return this.month;
    }

    public int getDayOfMonth() {
        return this.day;
    }

    public String getMonthName(){
        return getMonthName(month);
    }

    public void printShortDate(){
        System.out.println(month + "/" + day + "/" + year);
    }

    public void printLongDate(){
        System.out.println(getMonthName(month - 1) + " " + day + ", " + year);
    }

    #code that wont work
    public boolean isLeapYear(){
        return isLeapYear(this.year);
    }

    private int getNumberOfDaysInMonth(int year, int month){
        int[] numberOfDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(month == 2 && isLeapYear()){
            return 29;
        } else {
            return numberOfDays[month - 1];
        }
    }

//    private int getNumberOfDaysInYear(int year){
//        if (isLeapYear(year)) {
//            return 366;
//        } else {
//            return 365;
//        }
//    }

    private String getMonthName(int month){
        String[] monthNames = {"January", "February", "March", "May", "June",
                "July", "August", "September", "October", "November", "December"};
        return monthNames[month - 1];
    }

    public void addDays(int days){
        while (days != 0){
            day++;
            if (day > getNumberOfDaysInMonth(year, month)){
                month++;
                day = 1;
                if(month > 12){
                    month = 1;
                    day = 1;
                    year++;
                }
            }
            days--;
        }
    }

    public void subtractDays(int days) {
        while (days != 0) {
            day--;
            if (day == 0) {
                if (month == 1) {
                    month = 12;
                    day = 31;
                    year--;
                } else {
                    month--;
                }
                day = getNumberOfDaysInMonth(year, month);
            }
            days--;
        }
    }
}

公历日期.java

public class GregorianDate extends Date {
    private int year;
    private int month;
    private int day;

    public GregorianDate(){
        this.year = 1970;
        this.day = 1;
        this.month = 1;
        addDays((int)((System.currentTimeMillis()+java.util.TimeZone.getDefault().getRawOffset())/86400000));
    }

    public GregorianDate(int year, int month, int day){
        super();
    }


    public boolean isLeapYear(int year){
        if(year % 4 == 0){
            return year % 100 != 0 || (year % 100 == 0 && year % 400 == 0);
        }
        return false;
    }

}

JulianDate.java

public class JulianDate extends Date{
    private int year;
    private int month;
    private int day;

    public JulianDate(){
        this.year = 1;
        this.day = 1;
        this.month = 1;
        addDays(719164);
        addDays((int)((System.currentTimeMillis()+java.util.TimeZone.getDefault().getRawOffset())/86400000));
    }

    public JulianDate(int year, int month, int day){
        super();
    }

    public boolean isLeapYear(int year){
        return year % 4 == 0;
    }

}

我尝试将这些功能放置在任何地方,看看是否有任何功能可以工作,但也许我遗漏了一些东西。

java oop inheritance polymorphism subclass
1个回答
0
投票

您可以在基类中声明

isLeapYear(int year)
方法,而无需在那里提供其实际实现。例如,通过抛出异常来指示它需要在子类中重写或提供默认实现。比如:

public class Date {
    // rest of your code
    // Declare a method with default behavior that forces subclasses to override it
    public boolean isLeapYear(int year) {
        throw new UnsupportedOperationException("isLeapYear() must be implemented in subclasses");
    }
}

并在子类中实现它

public class GregorianDate extends Date {
    // rest of your code
    @Override
    public boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
}

public class JulianDate extends Date {
    // rest of your code
    @Override
    public boolean isLeapYear(int year) {
        return year % 4 == 0;
    }
}

如果您不想引发异常,您可以在

Date
类中提供默认实现

public class Date {
   // rest of your code

    public boolean isLeapYear(int year) {
    //simple fallback rule or  a default implementation
        return false;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.