用Java获取和设置方法。他们是如何工作的? [英] Get and Set methods in Java. How do they work?

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

问题描述

我正在努力获得比迄今为止更好的理解......这是为了帮助我上课,但没有任何具体的家庭作业;我正在寻找更多我在教科书或教程中找不到的概念信息。在纯粹的理论层面上,我知道为什么封装对OO编程很有用,但需要更好地理解具体的内容。我一直在混淆数据流动的路径,并想知道我们在哪里获取和设置数据。希望这不是没有愚蠢的问题规则的例外。

I'm trying to get a better understanding than I've been able to so far...it's to help me with class, but not with any specific homework assignment; I'm looking for more conceptual information that I've not found in my textbook or in the tutorials. On a purely theoretical level, I get why encapsulation is powerful for OO programming, but need a better understanding of what, specifically is going on. I keep confusing myself about the path through which data flows, and wonder where we're getting and setting data FROM. Hopefully this is not the exception to the "there are no stupid questions" rule.

考虑:

 public class Employee
 {
    private int empNum;
    public int getEmpNum()
    {   return empNum;   }
    public void setEmpNum(int Emp)
    {   empNum = emp;   }
 }

我对此的解释:使用 int Emp 从某处传递,我们在 set ... 方法中设置声明的empNum变量。然后我们获取... 该变量并将其返回给调用方法。这是一个很好的理解吗?

My interpretation of this: Using int Emp passed from somewhere, we set the declared empNum variable in the set... method. We then get... that variable and return it to the calling method. Is that a good understanding?

进一步考虑:

import.javax.*
public class CreateEmp
{
    public static void main(String[] args)
    {
         int ssn;
         ssn = JOptionPane.showInputDialog(null,"enter ssn");
         Employee newEmp = new Employee(123456789);
         JOptionPane.showMessageDialog(null,"Employee ID " + newEmp.getEmpNum);
     }
 }

有了这个课,我不需要明确设置变量,因为声明新员工(123456789)会自动将该值发送到集。 .. 方法。 Employee方法中的 return 将结果值返回给调用方法,并使该变量可用,在本例中为对话框输出。我有处理吗?

With this class, I don't need to explicitly set a variable because declaring new Employee(123456789) automagically sends that value to the set... method. The return in the Employee method returns the resulting value to the calling method and makes that variable available, in this case to my dialog box output. Do I have a handle on that?

这是我感到困惑的地方。根据我上面提到的理解 - 可能是关闭 - 我永远无法决定放置逻辑的最佳位置。例如:我将输入员工编号对话框放在 Employee 类中,并且只有在传递给方法的参数为null时才运行条件。在我看来,正如我对数据流的理解,该课程的目的是获得一个员工编号。如果没有提供默认值,则需要创建一个。我不确定这种想法是不是最好的做法,或者我是否会在以后为自己设置麻烦。

Here's where I get confused. With my understanding as noted above--which may be off--I can never decide the best place to put logic. For instance: I would place the "enter employee number" dialog box in the Employee class, with a condition to run ONLY if the parameter passed to the method was null. In my mind, as I understand the data flow, the purpose of the class is to get an employee number. If one is not supplied as a default, one needs to be created. I'm not sure if this thinking is best practices, or if I'm setting myself up for trouble later.

我很想听到有关是否或不是我正确理解发生了什么。

I'd love to hear some thoughts about whether or not I'm properly understanding what's happening.

推荐答案

首先,这个网站将更多地用于可以得到具体回答的问题;与理论问题无关。

First, this site is to be used more for questions that can be concretely answered; not so much for theoretical questions.

无论如何,第一部分有'get'和'set',你理解得很好。现在,当你说你不需要明确设置员工编号,因为你做新员工......并不完全正确。如果您明确定义了一个构造函数,它接受一个参数的整数,然后将'empNum'设置为等于争论的整数值,那么这将有效。

Anyways, the first part with the 'get' and 'set' you understand fine. Now when you said that you do not need to explicitly set the employee number because you do 'new Employee ...' is not quite true. That would work if you had explicitly defined a constructor that takes an integer for an argument and then sets 'empNum' equal to the argued integer value.

Getters和setter通常与'private'修饰符一起使用。 Setter可用于控制输入。例如,您可以检查提供的值是否大于零,如果您提供直接访问权限,则无法检查。 Getters只允许检索值,但是为了进行更改,你强制使用相应的setter(如果有的话)。

Getters and setters are usually used in conjunction with the 'private' modifier. Setters can be used to control the input. For example, you can check if the provided value is greater than zero, where as you cannot if you give direct access. Getters just allow values to be retrieved, but to make changes you force the use of the corresponding setter (if any).

你是对的,如果一个值不是提供你应该默认设置一个。请考虑您检索的值(如果有)将用于其他类。如果用户从未设置值,该怎么办?如果不是你想要的怎么办?不同的阶级,无论它是什么,仍然需要一个价值,你仍然需要传递一些东西。通过设置默认值,不同的类可以识别出哦,这是一个默认值 - 这意味着用户可能没有提供正确的输入。通过这样做,您可以创建一个相对故障安全的应用程序;即使输入不符合预期,你也可以继续工作。

You are right about how if a value is not supplied you should set one by default. Consider that the value that you retrieve (if any) is to be used in a different class. What if the user never set a value? What if it's not something you expected? The different class, whatever it may be, still requires a value and you still need to pass something. By setting a default value, the different class can recognize that, "oh, this is a default value - this means that the user probably did not supply the correct input." By doing so, you create a relatively fail-safe application; you can keep on working even if the input is not what it is expected to be.

这篇关于用Java获取和设置方法。他们是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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