开发者

Inheritance, any suggestions how I should improve this program?

public class Person{

private String name;
private String surname;
private int age;
private String address;

    Person(String n, String s, int a, String i){
    name = n;
    surname = s;
    age = a;
    address = i;

    this.name = name; this.surname= surname;
    this.age = age; this.address = address;

 }

    public String getName() {return name;}
    public String getSurname() {return surname;}
    public int getAge() {return age;}
    public String getAddress() {return address;}

    System.out.println(name+surname+age+address);

    Person(){
        this("Ryan","Borg",25,"Gudja");
    }
}

public class Student extends Person{
    int mark;
    String credits;

    Student(){
    }

    Student(String n, String s, int a, String i, int m, String c){
        super(n, s, a, i);
        mark = m;
        credits = c;

    public String getName() {return name;}
    public String getSurname() {return surname;}
    public int getAge() {return age;}
    public String getAddress() {return address;}
    public int getMark(){return mark;}
    public String getCredits() {return credits;}

    System.out.println(name+surname+age+address+mark+credits);

    }
 }

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

    Teacher(){
    }

    Teacher(String n, String s, int a, String i, int sal, String sub){
        super(n, s, a, i开发者_高级运维);
        salary = sal;
        subject = sub;

    public String getName() {return name;}
    public String getSurname() {return surname;}
    public int getAge() {return age;}
    public String getAddress() {return address;}
    public int getSalary(){return salary;}
    public String getSubject() {return subject;}

    System.out.println(name+surname+age+address+salary+subject);
    }
  }


  1. The person subclasses have getters that are not necessary because they are on the Person base class. If you did something different you could keep accessors on subclasses if they were different, but in this particular case they are not.
  2. Avoid variable names like 'n'. That should be 'name'. Being verbose within reason leads to more readable code.


Is this homework?

Anyhow, in addition to hvgotcodes, the Person constructor has redundant code.

name = n; this.name = name; lines are not required. You only need name = n; or this.name = n; whatever your coding preference is.

Also, why is there a System.out.println in the middle of the class definition?

Why does Person have the default constructor create a default Person? Normally, this would be done with some form of Builder, or only in test code. Not valuable for production code.

The subclasses don't define there fields as private. They're only default. Is there an explicit reason for this? They should likely be private.


On top of what hvgotcodes has suggested override toString() instead of doing System.out.println()


Adding to hvgotcodes, in the Person classe's constructor no need to assign the parameters values to local variables.

In person class you cannot write the sysout statements which should be in some method.

and in the constructors of Teacher ans Student you have defined the methods which should not. and are not required as these are present in Person.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜