为什么 Bruce Eckel 说只能将对象传递给 Java 中的方法?
Why does Bruce Eckel says that only objects can be passed into a method in Java?
这里引用 Bruce Eckel 的书 "Thinking in Java":
The method argument list specifies what information you pass into the
method. As you might guess, this information—like everything else in
Java—takes the form of objects. So, what you must specify in the
argument list are the types of the objects to pass in and the name to
use for each one.
我不明白。我认为您可以将基元传递给方法(例如 int),而基元不是对象。
例如:
public static int multiply(int x, int y){
return x * y;
这是一个方法,里面只有基元,根本没有对象。
作者并没有说只有个对象可以传递给方法。这句话出现在名为 "Everything is an Object" 的章节中。它旨在突出语言的面向对象方面,但它也包括一个关于原语的部分,解释它们是一种特殊情况。
尽管如此,您所说的仅采用原始类型的方法仍然是正确的,但这已隐含在 "Special case: primitive types" 部分中。
这里引用 Bruce Eckel 的书 "Thinking in Java":
The method argument list specifies what information you pass into the method. As you might guess, this information—like everything else in Java—takes the form of objects. So, what you must specify in the argument list are the types of the objects to pass in and the name to use for each one.
我不明白。我认为您可以将基元传递给方法(例如 int),而基元不是对象。 例如:
public static int multiply(int x, int y){
return x * y;
这是一个方法,里面只有基元,根本没有对象。
作者并没有说只有个对象可以传递给方法。这句话出现在名为 "Everything is an Object" 的章节中。它旨在突出语言的面向对象方面,但它也包括一个关于原语的部分,解释它们是一种特殊情况。
尽管如此,您所说的仅采用原始类型的方法仍然是正确的,但这已隐含在 "Special case: primitive types" 部分中。