有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

为什么我的子类对象总是空的,即使我在Java中有一个合适的构造函数?

我有一个子类,它继承自一个超类,我注意到,如果我试图打印出来,这个子类的对象似乎是空的。 我以前从未遇到过这样的问题,非常感谢您的帮助

我有这样一个子类:

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



    public OnetimeAppointment(int year, int month, int day, String description)
    {
        super();
        System.out.println(this.year);
        System.out.println(year);
        this.year = year;
        this.month = month; 
        this.day = day;
        this.description = description;
        System.out.println(this.year);
    
        }

    public int getYear() 
    {
        return year;
    }
    public int getMonth() 
    {
        return month;
    }


    @Override
    public boolean occursOn(int year, int month, int day) {
        // TODO Auto-generated method stub
        return false;
    
    }

    @Override
    public boolean equals(Object otherObject)
    {
        return false;
    }

}
}

下面是超类:

public abstract class Appointment {
    private String description;

    
    public Appointment() {
        description = "";
    }
    
    public abstract boolean equals(Object otherObject);
    
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getDescription() {
        return description;
    }

    
    public abstract boolean occursOn(int year, int month, int day);

    
    public String toString() {
        return description;
    }
    
    public static void main(String args[])
    {
        Appointment app = new OnetimeAppointment(2020, 11, 1, "Dentist");
        System.out.println(app);

}

共 (0) 个答案