简体   繁体   English

父类的重写方法

[英]Overriding method from parent class

Trying to access the isLeapYear method in my Date parent class.尝试访问我的 Date 父类中的 isLeapYear 方法。 I have already defined the method in the JulianDate child class.我已经在 J​​ulianDate 子类中定义了该方法。 How do I correctly implement the child class to go into for the correct method?如何正确实现子类以使用正确的方法?

JulianDate (child class): JulianDate(子类):

class JulianDate extends Date {

    public JulianDate() {
        super(1, 1, 1);
        addDays(719164);
        addDays((int) ((System.currentTimeMillis() + java.util.TimeZone.getDefault().getRawOffset()) / 1000 / 60 / 60 / 24));
    }
    public JulianDate (int y, int m, int d) {
        super(y, m, d);
    }
    public boolean isLeapYear(int year) {
        return year % 4 == 0;
    }

This is what I have come up with in my parent class (Date) regarding isLeapYear:这是我在父类(Date)中提出的关于 isLeapYear 的内容:

Date JDate = new JulianDate();
        JulianDate.isLeapYear();

    public boolean isLeapYear() {
        return isLeapYear(this.year);
    }

What am I missing in my parent and child class to make this work?我在父子类中缺少什么来完成这项工作? I want the child class to calculate leap year and override the parent method.我希望子类计算闰年并覆盖父方法。

A child class will inherit all of its parent class's methods.子类将继承其父类的所有方法。 You don't need to define them again in the child class.您不需要在子类中再次定义它们。 So if you want the parent class( Date ) to check leap year, then define the method in Date class, and remove the isLeapYear method from JulianDate .因此,如果您希望父类( Date )检查闰年,则在Date类中定义该方法,并从JulianDate删除isLeapYear方法。 If you want the child class to check it, you need to override method of the parent class.如果你想让子类检查它,你需要覆盖父类的方法。

Is the last piece of code from selfmade Date class?, if so it doesn't seem to be defined correctly.是自制Date类的最后一段代码吗?如果是这样,它似乎没有正确定义。 Its missing a constructor and for some reason have a method call, which it shouldn't.它缺少一个构造函数,并且由于某种原因有一个方法调用,它不应该。


Added after comment评论后添加

class Parent {
int number;

    public Parent (int number) {
    this.number = number;
    }
    public int getNumber ()  {
    return number;
    }
}

class Child extends Parent {
super() {
}
@override
public int getNumber () {
return this.number;
}

}


Child demo = new Child(10);
demo.getNumber(); //uses the child method getNumber.

however i would just drop the getNUmber(or leapyear in your case) from the parent and only have a function for it in the child class.但是,我只会从父类中删除 getNUMber(或在您的情况下为闰年),并且在子类中只有一个函数。 By putting the same function into both classes you are duplicating code which isnt good practice.通过将相同的函数放入两个类中,您正在复制代码,这不是好的做法。

class JulianDate extends Date { class JulianDate 扩展日期 {

public JulianDate(){
    super(1, 1, 1);
}

public JulianDate(int y, int m, int d) {
    super(y,m,d);
}

public boolean isLeapYear() {
    boolean leap = false;
    if(getYear() % 4 == 0)
    {
        if( getYear() % 100 == 0)
        {
            // getYear() is divisible by 400, hence the getYear() is a leap getYear()
            if ( getYear() % 400 == 0)
                leap = true;
            else
                leap = false;
        }
        else
            leap = true;
    }
    else
        leap = false;

    return leap;
}

public boolean isLeapYear(int y) {
    this.setYear(y);


      return  this.isLeapYear();
   }
    public static void main(String [] ar){

    JulianDate julianDate= new  JulianDate(1988, 02, 21);

    System.out.println(julianDate.isLeapYear());

    JulianDate julianDate2= new  JulianDate();

    System.out.println(julianDate2.isLeapYear(1988));


 }
}

Hope this will help you.希望这会帮助你。

class JulianDate extends Date {

 public JulianDate() {}

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

and you can use it like,你可以像这样使用它

Date date = new JulianDate();
boolean isLeapYear = date.isLeapYear(2020);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM