java Java中设置的公共无效;究竟是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12050534/
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
public void set in Java; What is it exactly?
提问by Secret
What does public void set
in Java, do? I've read that it 'Sets the value of a named option' so is it simply like a variable assignment in the form of a function or something? (Sorry, not really a Java programmer.)
是什么public void set
在Java中,怎么办?我读过它“设置命名选项的值”,所以它只是像函数或其他形式的变量赋值吗?(抱歉,不是真正的 Java 程序员。)
Here is the code I'm analyzing:
这是我正在分析的代码:
public void set(String s, int value) {
set(s, new Integer(value));
}
What's inside it anyway? (the set(s,new Integer(value)
) I'm thinking the two 'sets' are entirely different things.
里面到底是什么?(the set(s,new Integer(value)
) 我认为这两个“集合”是完全不同的东西。
Please Help. Sorry if this is a simple question, but I'm just trying to confirm my thoughts about the subject.
请帮忙。对不起,如果这是一个简单的问题,但我只是想确认我对这个主题的想法。
回答by aioobe
public void set(String s, int value)
is a method declaration, which is followed by a method body inside {
... }
.
public void set(String s, int value)
是一个方法声明,后面跟着一个里面的方法体{
... }
。
public void set(String s, int value)
\____/ \__/ \_/
| | |
| | '---- Method name
| |
| '-------- Method return type
|
'-------------- Access modifier
The line after the {
:
之后的行{
:
set(s,new Integer(value))
is a method call, which calls the set
-method with s
and new Integer(value)
as arguments.
是一个方法调用,它set
使用s
和new Integer(value)
作为参数调用-method 。
回答by Marko Topolnik
It's a method just like any other. There are no special semantics for methods namet set
. The method will probably work akin to Map.put
and will register the value under the string key supplied, but it could also start 10 threads and compute the meaning of life for all we know.
这是一种和任何其他方法一样的方法。方法 namet 没有特殊的语义set
。该方法可能类似于Map.put
并在提供的字符串键下注册值,但它也可以启动 10 个线程并计算我们所知道的生命的意义。
That "other" set method that is called inside the declaration could be a method with signature
在声明中调用的“其他”设置方法可能是带有签名的方法
set(String key, Object value)
or, also
或者,也
set(String key, Integer value)
Both would work in the context, but I vote for the former since the latter would be redundant due to autoboxing.
两者都适用于上下文,但我投票支持前者,因为后者由于自动装箱而变得多余。
回答by Herm
it's a function. public
is the scope where the function is reachable (in this case you can call it from outside of the class), void
is the return type, while void means there is no return value. set is the name of the function.
这是一个函数。public
是函数可访问的范围(在这种情况下,您可以从类外部调用它),void
是返回类型,而 void 表示没有返回值。set 是函数的名称。
The Method seems to be a kind of interface to set any variable to the given value from outside of the class (while it only calls another Method 'set' with the String and a new Integer-Object containing the value, propably to reach another overloaded function which could look like
该方法似乎是一种接口,可以从类外部将任何变量设置为给定值(虽然它只调用另一个带有字符串的方法“set”和一个包含该值的新整数对象,可能会达到另一个重载看起来像的功能
private void set(String s, Integer value)
you should look for something like that, perhaps your code will become clear then.
你应该寻找类似的东西,也许你的代码会变得清晰。
回答by mssb
In here public means Access modifier if you use public you can access to that method outside of the class. instead of public you can use private,defaultand protected key word also. this is good link for learn about that here
在这里,public 表示访问修饰符,如果您使用 public,则可以在类之外访问该方法。您也可以使用 private、default 和 protected 关键字代替 public。这是在此处了解相关信息的好链接
void means the return type in here you not return anything that's why use voidif you need to return integer value you have to use intinstead of the void key word. eg - public int getData(){} but if you specify the return type you have to return value.
void 意味着这里的返回类型你不返回任何东西,这就是为什么如果你需要返回整数值,你必须使用int而不是 void 关键字,所以使用void。例如 - public int getData(){} 但如果您指定返回类型,则必须返回值。
in here set means method name it is not a key word. but in the java to set some values we use set word as an example public void setName(){}like wise.
在这里 set 表示方法名称,它不是关键字。但是在 java 中设置一些值我们使用 set word 作为示例public void setName(){}像明智的那样。
(String s, Integer value) these are the parameters if you need to call that method you have to pass to objects of the specified classes. in here you have to pass string value and integer value. eg - set("Secret",1);
(String s, Integer value) 如果您需要调用必须传递给指定类的对象的方法,这些是参数。在这里你必须传递字符串值和整数值。例如 - 设置(“秘密”,1);