Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java继承,有什么建议我应该如何改进这个程序吗?

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) 个答案

  1. # 1 楼答案

    这是家庭作业吗

    无论如何,除了hvgotcodes,Person构造函数还有冗余代码

    name=n;这姓名=姓名;不需要排队。你只需要name=n;或者这个。name=n;不管你的编码偏好是什么

    还有,为什么会有一个系统。出来PrtLn在类定义的中间?

    为什么Person让默认构造函数创建默认Person?通常,这将通过某种形式的生成器完成,或者只在测试代码中完成。对于生产代码来说没有价值

    这些子类没有将这些字段定义为private。它们只是默认的。有明确的原因吗?他们应该是私人的