我的子类构造函数中的构造函数错误

兰迪·吉尔曼

这是作业:

使用以下准则设计和实现代表Person的类以及3个子类:

创建一个名为Person的类及其三个子类Employee,Student和Retired。

b。Person具有以下数据字段:name,year_of_birth,isStudying和isEmployed。它还具有用于设置和获取每个字段的值的方法,以及用于计算当前年龄并显示人员状态的方法。Person类中还包含将isStudying和isEmployed字段设置为false的构造函数。如果愿意,欢迎您添加其他数据字段和方法。

1.最后,创建一个Java测试类,该类使用您的Person类进行模拟。在测试类中,您至少应:a)构造一个Person的4个实例,b)打印实例的名称c)根据实例的年龄,isStudying和isEmployed属性值打印实例的状态。

public class Person2 {//begin class
    //declare variables
    String name;
    int year_of_birth;
    boolean isStudying;
    boolean isEmployed;
    int age;

public Person2(String name, int year_of_birth, boolean isEmployed, boolean isStudying, int age){//begin constructor
    this.name = name;
    this.year_of_birth = year_of_birth;
    this.isEmployed = false;
    this.isStudying = false;
    this.age = age;
}//end constructor

public int getYear(){//get year method
        return year_of_birth;
}//end method

public String getName(){//get name method
        return name;
}//end method

public boolean getEmployed(){//get employed method
        return isEmployed;
}//end method

public boolean getStudying(){//get employed method
        return isStudying;
    }//end method

public int getAge(){//get year method
        age = 2014 - year_of_birth;
    return age;
}//end method

public void setName(String name){//set name method
        this.name = name;
}//end method

public void setYear (int year){//set year method
        this.year_of_birth = year;
}//end method

public void setEmployed(boolean employed){//set employed method
        this.isEmployed = employed;
}//end method

public void setAge (int age){//set year method
        this.age = age;
}//end method

public static void main(String[] args) {
        // TODO code application logic here
}

}

class Student extends Person2 {//begin class

    public Student(String name, int year_of_birth, boolean isEmployed, boolean isStudying, int age){//begin constructor
        this.name = name;
        this.year_of_birth = year_of_birth;
        this.isEmployed = isEmployed;
        this.isStudying = isStudying;
        this.age = age;
}//end constructor)

    @Override
    public int getYear(){//get year method
        return year_of_birth;
    }//end method

    @Override
    public String getName(){//get name method
        return name;
    }//end method

    @Override
    public boolean getEmployed(){//get employed method
        return isEmployed = false;
    }//end method

    @Override
    public boolean getStudying(){//get employed method
        return isStudying = true;
    }//end method

    @Override
    public int getAge(){//get year method
    age = 2014 - year_of_birth;
    if (age > 30){
        System.out.println("Person is not a student");
    }
    return age;
}//end method

}

这段代码显然是不完整的,我对此构造函数错误挂了起来。它说“实际和形式上的论据的长度不同”。

杰米·科伯恩(Jamie Cockburn)

您的问题是您没有调用超级构造函数Person2中的构造函数Student正在尝试调用默认的构造函数(不带参数),该默认构造函数Person2不存在。

您在中只有一个构造函数Person2,应该从的构造函数中调用该构造函数Student

public Student(String name, int year_of_birth, boolean isEmployed, boolean isStudying, int age) {
    super(name, year_of_birth, isEmployed, isStudying, age);
}

这也意味着您不需要重复所有初始化代码两次。

你也应该不会被重复所有的方法Student是在Person2如果将它们取出,Student无论如何都将继承它们。这就是扩展课程的重点。如果您不想要继承的行为,而是想要某些Student特定的行为,则仅应覆盖类似的方法

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章