简体   繁体   中英

Adding in String and int to parent class with super.toString

Okay, so I have 4 different classes for my program, and I'm trying to add a new variable in one of my subclasses. I will show the code for all of the classes involved and then explain what exactly is wrong.

First Code is my Person Class:

class Person {
private String myName; // name of the person
private int myAge; // person's age
private String myGender; // 'M' for male, 'F' for female

// constructor
public Person(String name, int age, String gender) {
    myName = name;
    myAge = age;
    myGender = gender;
}

public String getName() {
    return myName;
}

public int getAge() {
    return myAge;
}

public String getGender() {
    return myGender;
}

public void setName(String name) {
    myName = name;
}

public void setAge(int age) {
    myAge = age;
}

public void setGender(String gender) {
    myGender = gender;
}

public String toString() {
    return myName + ", age: " + myAge + ", gender: " + myGender;
}

}

my next class is my Teacher Class:

public class Teacher extends Person {
private static int salary;
private static String subject;

public Teacher(String name, int age, String gender, String subject, int salary) {
    super(name, age, gender);
    //Constructor
    salary = salary;
    subject = subject;
    public String toString(){


    return super.toString() +", Subject: " + subject + " Salary: " + salary;
}
}

}

So the problem with my program is, I'm trying to add in the String Subject and the Int Salary. I'm getting an error that keeps saying "Syntax error on token "String", @ expected"(Line 12 of the Teacher Class) and then another one right next to it saying:""Syntax error, insert "EnumBody" to complete BlockStatement"(line 12 of the Teacher Class).

So all I am trying to do is add the new values into my parent class so that when I go to add in a user I can enter their salary and subject and so on. Sorry if i'm not too clear. It might be in plain sight for some of you, if i'm not clear enough please let me know.

Any help is greatly appreciated!

Thanks, Sully

I haven't looked all through your code, but I believe this is the problem:

public Teacher(String name, int age, String gender, String subject, int salary) {
    super(name, age, gender);
    //Constructor
    salary = salary;
    subject = subject;
    public String toString(){

    return super.toString() +", Subject: " + subject + " Salary: " + salary;
}

You've started overriding toString in the middle of the constructor. You need to close the constructor, then start the toString method:

public Teacher(String name, int age, String gender, String subject, int salary) {
    super(name, age, gender);
    //Constructor
    salary = salary;
    subject = subject;
}

public String toString() {    
    return super.toString() + ", Subject: " + subject + " Salary: " + salary;
}

Ideally, use the @Override annotation to tell the compiler to validate that you really are overriding a superclass method, too:

@Override
public String toString() {    
    return super.toString() + ", Subject: " + subject + " Salary: " + salary;
}

One issue is:

public Teacher(String name, int age, String gender, String subject, int salary) {
    super(name, age, gender);
    //Constructor
    salary = salary;
    subject = subject;
    public String toString(){


    return super.toString() +", Subject: " + subject + " Salary: " + salary;
}
}

Your toString() method is inside constructor. Move it to outside the constructor.

public Teacher(String name, int age, String gender, String subject, int salary) {
        super(name, age, gender);
        //Constructor
        salary = salary;
        subject = subject;
    }

   public String toString(){
       return super.toString() +", Subject: " + subject + " Salary: " + salary;
    }

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