Inheritance 在子类中访问私有Int

Inheritance 在子类中访问私有Int,inheritance,subclass,private,Inheritance,Subclass,Private,超类源代码 public class Date { private int month; private int day; private int year; public Date() { setMonth(1); **day = 1;** setYear(1900); } public Date(int month, int day, int year) { this.setMonth(month); this.**day** = day; this.setYear(year);

超类源代码

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


public Date() {
setMonth(1);
**day = 1;**
setYear(1900);
}

public Date(int month, int day, int year) {
this.setMonth(month);
this.**day** = day;
this.setYear(year);
}
Month和Year可以正常工作,因为我可以在子类中使用setMonth和setYear。然而,当我尝试使用day时,它说var不可见,因为它是私有的。在超类中没有一天的二传者,但有一个获得者。二传手应该是什么样子?此外,我的子类构造函数应该是什么样子

子类构造函数

public EDate(int month, int day, int year) 
{

this.setMonth(month);
day = getDay();
this.setYear(year);
}
子类日期设定器

public void setDay(int newInt) {
if (isGooddDate(getMonth(), newInt, getYear())==true)
{    
newInt = this.getDay();
}

非常感谢您的帮助

我认为首先没有必要对Date类进行子类化。原因是,无论发生什么情况,日期功能都将保持不变。所以不需要子类构造函数

至于日期设定者,你正朝着正确的方向前进:

public void setDate(int dateValue) {
    if(isDateValid(dateValue)) {
        date = dateValue;
    } else {
        throw new Exception("Invalid date");
    }
}
如果您的类具有时间支持功能,您实际上可以编写更好的解决方案。无论日期是什么,都要及时转换日期值。存储时,将时间转换为适当的日期。这就是内置DateTime类的工作原理


例如:如果存储2006-16-80,它将存储为2007-06-19,而不是引发异常。只是一个想法

OO语言之间存在一些差异。你用的是哪一种?谢谢你的快速回复。我正在使用Java。