Java 副作用——这是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1073909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Side effect--what's this?
提问by
Could someone tell me the meaning of "side effect" in the following line?
有人能告诉我下一行中“副作用”的含义吗?
If you're calling an EL function that doesn't return anything, then you're calling it just for its side effects.
如果您正在调用一个不返回任何内容的 EL 函数,那么您只是为了它的副作用而调用它。
回答by unwind
This means that you're not calling a "true" function, in the mathematical sense. Such a function always returns a value, which is totally decided by its input parameters. There is no "state" to modify, and nothing else can happen. This is why functional programming is interesting from the parallelization point of view; it makes it easier to prove that e.g. two function calls are independent and can run in parallel.
这意味着您不是在数学意义上调用“真实”函数。这样的函数总是返回一个值,这个值完全由它的输入参数决定。没有要修改的“状态”,也不会发生其他任何事情。这就是为什么从并行化的角度来看函数式编程很有趣。它更容易证明例如两个函数调用是独立的并且可以并行运行。
See the Wikipedia entry on pure functionsfor further detail.
有关更多详细信息,请参阅有关纯函数的 Wikipedia 条目。
回答by Michael Borgwardt
A side effect is anything a method does besides computing and returning a value. Any change of instance or class field values is a side effect, as is drawing something on the screen, writing to a file or a network connection.
副作用是方法除了计算和返回值之外所做的任何事情。实例或类字段值的任何更改都是副作用,就像在屏幕上绘制某些内容、写入文件或网络连接一样。
Strictly speaking, a "function" is defined as not having side effects - which is why Java uses the word "method" instead. A real function with no return value would be pointless.
严格来说,“函数”被定义为没有副作用——这就是 Java 使用“方法”这个词的原因。没有返回值的真正函数将毫无意义。
Obviously, a method that does not have a return value must have some sort of side effect that justifies its existence. Set methods are an example - the side effect is changing the object's internal state.
显然,一个没有返回值的方法必须有某种副作用来证明它的存在是正确的。Set 方法就是一个例子——副作用是改变对象的内部状态。
回答by codethulhu
A side effect is when a method call changes a class's state. So
副作用是当方法调用更改类的状态时。所以
public class SideEffectClass{
private int state = 0;
public doSomething(int arg0){
state += arg0;
}
}
Here, doSomething(int arg0) has the side effect of changing the state variable.
这里, doSomething(int arg0) 有改变状态变量的副作用。
When you think of a program, you can think of it as instructions + state + input. So if the domain of a program is the range of all possible input * state, and the program has side effects, you can see that the codomain of possible results for the application can grow explosively, as the number of side effects increase. This makes the possible states for the program large, which leads to complicated testing. The functional programming paradigm is designed to eliminate side effects. By making functions first class citizens and by making all declarations immutable functional programming prevents side effects, which makes functional programming shine in parallel processing, as synchronization issues are reduced.
当你想到一个程序时,你可以把它想象成指令+状态+输入。所以如果一个程序的域是所有可能的输入*状态的范围,并且程序有副作用,你可以看到应用程序的可能结果的codomain会随着副作用数量的增加而爆炸性增长。这使得程序的可能状态变大,从而导致复杂的测试。函数式编程范式旨在消除副作用。通过使函数成为一等公民,并使所有声明不可变,函数式编程可以防止副作用,这使得函数式编程在并行处理中大放异彩,因为同步问题减少了。
回答by Oekel
Let's manipulate the given code above a little bit to make it clear in comparison.
让我们稍微操作一下上面给定的代码,以便进行比较。
public class SideEffectClass{
private int state = 0;
public doSomething(...){//Does not matter
state ++;
}
}
回答by Bahadir Tasdemir
When you use a medicine, it's side effects are those that are generally unwanted bad effects. The main purpose to use it is to get it's "demanded" effect. Here, when we look at the angle of functions, generally when you call them you get a calculated value and use. There are other functions that they also change some values while calculating the "demanded" value so here the "changing some values" is a side effect. In the description of your sentence, if a function does not return anything, it is only used for its side effects so here, the side effects are "changing some values".
当您使用药物时,它的副作用是那些通常不需要的不良影响。使用它的主要目的是获得它的“需求”效果。在这里,当我们查看函数的角度时,通常当您调用它们时,您会得到一个计算值并使用。还有其他函数,它们在计算“要求”值时也会改变一些值,所以这里“改变一些值”是一个副作用。在你的句子的描述中,如果一个函数没有返回任何东西,它只是用于它的副作用,所以在这里,副作用是“改变一些值”。