简体   繁体   中英

How do you call a mutator from within a constructor in the same class?

For a homework assignment the instructions state (within Undergrad class):

You do NOT need to include a default constructor, but you must write a full parameterized constructor (it takes 4 arguments) -- this constructor calls the parent class parameterized constructor and the mutator for year level.

Because Undergrad extends Student, then Student is my parent class, right? I just can't quite figure out how I'm to use my year level mutator (which is just the simplest of methods) to assign my "year" attribute.

    public void setYear(int inYear)
{
    year = inYear;
}

public Student(String inName, String inID, int inCredits)
{
    name = inName;
    id = inID;
    credits = inCredits;
}

    public Undergrad(String inName, String inID, int inCredits,int inYear)
{
    super(inName, inID, inCredits);
    year = inYear;
}

I keep missing assignments because I spend too much time on these small specific points of the homework so just asking for a little help. I swear it's the wording that throws me off on these assignments almost as often as just learning the material itself.

I believe they want you to use the setter in order to set the year instead of setting the year directly.

public Undergrad(String inName, String inID, int inCredits,int inYear)
{
    super(inName, inID, inCredits);
    setYear(inYear);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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