1.9 Method Signatures

7 阅读2分钟

1. Exam Points

  • Declare and call instance methods.
  • Pass arguments(实际参数) into parameters(形式参数).
  • The arguments must 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 returned from 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 method is a named 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.
    • methodName is 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.
  • A method signature for a method with parameters consists of the method 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");
    • A method with a return type
      • declare a method: public int square(int x){ return x*x; }
      • call a method: int x = square(4);
  • 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 statement within a method. Ex. rerurn 3;
    • A method can have more than one return statement, but only 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 must match the declared return type of 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 back to where the method is called.
    • Statements after a return statement are not reachable.
    • Example 1:
      image.png
    • Example 2:
      image.png
    • Example 3:
      image.png

(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 arguments passed to a method must be compatible in number and order with the types identified in the parameter list 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 overloaded when there are multiple methods with the same name but different signatures (i.e. different parameter list).

(5) Constructors(构造函数)

  • A constructor is 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");
  • A constructor has the following features:
    • has the same name as the class in 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.
    • purpose of a constructor: to initialize instance variables or do other initialization work.
  • Example:
    image.png

3. Exercises