Java 覆盖受保护的 setter 并消除 Public setter

Java Overriding Protected Setters and Eliminate Public Setters

人Class:

public class Person {


    private String firstName;
    private String lastName;

    public Person(String firstname,String lastname) throws InvalidDataException
    {
        setFirstname( firstname);
        setLastname(lastname);
    }


    public void personFirstName(String firstName) throws InvalidDataException { 
        setFirstname(firstName);
    }

    public void personLastName(String lastname) throws InvalidDataException {

        setLastname(lastname);
    }

    public String getFirstName() {
        return firstName;
    }

    public String getlasttName()
    {
        return lastName;
    }

    protected final void setFirstname(String firstname) throws InvalidDataException{
         if( firstname == null ||firstname.length() < 1) {
                throw new InvalidDataException("First Name Cannot be Empty");
         }
          this.firstName=firstname; 

    }

    protected final void setLastname(String lastname) throws InvalidDataException {

         if( lastname == null ||lastname.length() < 1) {
                throw new InvalidDataException("Last Name Cannot be Empty");

         }

         this.lastName = lastname;
    }



}

教授Class:

public class Professor extends Person {


    private String  professorID;



    public Professor(String professorID,String firstname, String lastname) throws InvalidDataException {
        super(firstname, lastname);
        // TODO Auto-generated constructor stub
        setProfessorID(professorID);
    }

    public void setID(String professorID) throws InvalidDataException{

        setProfessorID(professorID);
    }

    public String getID()
    {
        return this.professorID;
    }

    private void setProfessorID(String ID) throws InvalidDataException{
         if( ID == null ||ID.length() < 1) {
                throw new InvalidDataException("ID Cannot be Empty");
         }
          this.professorID=ID; 

    }

    public void printData()
    {
         System.out.println("Professor ID: " + this.getID() + " First Name: " + this.getFirstName() + " Last Name: " + this.getlasttName());
    }

}

我对实现 setter 并在我的子类中调用它们做了一些研究。通过将它们声明为 protected 和 final,我可以防止子类覆盖它并做出不需要的行为。我的问题是,我现在可以摆脱 personFirstName()personLastName() 吗?我的构造函数不使用它,他们调用受保护的最终 setFirstnamesetLastname()。摆脱 public setters 会在以后的开发中引起问题吗?

您应该尽量避免在 getter 和 setter 中做任何事情,这会让您很难追踪错误 - 请检查别处的数据。我认为这个更简单的版本可以:

public class Person {

    private String firstName;
    private String lastName;

    public Person() {} // introducing an empty constructor gives you more flexibility


    public Person(String firstname, String lastname) {
        setFirstname( firstname);
        setLastname(lastname);
    }

    public String getFirstName() {
        return firstName;
    } 

    public String getLasttName() {
        return lastName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

public class Professor extends Person {

    private String  professorID;

    public Professor (super();) {} // introducing an empty constructor gives you more flexibility

    public Professor (String firstname, String lastname, String professorID) {
        super(firstname, lastname);
        this.professorID = professorID;
    }

    public void setID(String professorID) {
        setProfessorID(professorID);
    }

    public String getID() {
        return this.professorID;
    }
}

您说过您的目标是避免在构造函数中调用可能被子类覆盖的方法。如果是这样,我会这样处理:

public Person(String firstName, String lastName) {
    this.privateSetFirstName(firstName);
    this.privateSetLastName(lastName);
}
private void privateSetFirstName(String firstName) {
    // ...your logic for setting the field...
}
private void privateSetLastName(String lastName) {
    // ...your logic for setting the field...
}
public void setFirstName(String firstName) {
    this.privateSetFirstName(firstName);
}
public void setLastName(String lastName) {
    this.privateSetLastName(lastName);
}

例如,使标准制定者成为标准,并在您的实现中将特定于实现的细节保存在私有方法中。