继承/覆盖指导?

时间:2015-03-17 16:56:31

标签: java inheritance

该计划是一个约会计划,每月课程需要做的是在每月,如果查询日期不早于约会日期并且匹配月份中的日期,那么则发生在该方法中应该回归真实。例如,假设你有一个月度约会,“去远足”,设置于2003年5月20日。对于查询日期,2012年5月20日,方法generateOn应该返回true。

我认为eventOn方法需要被覆盖,但我需要这里的指导,因为这是我做过的为数不多的继承程序之一。

目前我有3节课

import java.util.Scanner;

public class AppointmentDemo
{        
    public static void main(String[] args)
    {            
        Appointment[] appointments = new Appointment[2];
        appointments[0] = new Monthly(2003, 5, 20, "Go hiking.");
        appointments[1] = new Onetime(2012, 4, 2, "Dentist appointment.");        

        System.out.println("Enter a date (year month day) to list appointments: (e.g. 2000 8 13)");
        Scanner in = new Scanner(System.in);
        int year = in.nextInt();
        int month = in.nextInt();
        int day = in.nextInt();
        boolean found = false;
        for (int i = 0; i < appointments.length; i++)
        {
            if (appointments[i].occursOn(year, month, day))
            {
                found = true;
                System.out.println(appointments[i]);
            }
        }
        if(!found){
            System.out.println("No appointment found.");
        }
    }
}

哪个是主要类

超级班:

public class Appointment
{
    private String description;
    private int year;
    private int month;
    private int day;

    public Appointment(int year, int month, int day, String description)
    {
        this.year = year;
        this.month = month;
        this.day = day;
        this.description = description;
    }

    public int getYear() { return year; }

    /**
     Returns the month of the appointment
     @return the month
     */
    public int getMonth() { return month; }

    /**
     Returns the day of the appointment
     @return the day
     */
    public int getDay() { return day; }

    public boolean occursOn(int year, int month, int day)
    {
        return (year == this.year) && (month == this.month) && (day == this.day);
    }

    /**
     Converts appointment to string description
     */
    public String toString() { return description; }
}

和每月课程

public class Monthly extends Appointment {
    private int year;
    private int day;
    private int month;
    private String description;

    public Monthly(int year, int month, int day, String description) {
        super(year, month, day, description);

        // TODO Auto-generated constructor stub
    }           
}

1 个答案:

答案 0 :(得分:0)

occursOn中覆盖Monthly,以便它不会检查year