Mr.Mou @ ShiShi AP Center

AP CSA 1.9:方法签名

In earlier lessons, most of our code ran directly inside main. In this lesson, we learn how methods help organize code, reduce repetition, and make programs easier to trace.

前几课中,大部分代码都直接写在 main 方法里。这一课,我们学习方法如何帮助我们组织代码、减少重复,并让程序更容易追踪。

A method is a named block of code; a method signature tells Java which method call matches which method. 方法是有名字的代码块;方法签名帮助 Java 判断一次方法调用对应哪一个方法。

Core Concepts:
核心知识点

1. Main Idea
核心理解

A method is a named block of code that performs a task when it is called.

方法(method) 是一个有名字的代码块,被调用时会执行某个任务。

public static void sayHello()
{
    System.out.println("Hello");
}

This method does not run just because it exists. It runs only when it is called.

这个方法不会因为“存在”就自动运行。它只有在被调用时才会执行。

sayHello();

For AP CSA 1.9, the key is not writing complicated methods. The key is reading method definitions, matching calls to methods, and tracing the order of execution.

对于 AP CSA 1.9,重点不是写很复杂的方法,而是能读懂方法定义、匹配方法调用,并追踪执行顺序。

2. Procedural Abstraction
过程抽象

Procedural abstraction means using a method by knowing what it does, without needing to know every detail of how it works.

过程抽象的意思是:我们只需要知道一个方法“做什么”,不一定需要知道它内部“怎么做”。

For example:

System.out.println("Hello");

You know it prints text. You do not need to know the internal code of println.

你知道它会打印文字,但不需要知道 println 内部到底怎样实现。

Procedural abstraction helps programmers:

过程抽象可以帮助程序员:

3. Method Calls and Flow of Control
方法调用与执行流程

Execution starts in the main method.

程序从 main 方法开始执行。

When Java sees a method call, it jumps to that method, runs the method body, and then returns to the line after the call.

当 Java 遇到方法调用时,会跳到对应的方法,执行方法体,然后回到调用语句后面的下一行。

Example:

public class Song
{
    public static void chorus()
    {
        System.out.println("E-I-E-I-O");
    }

    public static void main(String[] args)
    {
        System.out.println("Old MacDonald had a farm");
        chorus();
        System.out.println("And on that farm he had a cow");
    }
}

Output:

Old MacDonald had a farm
E-I-E-I-O
And on that farm he had a cow

Trace it like this:

Step Code running Output
1 main starts  
2 first println Old MacDonald had a farm
3 chorus(); calls chorus E-I-E-I-O
4 return to main  
5 last println And on that farm he had a cow

4. Method Header vs. Method Signature
方法头 vs. 方法签名

A method header is the first line of a method definition.

方法头(method header) 是方法定义的第一行。

public static void verse(String animal, String sound)

For AP CSA, understand these parts:

Part Example Meaning
return type void this method returns no value
method name verse name used to call the method
parameter list (String animal, String sound) values the method needs

A method signature is:

方法签名(method signature) 是:

method name + ordered list of parameter types

For example:

public static void verse(String animal, String sound)

Its method signature is:

verse(String, String)

Important AP point: the return type is not part of the method signature.

AP 重点:返回类型不是方法签名的一部分

5. Parameters and Arguments
参数与实参

A parameter is a variable declared in the method header.

参数(parameter) 是方法头里声明的变量。

An argument is the actual value passed into the method call.

实参(argument) 是调用方法时真正传进去的值。

Example:

public static void verse(String animal, String sound)
{
    System.out.println("The animal is a " + animal);
    System.out.println("It says " + sound);
}

public static void main(String[] args)
{
    verse("cow", "moo");
}

Here:

Code Role
animal parameter
sound parameter
"cow" argument
"moo" argument

When the method is called:

animal gets "cow"
sound gets "moo"

调用方法时:

animal 得到 "cow"
sound 得到 "moo"

6. Method Calls Do Not Include Data Types
调用方法时不要写数据类型

In a method header, parameters include data types.

在方法头中,参数要写数据类型。

public static void verse(String animal, String sound)

But in a method call, you only pass values.

但是调用方法时,只传值,不写数据类型。

Correct:

verse("duck", "quack");

Wrong:

verse(String "duck", String "quack");

The arguments must match the parameter list in:

实参必须和参数列表匹配:

Example:

public static void printScore(String name, int score)
{
    System.out.println(name + ": " + score);
}

Correct:

printScore("Evie", 95);

Wrong:

printScore(95, "Evie");

The order is wrong.

顺序错了。

7. Call by Value
按值传递

Java uses call by value when passing arguments to methods.

Java 调用方法传参时使用 按值传递(call by value)

This means the parameter gets a copy of the argument value.

意思是:参数得到的是实参值的一个副本

Example:

public static void change(int x)
{
    x = 10;
    System.out.println("inside method: " + x);
}

public static void main(String[] args)
{
    int num = 5;
    change(num);
    System.out.println("after method: " + num);
}

Output:

inside method: 10
after method: 5

Inside the method, x changes to 10. But num in main is still 5.

在方法内部,x 变成了 10。但是 main 里的 num 仍然是 5

For this lesson, remember:

本课记住:

parameter = copy of the argument value

8. Overloaded Methods
方法重载

Methods are overloaded when multiple methods have the same name but different signatures.

如果多个方法名字相同,但方法签名不同,这叫 方法重载(overloading)

Example:

System.out.println();
System.out.println("Hello");
System.out.println(42);

These are different versions of println.

这些是 println 的不同版本。

Method Call Matching Signature Meaning
println() println() print a newline
println("Hello") println(String) print a String
println(42) println(int) print an int

Java chooses which method to use based on the number and types of arguments.

Java 会根据实参的数量和类型,选择对应的方法。

9. Common Beginner Mistakes
常见初学者错误

Mistake Wrong Code Why Wrong Correct Code 中文解释
Forgetting parentheses chorus; Method calls need () chorus(); 调用方法必须写括号
Thinking methods run automatically only defining chorus() A method runs only when called call chorus(); in main 方法定义了不等于执行了
Putting data types in method calls verse(String "cow", String "moo"); Calls use values only verse("cow", "moo"); 调用时不写数据类型
Wrong argument order printScore(95, "Evie"); Order must match parameters printScore("Evie", 95); 实参顺序要匹配
Confusing parameter and argument saying "cow" is a parameter "cow" is passed during the call "cow" is an argument 参数在方法头,实参在调用里
Including return type in signature void verse(String, String) Return type is not part of signature verse(String, String) 方法签名不包含返回类型

10. Debugging Example
调试例子

Buggy code:

public class Song
{
    public static void verse(String animal, String sound)
    {
        System.out.println(animal + " says " + sound);
    }

    public static void main(String[] args)
    {
        verse(String "cow", String "moo");
    }
}

Problem:

The method call incorrectly includes data types. Data types belong in the method header, not in the method call.

问题:

方法调用里错误地写了数据类型。数据类型应该写在方法头里,而不是调用语句里。

Fixed code:

public class Song
{
    public static void verse(String animal, String sound)
    {
        System.out.println(animal + " says " + sound);
    }

    public static void main(String[] args)
    {
        verse("cow", "moo");
    }
}

Output:

cow says moo
Bug Type Fix
verse(String "cow", String "moo"); syntax / compile-time error use verse("cow", "moo");
wrong number of arguments compile-time error match the parameter list
wrong argument type compile-time error pass a compatible value
wrong argument order compile-time or logic error match the parameter order

11. Mini Practice
小练习

Practice 1: Choose the Output
练习 1:选择输出

What is printed?

public class Test
{
    public static void a()
    {
        System.out.print("A");
    }

    public static void b()
    {
        System.out.print("B");
        a();
    }

    public static void main(String[] args)
    {
        a();
        b();
    }
}

Answer:

ABA

Explanation: main calls a() first, printing A. Then it calls b(), which prints B and then calls a() again.

解释:main 先调用 a(),打印 A。然后调用 b(),先打印 B,再调用 a() 打印 A

Practice 2: Match the Signature
练习 2:匹配方法签名

Given this method:

public static void printScore(String name, int score)
{
    System.out.println(name + ": " + score);
}

Which call is correct?

A. printScore("Mia", 90); B. printScore(90, "Mia"); C. printScore(String "Mia", int 90); D. printScore("Mia");

Answer:

A. printScore("Mia", 90);

Explanation: the method needs a String first and an int second.

解释:这个方法需要第一个实参是 String,第二个实参是 int

Practice 3: Predict Call by Value
练习 3:预测按值传递

What is printed?

public class Test
{
    public static void change(int x)
    {
        x = x + 5;
    }

    public static void main(String[] args)
    {
        int num = 10;
        change(num);
        System.out.println(num);
    }
}

Answer:

10

Explanation: x gets a copy of num’s value. Changing x does not change num.

解释:x 得到的是 num 的值的副本。改变 x 不会改变 num

Practice 4: Fix the Code
练习 4:修复代码

Fix the method call.

public static void verse(String animal, String sound)
{
    System.out.println(animal + " says " + sound);
}

public static void main(String[] args)
{
    verse(String "duck", String "quack");
}

Fixed code:

public static void main(String[] args)
{
    verse("duck", "quack");
}

Explanation: data types appear in the method header, not in the method call.

解释:数据类型写在方法头里,不写在方法调用里。

Quick Checklist
快速检查清单

Before answering a method-signature question, check:

做方法签名题前,检查: