关于java方法的问题 [英] Question about java methods
问题描述
以下声明
The following statement
public double getValue(double new_value)
{
getValue = 5+8;
return getValue
}
<pre>
在驱动程序类中编写以下代码
s.getValue(0);
这项工作与否?
我尝试了什么:
我对这个规则感到困惑
in the driver class the following code is written
s.getValue(0);
Would this work or not?
What I have tried:
I am just confused about the rules for this one
推荐答案
好吧,它会编译并运行。例如,尝试:
Well, it would compile and run. Try, for instance:
class Foo
{
double getValue;
public double getValue(double new_value)
{
getValue = 5+8;
return getValue;
}
public static void main(String args[])
{
Foo foo = new Foo();
double g = foo.getValue(5);
System.out.format("getValue is %g\n", g);
}
}
然而,这样的代码将是的噩梦维护者不好: getValue
非常名称提示'getter'方法,这是一种在不改变对象状态的情况下返回值的方法。事实上它的主体改变了对象的状态。
此外,传递的 newValue
参数在方法体中没有任何效果。
However, such a code would be a nightmare for the poor maintainer: getValue
very name suggest a 'getter' method, that is a method returning a value without changing the state of the object. As matter of fact its body changes the state of the object.
Moreover, the passed newValue
parameter has no effect in the method body.
请参阅 Java™教程 [ ^ ]。
这篇关于关于java方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!