3.5 Methods: How to Write Them

0 阅读2分钟

1. Exam Points

  • Declare and call methods.
  • Implement encapsulation (getter and setter methods)
  • declare instance variables as private to hide them
  • add accessor methods (getters) to obtain the value of instance variables.
    • Ex: public int getAge() { return age; }
    • has a return type and no parameter, so a value must be returned.
  • add mutator methods (setters) to update the value of instance variables.
    • Ex: public void setAge(int a) { age = a; }
    • has a parameter and no return type
  • Pass parameters
    • For a primitive type parameter, changes to the parameter have no effect on the corresponding argument.
    • For a reference type parameter, changes to the parameter will reflect on the corresponding argument.
    • For a String type parameter, changes to the parameter will not reflect on the argument. String is an immutable class.
  • Note
    • If a method has a return type of non-void, a value must be returned.
    • If a method has a return type of void, do not return a value.
    • Only a method with a return type of non-void can be used in print() or println().

2. Knowledge Points

(1) Understand Methods

  • If you want to reuse a code segment, make it a named method.
  • To use a method, you should declare a method and then call it.
  • Each part of a method:
    • method header: public returnType methodName(parameter list)
    • method body: {} and the code inside {}
  • Example:
    image.png
    • In this example:
      • method header: public int sum(int a, int b)
      • method body: {} and the code inside {}
      • method signature: sum(int a, int b)
      • a, b: parameters(形式参数)
      • 3, 4: arguments(实际参数)

(2) Write Methods

  • A method with no parameter image.png
  • A method with parameters image.png
  • A method with a return type image.png
    • Note: For a method with a return type of void, do not use it in the 3 ways above as a method with a return type.

(3) The return Keyword

  • The return keyword is used to return the flow of control to the point where the method or constructor was called.
  • Purpose of a return statement:
    • return a value
    • return back the flow of control
  • Example: image.png
    • order of statement execution: 1,2,3 -> 7,8 -> 4,5,6
    • output: 12ab034
  • Any code that is sequentially after a return statement will never be executed.
    image.png
  • Executing a return statement inside a selection or iteration statement will halt the statement and exit the method or constructor. image.png
    • In this example, the value of result would be 2.
  • Note:
    • If a method has a return type of non-void, a value must be returned.
    • If a method has a return type of void, do not return a value.
    • Only a method with a return type of non-void can be used in print() or println(). image.png

(4) Accessor(访问器) and Mutator(更改器) methods

  • An accessor method(getter) allows objects of other classes to obtain a copy of the value of instance variables or class variables. (read/get value).
  • An accessor method:
    • has a name of getAttributeName
    • has a return type of the attriburte retrieved
    • has no parameters.
    • Example:
      image.png
  • A mutator method(setter) is a method that changes the values of the instance variables or class variables. (update/change value)
  • A mutator method:
    • has a name of setAttributeName
    • has a return type of void
    • has a parameter of the same type as the attribute
    • Example:
      image.png

(5) Pass Arguments to Parameters

  • For a primitive type parameter, changes to the parameter have no effect on the corresponding argument.
    image.png
  • For a reference type parameter, changes to the parameter will reflect on the corresponding argument.
    image.png
  • Among reference types, String is an exception, because String is an immutable class.
    image.png

3. Exercises