java - 为什么使用 protected 方法而不是构造函数

标签 java

我在看书here .用户建议

if you plan on subclassing and want the validation setter available, declare it protected final instead of private.

当我可以将它声明为私有(private)并让基类中的构造函数设置变量时,为什么要声明一个方法protected final

例子:

    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;
        }

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

        }

        private void setLastname(String lastname) throws InvalidDataException {

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

             }

             this.lastName = lastname;
        }

    }

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());
    }

}

在什么情况下我希望将 Person 中的私有(private)方法声明为 protected final?我的子类 Professor 可以通过基类中的构造函数访问它们。

最佳答案

protected

protected 关键字是一个访问修饰符,允许子类访问该资源。在这种情况下,方法。

最终

final 关键字是一种多态限制,可防止子类重新定义 给定资源。同样,在这种情况下,方法。

考虑到这些定义,这将创建一个验证方法,子类仍然可以访问而不能更改它。简而言之,任何子类都不能违反验证条款。通过将调用放在构造函数中,这意味着子类无法在其他任何地方进行验证调用。他们每次要测试时都必须创建一个新对象,这显然是不可取的。

这样做使其更易于测试,但允许子类在不违反验证规则的情况下尽可能多地灵活。

关于java - 为什么使用 protected 方法而不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28795340/

相关文章:

java - 通过套接字发送javafx矩形/ Pane

java - 有没有不使用Java2D API的高质量图像缩放库?

java - 如何使用 Spring Web Flow 绑定(bind)复合模型

java - 如何在渲染线程和更新线程之间传递复杂对象?

java - Cassandra 自定义类型映射器

java - 如何对JTable中JComboBox的字符串进行排序?

在继续之前要渲染 Java Swing JFrame

java - 如何使用 Hibernate 和 spring MVC 在 Jsp 页面上显示数据库中的图像

Java/Mockito : Set up 'when' for list arg containing element

java - 1 个框架上的 3 个面板