java为超类

时间:2017-06-14 02:46:15

标签: java inheritance constructor

我有一个带有3个构造函数的超类,我想知道是否有更聪明的方法来编写子类构造函数

public class Person{

private String name;
private int age;
private String homeTown;


public Person(String name){
    this.name = name;
    this.age = 18;
    this.homeTown = "Atlanta";
}

public Person(String name, int age){
    this.name = name;
    this.age = age;
    this.homeTown = "Atlanta";
}

public Person(String name, int age, String homeTown){
    this.name = name;
    this.age = age;
    this.homeTown = homeTown;   
}

我还有一个继承超类

的子类
public class Student extends Person{
private double avgGPA;
private int ID;
private String[] classes;

public Student(double avgGPA, int ID, String[] classes, String name){
    super(name);
    this.avgGPA = avgGPA;
    this.ID = ID;
    this.classes = classes;
}

public Student(double avgGPA, int ID, String[] classes, String name, int age){
    super(name, age);
    this.avgGPA = avgGPA;
    this.ID = ID;
    this.classes = classes;
}

public Student(double avgGPA, int ID, String[] classes, String name, int age, String homeTown){
    super(name, age, homeTown);
    this.avgGPA = avgGPA;
    this.ID = ID;
    this.classes = classes;
}

我的子类工作正常并且运行没有错误,但我想知道是否有另一种方法为子类编写构造函数而不编写相同的构造函数3次,只是因为超类有3个不同的构造函数。

3 个答案:

答案 0 :(得分:2)

嗯,Java中有一些东西可以简化你的超类。您可以使用this();在同一个类中调用另一个构造函数。因此,不是为每个构造函数设置每个变量,而是使用一个变量设置构造函数并使用this();来传递它的默认值。对于您的超类,您可以使用这些:

public Person(String name){
    this(name, 18, "Atlanta");
}

public Person(String name, int age){
    this(name, age, "Atlanta");
}

public Person(String name, int age, String homeTown){
    this.name = name;
    this.age = age;
    this.homeTown = homeTown;   
}

对于子类,我创建了一个名为setVars的私有方法,它接受您使用的三个变量:double avgGPAint IDString[] classes。因此,不是在每个构造函数中设置它们,您的类可能如下所示:

public Student(double avgGPA, int ID, String[] classes, String name){
    super(name);
    setVars(avgGPA, ID, classes);
}

public Student(double avgGPA, int ID, String[] classes, String name, int age){
    super(name, age);
    setVars(avgGPA, ID, classes);
}

public Student(double avgGPA, int ID, String[] classes, String name, int age, String homeTown){
    super(name, age, homeTown);
    setVars(avgGPA, ID, classes);
}

private void setVars(double avgGPA, int ID, String[] classes) {
    this.avgGPA = avgGPA;
    this.ID = ID;
    this.classes = classes;
}

我认为除非你想创建一个静态初始化方法,因为它会像你得到的那样高效。

答案 1 :(得分:1)

有些事情如下:

public final class Person{

    private final String name;
    private final int age;
    private final String homeTown;
    private double avgGPA;

    private Person(String name, int age, String homeTown, avgGPA){

        this.name = name;
        this.age = age;
        this.homeTown = homeTown;
        this.avgGPA = avgGPA;
    }

    public static Person createPerson(String name, age, homeTown, avgGPA){
        return new Person(name, age, homeTown, avgGPA);
    }

    public static Person createPersonwithoutHomeTown(String name, age,avgGPA){
        return new Person(name, age, "Atlanta", avgGPA);
    }

    public static Person createPersonwithoutAge(String name,avgGPA){
        return new Person(name, 18, "Atlanta", avgGPA);
    }

}

Immutable个对象是在创建后不会更改其状态的对象,并且不允许进行子类化。从长远来看,不可变的课程是有利的。

答案 2 :(得分:0)

我认为修改您的Person对象以使用构建器模式会对您有所帮助。