Java中的获取者和设置者 [英] Getters and setters in Java

查看:87
本文介绍了Java中的获取者和设置者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java 非常新,我正试图理解一些概念。我一直在阅读几本书,但我一直陷在同一个地方。看看下面的代码:

  package Grade; 
import static java.lang.System.out;
import java.util。*;

public class GradeBook {

private String courseName;

public void setCourseName(String name){
courseName = name;


public String getCourseName(){
return courseName;

$ b $ public void Display()//需要一个字符串来运行
{
out.println(Welcome to:+ getCourseName());
}
}

我不确定为什么 setCourseName() needs(String name)在那里或者它甚至从那里得到那个值。如果有人可以尝试用通俗的术语来解释这一点,那么我们将非常感激。

解决方案

您需要使用getter和setter方法这个类是因为 courseName 字段是私有的;代码之外的代码不能直接访问该字段。其他使用该类的代码(通常称为客户端代码)将提供传递给 setCourseName 的参数值,并使用<$ c返回的值$ c $> getCourseName 。



有了这个简单的例子,很难理解为什么要这样设计类。人们可以很容易地让 courseName 一个公共字段,让客户端代码直接操作它。然而,随着班级变得更加复杂,使用get / set风格提供了许多优势。这里有一些内容:使用getter / setter方法有助于将客户端代码从更改中隔离出来,以实现类的实现。 例如,你稍后可能会决定课程名称具有某种内部结构,并且您想将名称的各个部分保留在单独的字段中。您可以简单地更改 setCourseName getCourseName 的实现方式,并且不需要更新客户端代码。 / b>

  • 自动化软件生成工具依赖于属性获取器和设置器的存在。使用这些工具,您可以简单地将您的类建模为具有courseName属性,它将生成使用适当方法的代码。

  • 您可能稍后决定改变课程名称应该有一定的副作用(例如更新某个课程目录)。通过修改 setCourseName 方法来实现这些依赖关系。




    <通常,在面向对象的编程中,默认设计应尽可能隐藏类实现,并只通过方法暴露类。有时有很好的理由违反这个原则,但坚持它往往会导致更强大的软件。


    I am very new to Java and I am trying to understand some concepts. I have been reading a couple of books but I keep getting stumped around the same place. Look at the following code:

    package Grade;
    import static java.lang.System.out;
    import java.util.*;
    
    public class GradeBook {
    
    private String courseName;
    
    public void setCourseName (String name) {
        courseName = name;
    
    }
    public String getCourseName() {
        return courseName;
    }
    
    public void Display() // needs a string to run
    {
    out.println("Welcome to:" + getCourseName() );  
    }
    }
    

    I'm not sure why setCourseName() needs ( String name ) to be there or where it even gets that value from. If someone could try to explain this in layman terms it will be greatly appreciated.

    解决方案

    You need to use getter and setter methods for this class because the courseName field is private; code outside the class cannot access the field directly. Other code that uses the class (usually called client code) would supply the argument value passed to setCourseName and would use the value returned by getCourseName.

    With this simple example, it's hard to appreciate why the class would be designed this way. One could just as easily make courseName a public field and let client code manipulate it directly. However, as the class becomes more complicated, using the get/set style offers many advantages. Here are some:

    • Using getter/setter methods helps insulate client code from changes to how the class is implemented. For instance, you might decide later that the course name has some sort of internal structure and you want to keep the pieces of the name in separate fields. You would be able to simply change how setCourseName and getCourseName are implemented and client code would not need to be updated.

    • Automated software generation tools rely on the existence of property getters and setters. With these tools, you can simply model your class as having a "courseName" property and it will generate code that uses the appropriate methods.

    • You might later decide that changing the course name should have certain side effects (such as updating a course catalog somewhere). Implementing these dependencies can be done by modifying the setCourseName method.

    With object-oriented programming in general, the default design should be to hide the class implementation as much as possible and expose the class only through methods. There are sometimes good reasons to violate this principle, but adhering to it tends to result in more robust software.

    这篇关于Java中的获取者和设置者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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