Java - 来自构造函数的setter [英] Java - Setters from Constructors

查看:136
本文介绍了Java - 来自构造函数的setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package cen.col.course.demo;

import java.io.Serializable;

public class Course implements Serializable {

private static final long serialVersionUID = 1L;
protected String code;
protected String title;
protected Professor professor;

public Course( String code) throws InvalidDataException {
    super();
    setCode(code);
}

public Course(String code, String title ) throws InvalidDataException  {
    this(code);
    setTitle(title);
}

public Course(String code, String title, Professor professor) throws InvalidDataException   {
    this(code,title);
    setProfessor(professor);
}
    public String getCode() {
    return code;
    }

protected void setCode(String code) throws InvalidDataException {
    if ( code == null || code.length() < 1) {
        throw new InvalidDataException("Course must have a course code");
    }
    this.code = code;
}

public String getTitle() {
    return title;
}

public void setTitle(String title)  throws InvalidDataException {
    if ( title == null || title.length() < 1) {
        throw new InvalidDataException("Course must have a title");
    }
    this.title = title;
}

public Professor getProfessor() {
    return professor;
}

public void setProfessor(Professor professor) {
    this.professor = professor;
}

public String toString() {
    String output = getCode() + ": [" + getTitle() + "]";
    if (getProfessor() != null ) {
        output += " is taught by " + getProfessor();
    }
    return output;
}

public boolean equals(Course c) {
    if ( ! this.getCode().equals(c.getCode())){
        return false;
    }
    if ( ! this.getTitle().equals(c.getTitle())){
        return false;
    } 
    // should the prof field be included in test for equality?
    if ( ! this.getProfessor().equals(c.getProfessor())){
        return false;
    } 
    return true;
}


}

我有三个问题:


  1. 我注意到我的教授从构造函数中调用了setter方法。我做了一些搜索,并对它有不同的想法。有人说没关系,有人说你在使用子类时要小心,是否可以从构造函数中调用你的setter?

  1. I noticed my professor calling the setter methods from the constructors. I did a little searching around, and have mixed thoughts about it. Some say its okay, some say you have to be careful when your using subclasses, Is it okay to call your setters from the constructors?

构造函数抛出异常因为她正在从构造函数中调用setter。现在我的问题是,如果从构造函数中调用setter并不是一种安全的方法,那么这样做的正确方法是什么?我的猜测是声明一个无参数构造函数,并使用setter构建对象。

The constructors throw exceptions because she is calling the setters from the constructor. Now my question is, if calling the setters from the constructors isn't a safe way of doing it, What is the proper way of doing it? My guess would be to declare a no argument constructor, and build the object using setters.

我想这样做是不可能的?

I guess doing this, is out of the question?

课程createCourse =新课程(1234,编程1,Pam Halpert);

Course createCourse = new Course("1234","Programming 1","Pam Halpert");

我正在调用带有3个参数的构造函数,但是,如果从构造函数中调用setter是不安全的,那么如何执行此操作并具有异常?我可以使用if语句吗?检查是否有空白并在必要时抛出异常?

I am calling the constructor that takes 3 arguments, However, if calling the setter from the constructor is not safe, how would go about doing this, and have the exception in place? Could I use if statements? Check to see if something is blank and throw the exception if necessary?

推荐答案

由于这是家庭作业或某项研究,你的教授想要给你看东西。

Since this is homework, or some study, your professor prolly wanna show you things.

然而,

课程createCourse =新课程(1234,编程1 ,Pam Halpert);

实际上是最好的事情。

根据您正在开发的内容,在大多数情况下,您希望提供尽可能少的构造函数,除非您正在设计编程语言。如果您正在使用公共API或产品,则应确保您的消费者不会犯错误或滥用您的API,如果您允许他们创建错误。

Depending on what you are developing, most of the time, you want to provide as little constructors as possible, unless you are designing a programming language. if you are working on a public API, or product, you should make sure that your consumers do not make mistakes, or abuse your API, if you allow them to create bugs.

构造函数可以抛出异常,这很好。

Constructor can throw and exception, which is good.

据我所知,调用setter的原因是做了一些验证或一些逻辑。这很好。

As far as i see, calling the setter reason was doing some validation or some logic. which is fine.

请记住,在构造函数中做任何工作都被认为是一种不好的做法。

Keep in mind, doing any work in constructor is considered a bad practice.

你应该在类外部进行,并将它们作为构造函数参数或setter / getter传递。

you should do it outside the class and pass them in as constructor arguments, or setter/getter.

这篇关于Java - 来自构造函数的setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆