Calling Class Methods · 调用类方法
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| class method/klæs ˈmeθəd/ | 类方法 | lèi fāng fǎ |
| static method/ˈstætɪk ˈmeθəd/ | 静态方法 | jìng tài fāng fǎ |
Calling a class method
- A class method 类方法 (a static method 静态方法) belongs to the class itself, not to an object.
- Call it with the class name and a dot:
Math.sqrt(16),Integer.parseInt("42"). - No object needs to exist — the method lives on the class.
- The keyword
staticin the header marks a method as a class method.
调用类方法
- 类方法(静态方法)属于类本身,而非某个对象。
- 用类名加一个点来调用:
Math.sqrt(16)、Integer.parseInt("42")。 - 不需要有对象存在——方法就在类上。
- 方法头里的关键字
static把一个方法标记为类方法。
Passing arguments
- Put the arguments inside the parentheses:
Math.max(3, 8). - The values are matched to the method's parameters, by position.
- Give the right number and types of arguments, or it won't compile.
- No arguments? Still write the empty parentheses:
sc.nextLine().
传入实参
- 把实参放进圆括号里:
Math.max(3, 8)。 - 这些值按位置匹配到方法的形参。
- 给出正确数量和类型的实参,否则无法编译。
- 没有实参?还是要写空括号:
sc.nextLine()。
Using the return value
- Most class methods return a value — capture it in a variable.
double r = Math.sqrt(16);stores4.0inr.- Ignoring a useful return value usually means you forgot to store the result.
- A returned value can also feed straight into another expression.
使用返回值
- 大多数类方法会返回一个值——用一个变量接住它。
double r = Math.sqrt(16);把4.0存进r。- 忽略一个有用的返回值,通常意味着你忘了存结果。
- 返回的值也可以直接喂进另一个表达式。
Static vs. instance — a first look
- A class (static) method is called on the class:
Math.abs(-5). - An instance method (next lessons) is called on an object:
word.length(). - The tell: is there a dot after a class name or after an object?
- Class methods don't need an object; instance methods do.
静态对实例——初步一瞥
- **类(静态)**方法在类上调用:
Math.abs(-5)。 - 实例方法(后面的课)在一个对象上调用:
word.length()。 - 判断标志:点后面是一个类名还是一个对象?
- 类方法不需要对象;实例方法需要。
Call a class method on the CLASS name, not an object: Math.sqrt(16), never m.sqrt(16). Match the argument count and types to the method's parameters, and remember to store the return value — Math.sqrt(16); on its own computes 4.0 and then throws it away. Empty parentheses are still required when there are no arguments.
在类名上调用类方法,而非对象上:Math.sqrt(16),绝不是 m.sqrt(16)。让实参的数量和类型匹配方法的形参,并记得存下返回值——单独的 Math.sqrt(16); 算出 4.0 然后就把它丢了。没有实参时仍然需要空括号。
Combining class method calls:
int bigger = Math.max(3, 8);→biggeris8.double root = Math.sqrt(bigger + 1);→Math.sqrt(9)→3.0.- A return value (
8) fed straight into the next call.
组合类方法调用:
int bigger = Math.max(3, 8);→bigger是8。double root = Math.sqrt(bigger + 1);→Math.sqrt(9)→3.0。- 一个返回值(
8)直接喂进下一个调用。
A class (static) method is called on the class name: Math.sqrt(16). Pass the right arguments (matched by position and type) in parentheses, and store the return value in a variable to use it. This differs from an instance method, which is called on an object.
类(静态)方法在类名上调用:Math.sqrt(16)。在括号里传入正确的实参(按位置和类型匹配),并把返回值存进变量以使用它。这不同于在对象上调用的实例方法。
Calling a static method · 调用一个静态方法
Math.max is called on the class and returns the larger value. · Math.max 在类上被调用,返回较大的值。
How do you correctly call the static method sqrt from the Math class? · 如何正确地调用 Math 类的静态方法 sqrt?
Class methods are called on the class name with a dot. · 类方法在类名上用点调用。
What does Math.max(3, 8) return? · Math.max(3, 8) 返回什么?
max returns the larger of the two arguments. · max 返回两个实参中较大的那个。
The keyword that marks a method as a class (static) method is... · 把方法标记为类(静态)方法的关键字是……
static means the method belongs to the class, not an object. · static 表示方法属于类,而非对象。
Math.sqrt(16); on its own line stores 4.0 for later use. · 单独一行的 Math.sqrt(16); 会把 4.0 存下来供以后使用。
It computes 4.0 but discards it — you must store the return value. · 它算出 4.0 但丢弃了——你必须存下返回值。
A class method is called on the class name, while an instance method is called on an object. · 类方法在类名上调用,而实例方法在对象上调用。
Math.abs(-5) (class) vs word.length() (object). · Math.abs(-5)(类)对 word.length()(对象)。