0

public void set在 Java 中,做什么?我读过它'设置命名选项的值',所以它只是像函数或其他形式的变量赋值吗?(对不起,不是真正的 Java 程序员。)

这是我正在分析的代码:

public void set(String s, int value) {
  set(s, new Integer(value));
}

里面到底是什么?(set(s,new Integer(value))我认为这两个“集合”是完全不同的东西。

请帮忙。对不起,如果这是一个简单的问题,但我只是想确认我对这个主题的想法。

4

4 回答 4

8

public void set(String s, int value)是一个方法声明,后面是方法体{... }

public void set(String s, int value)
\____/ \__/ \_/
   |     |   |
   |     |   '---- Method name
   |     |
   |     '-------- Method return type
   |
   '-------------- Access modifier

后面的行{

set(s,new Integer(value))

是一个方法调用,它set使用snew Integer(value)作为参数调用 - 方法。

于 2012-08-21T08:02:20.053 回答
4

这是一种与其他方法一样的方法。方法 namet 没有特殊的语义set。该方法可能类似于Map.put并在提供的字符串键下注册值,但它也可以启动 10 个线程并计算我们所知道的生命的意义。

在声明中调用的“其他”设置方法可能是带有签名的方法

set(String key, Object value)

或者,也

set(String key, Integer value)

两者都可以在上下文中工作,但我投票支持前者,因为后者由于自动装箱而变得多余。

于 2012-08-21T08:02:28.040 回答
0

这是一个功能。public是函数可访问的范围(在这种情况下,您可以从类外部调用它),void是返回类型,而 void 表示没有返回值。set 是函数的名称。

该方法似乎是一种从类外部将任何变量设置为给定值的接口(虽然它只使用字符串和包含该值的新整数对象调用另一个方法“设置”,可能会达到另一个重载看起来像的功能

private void set(String s, Integer value)

你应该寻找类似的东西,也许你的代码会变得清晰。

于 2012-08-21T08:06:45.927 回答
0

在这里,公共意味着访问修饰符,如果您使用公共,您可以在类之外访问该方法。您也可以使用私有、默认和受保护的关键字来代替公共。这是了解这里的好链接

void means the return type in here you not return anything that's why use void if you need to return integer value you have to use int instead of the void key word. eg - public int getData(){} but if you specify the return type you have to return value.

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.

(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);

于 2012-08-21T08:30:31.050 回答