1. Exam Points
Declare and call instance methods.Pass arguments(实际参数) into parameters(形式参数).- The
argumentsmust match the parameters of a method.Call instance methods(non-static) using an instance name.- One instance method can call another method from the same class directly.
Store the value returnedfrom a method in a variable of the right type.A method must a have return type(either void or any type).- Only constructors can have no return type.
If a method has a non-void return type, a value must be returned.
2. Knowledge Points
(1) Methods(方法)
- A
methodis anamed block of code enclosed in braces {}that only runs when it is called. - Declaring a method is the process of
procedural abstraction. - Procedural abstraction allows a programmer to use a method by knowing what the method does even if they do not know how the method was written.
(2) Declare Methods (声明方法)
-
Syntax:
public [static] returnType methodName(parameter list){ //method header // method body: code to execute in the method }- In AP CSA, just declare a method as
public static is optional, it is used when you define a class method.methodNameis the name of the method, use a meaningful name.parameter(参数)list can contain one or multiple parameters, you should specify the data type and name of each parameter.
- In AP CSA, just declare a method as
-
A
method signaturefor a method with parameters consists of themethod name and the ordered list of parameter types.- Example: public void add(int x, int y){}
- Method signature: add(int x, int y)
-
Examples:- A method with no parameter
- declare a method: public void welcome(){}
- call a method: welcome();
- A method with parameters
- declare a method:
- public void add(int x, int y){}
- public void play(String name){}
- call a method:
- add(10,20);
- play("LOL");
- declare a method:
- A method with a return type
- declare a method: public
intsquare(int x){returnx*x; } - call a method:
int x= square(4);
- declare a method: public
- A method with no parameter
-
About the
return statement:- A method's return value is a specific value returned by the method.
- A return value is obtained through a
return statementwithin a method. Ex.rerurn 3; - A method can have
more than one return statement, butonly one return statement is executed, the first matching return is executed. - A method's
return value must be of a specific type, the value returned mustmatchthedeclared return typeof a method. - For methods without a return value, the "
void" keyword should be used to declare the method as "untyped" (or "empty type"). - A return statement also
returns the execution backto where the method is called. Statements after a return statement are not reachable.- Example 1:
- Example 2:
- Example 3:
(3) Parameters and Arguments
- A
parameter(形式参数) is a variable declared in the header of a method or constructor and can be used inside the body of the method. - An
argument(实际参数) is a value that is passed into a method when the method is called. - The
argumentspassed to a method must becompatiblein number and order with the types identified in theparameterlist of the method signature. - Example:
- declare a method: public void sum(int x, int y){}
- call a method: sum(10,20);
- In this example:
- x, y are parameters
- 10, 20 are arguments
(4) Method Overloading(方法重载)
- Methods are said to be
overloadedwhen there are multiple methods with the same name but different signatures (i.e. different parameter list).
(5) Constructors(构造函数)
- A
constructoris a special method inside a class. - A class contains constructors that
are called to create objects. - Example:
- Student s1 = new
Student(); - Student s2 = new
Student("xiaoming");
- Student s1 = new
A constructor has the following features:- has the
same name as the classin which it is declared. - has
no return type. - is
called when an object is created using the new keyword. can be overloaded, meaning there can be multiple constructors with different signatures inside a class.purposeof a constructor: to initialize instance variables or do other initialization work.
- has the
- Example: