Netbeans抛出关于超级构造函数的错误

时间:2013-08-27 02:10:38

标签: java inheritance netbeans constructor

我正在制作一个学生数据库应用程序,它有不同类型的学生来练习继承并体验其他OO原则。

我有一些课程继承自超级学生"类。问题是我无法让子类重载构造函数。

Netbeans显然可以使用Student构造函数,但它在子类中告诉我,在Superclass中找不到合适的构造函数。仅此一点我就完全糊涂了。

到目前为止,这是代码:

class MyDB {

    public abstract class Student {
        //Variable declaration
        //
        public String firstName;
        public String lastName;
        public int studentID;
        public float gpa;
        public String status;
        public String mentor;

        //Constructor & overloaded constructors
        //
        public Student(String fn, String ln, int ID, float sgpa, String stat, String m) {
            firstName = fn;
            lastName = ln;
            studentID = ID;
            gpa = sgpa;
            status = stat;
            mentor = m;
        }

        public Student(String fn, String ln) {
            this(fn, ln, 0, 0, "", "");
        }

        //Class Methods
        //
        public abstract float calculateTuition(String status, float numberOfCreditHours);
                        //Tuition is Calculated
                /*
            if(status == "resident") {
                if(undergraduate) {
                  if(tuition) {
                        return numberOfCreditHours * 200;
                    } else {
                        return numberOfCreditHours * 250;
                    }
                } else {
                    return numberOfCreditHours * 300;
                }
            } else {
                if(undergraduate) {
                    if(tuition) {
                        return numberOfCreditHours * 400;
                    } else {
                        return numberOfCreditHours * 450;
                    }   
                } else {
                        return numberOfCreditHours * 350;
                }
            }
        */


                public abstract void update();
            //Database entry is updated
        public abstract void add();
            //Database entry for student is created
        public abstract void delete();
            //Database entry for student is deleted
        public abstract void query();
            //Query to DB is made, then info is returned to the screen
    }

    public class Undergraduate extends Student {
        private String level;
        private final String m;

        public String getLevel() {
            return level;
        }
        public void setLevel(String newLevel) {
            level = newLevel;
        }

                //Overloaded Constructor
        public Undergraduate(String newlevel, String fn, String ln, int ID, float sgpa, String stat, String m) {
        }

        @Override
        public float calculateTuition(String status, float numberOfCreditHours) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void update() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void add() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void delete() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void query() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }

    public class Graduate extends Student {
        private String thesisTitle;
        private String thesisAdvisor;

        public String getThesisTitle() {
            return thesisTitle;
        }
        public String getThesisAdvisor() {
            return thesisAdvisor;
        }
        public void setThesisTitle(String newThesisTitle) {
            thesisTitle = newThesisTitle;
        }
        public void setThesisAdvisor(String newThesisAdvisor) {
            thesisAdvisor = newThesisAdvisor;
        }

                //Overloaded Constructor

        @Override
        public float calculateTuition(String status, float numberOfCreditHours) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void update() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void add() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void delete() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void query() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }

    public class PartTime extends Student {
        private String company;
                    //Name of sponsoring employer

        public String getCompany() {
            return company;
        }
        public void setCompany(String newCompany) {
            company = newCompany;
        }

                //Overloaded Constructor

        @Override
        public float calculateTuition(String status, float numberOfCreditHours) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void update() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void add() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void delete() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void query() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }




    public static void main(String[] args) {
    }
}

请忽略覆盖语句。他们看起来很乱,因为我让netbeans auto做任何想要摆脱错误的事情。

应用程序还有很多工作要做,但目前问题在于构造函数。

我检查过的教程和问题通常涉及同一类的重写构造函数,或者不会覆盖构造函数的子类。

1 个答案:

答案 0 :(得分:2)

您的子类必须调用其父构造函数之一。当类具有默认(无参数)构造函数时,这是隐式完成的。由于Student没有no-arg构造函数,因此从它扩展的任何内容必须在其构造函数中调用其构造函数之一,例如

public class Undergraduate extends Student {
    public Undergraduate() {
        super(null, null);
    }
    public Undergraduate(String fn, String ln) {
        super(fn, ln);
    }
    //...
}

否则,只需向Student添加默认构造函数:

protected Student() {
    this(null, null);
}