简体   繁体   English

获取价值的正确方法?

[英]Correct way to get a value?

As part of my AP curriculum I am learning java and while working on a project I wondered which of the following is best way to return a value? 作为我的AP课程的一部分,我正在学习Java,并且在从事一个项目时,我想知道以下哪种方法是返回值的最佳方法?

   public double getQuarters(){
    return quarters;
}

or 要么

     public void getQuarters(){
    System.out.println(quarters);
}

***Note: I now that the second option is not "technically" returning a value but its still showing my the value so why bother? ***注意:我现在第二个选项不是“技术上”返回值,而是仍然显示我的值,所以为什么要打扰?

Your answer would be correct. 您的答案将是正确的。 The second method doesn't return any value at all, so while you might be able to see the output, your program can't. 第二种方法根本不返回任何值,因此虽然您可能可以看到输出,但程序却看不到。 The second method could still be useful for testing or even for a command line application, but it should be named something like printQuarters instead. 第二种方法对于测试甚至是命令行应用程序仍然可能有用,但是应该使用诸如printQuarters之类的名称。

 public double getQuarters(){
    return quarters;
}

Use this incorder to encapsulate quarters and hide it from being accessed by other programs. 使用此命令可以封装quarters并使其不被其他程序访问。 That means, you have to declare it as private quarters . 这意味着,您必须将其声明为private quarters Let see the second option: 让我们看第二个选项:

 public void getQuarters(){
    System.out.println(quarters);
}

However, this seems wrong as getQuarters is not returning anything. 但是,这似乎是错误的,因为getQuarters不返回任何内容。 Hence it would make more sense to refactor it as 因此,将其重构为

     public void printQuarters(){
         System.out.println(quarters);
     }

You answered your own question. 你是在自问自答。 For most definitions of the word "best", you should go with the first option. 对于“最佳”一词的大多数定义,您应该选择第一个选项。

Your question, however, does touch on the object-oriented programming topic of accessors and mutators. 但是,您的问题确实涉及访问器和更改器的面向对象编程主题。 In your example, "getQuarters" is an accessor. 在您的示例中,“ getQuarters”是访问器。 It is usually best to use accessors to retrieve your values. 通常最好使用访问器来检索您的值。 This is one way to adhere to the Open/Closed Principle . 这是遵守开放/封闭原则的一种方法。

Also, the Java community has a coding convention for this and many tools and libraries depend on code following those conventions. 而且,Java社区对此有一个编码约定 ,许多工具和库都依赖遵循这些约定的代码。

If all you need to do is display the value when this method is called, and you are ok with console output, then your System.out.println method will do the job. 如果您需要做的就是在调用此方法时显示该值,并且您可以使用控制台输出,那么您的System.out.println方法将完成此工作。 HOWEVER, a function that actually returns the variable is much more semantically correct and useful. 但是,实际上返回变量的函数在语义上更加正确和有用。

For example, while you may only need to print the variable for your current project, what if you came back later and decided that you were instead going to output your variable to a file? 例如,虽然您可能只需要为当前项目打印变量,但是如果稍后返回并决定将变量输出到文件中怎么办? If you wrote your getQuarters function with a println statement, you would need to rewrite the whole thing. 如果使用println语句编写了getQuarters函数,则需要重写整个过程。 On the other hand, if you wrote the function as a return, you wouldn't need to change anything. 另一方面,如果您将函数编写为返回值,则无需进行任何更改。 All you'd have to do is add new code for the file output, and consume the function where needed. 您要做的就是为文件输出添加新代码,并在需要的地方使用该函数。

A returning function is therefore much more versatile, although more so in larger code projects. 因此,返回函数的用途更加广泛,尽管在较大的代码项目中更是如此。

You return values to a specific point in your program, so that the program can use it to function. 您可以返回到程序中的特定点,以便程序可以使用它来运行。

You print values at a specific point in your program, so that you as an end user can see what value you got back for some function. 您可以在程序的特定位置打印值,以便您作为最终用户可以看到您为某个功能获得的值。

Depending on the function - for instance, yours - the result of quarters is no longer regarded in the program; 根据函数(例如您的函数)的不同,在程序中不再考虑quarters的结果。 all it did was print a value to the screen, and the application doesn't have a [clean|easy] way to get that back to use it. 它所做的只是在屏幕上打印一个值,而该应用程序没有[简洁]的方法来重新使用它。

If your program needs the value to function, then it must be a return . 如果您的程序需要该值起作用,那么它必须return If you need to debug, then you can use System.out.println() where necessary. 如果需要调试,则可以在必要时使用System.out.println()

However, more times than not, you will be using the return statement. 但是,您将多次使用return语句。

Option 1 is far superior. 选项1优越得多。

  1. It can be easily Unit Tested. 它可以很容易地进行单元测试。
  2. What if the spec changes and sometimes you want to print the result, other times put it into a database? 如果规格发生变化,有时您想打印结果,而有时又将其放入数据库,该怎么办? Option 1 splits apart the logic of obtaining the value from what to do with it. 选项1将获取值的逻辑与处理方法分开。 Now, for a single method getQuarters no big deal, but eventually you may have getDimes, getEuros, etc... 现在,对于单个方法,getQuarters没什么大不了的,但是最终您可能会有getDimes,getEuros等。
  3. What if there may be an error condition on quarters, like the value is illegal? 如果季度出现错误情况(例如值是非法的)怎么办? In option 1, you could return a "special" value, like -1.0, or throw an Exception. 在选项1中,您可以返回“特殊”值,例如-1.0,或者引发Exception。 The client then decides what to do. 然后,客户决定要做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM