Lecture 10

advertisement
Introduction to Java Programming
Lecture 10
Method
Benefits, Declaring, and Calling Methods
Methods
 Introducing
Methods
– Methods (方法) 的宣告
 Passing
Parameters
– 代入參數 – pass by value (代入數值)
 Overloading
Methods
– Method 的重載
 Scope
of Local Variables
 Method
Abstraction
Method Structure : Declaring Methods (方法的宣告)
Modifiers Return value type
Method name
Parameters
public static int max( int num1, int num2 ) {
int result = 0;
if (num1 > num2)
result = num1;
else
result = num2;
Method body
return result ;
}
Return value
使用 Methods 的好處
• 寫好之後可以在不同的程式中重複使用.
• Information hiding -- Hide the implementation from
the user (Method 的使用者不需知道這個 method 是怎麼寫
的,只要知道怎麼用就行 : method 的功能、需代入的參數
、return 的值) .
•
減低程式的複雜度 – 程式分隔成數個 methods,容易整理,
容易維護,也比較容易理解
Squares1.java Square2.java
Square3.java
Squares 1
// Squares1: 計算平方, 不使用 method
public class Squares1 {
public static void main( String[ ] args ) {
for (int index = 1; index <= 10; index ++)
{
System.out.println("The square of " + index +
" is " + ( index*index ));
}
System.exit(0);
} // end of main
} // end of class squares
Squares 2
/* Squares2 使用 method square */
public class Squares2 {
public static void main( String[] args ) {
for (int index = 1; index <= 10; index ++)
{
System.out.println("The square of " + index +
" is
" + ( square( index )));
System.out.println("The sum of " + index +
"+2 squared is " + ( square( index+ 2)));
System.out.println("The sum of " + index +
" squared+ 2 is " + (( square( index))+2) );
System.out.println();
}
System.exit(0);
} // end of main
// square method declaration follows:
public static int square( int x )
{
int y = x * x;
return y;
} // end of method square
} // end of class squares2
Spuares 3
/* Squares3 This program demonstrates the use of methods with a
return value and two inputs. 帶入兩個參數的 square method*/
public class Squares3 {
public static void main( String[] args ) {
for (int index = 1; index <= 10; index ++)
{
System.out.println(index + " The sum of squares of "+ index
+ " & " + (index+1) + " is " + square( index, index+1 ));
}
System.exit(0);
} // end of main
// square method declaration follows:
public static int square( int x, int y )
{
int a, b;
a = x * x;
b = y * y;
return (a+b);
} // end of method square
} // end of class squares3
Method overloading (重載)
// TestMethodOverloading.java: Demonstrate method overloading
public class TestMethodOverloading {
public static void main(String[] args) {
// 代入兩個 int 的 max method
System.out.println("The maximum between 3 and 4 is " +
max(3, 4));
//代入兩個 double 的 max method
System.out.println("The maximum between 3.0 and 5.4 is " +
max(3.0, 5.4));
//代入三個 double 的 max method
System.out.println("The maximum between 3.0, 5.4, and 10.14 is "
+ max(3.0, 5.4, 10.14));
}
public static int max(int num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, double num2) {
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, double num2, double num3) {
return max(max(num1, num2), num3);
}
}
Pass by Value
// TestPassByValue.java: Demonstrate passing values to methods
public class TestPassByValue {
/** Main method */
public static void main(String[] args) {
// Declare and initialize variables
int num1 = 1;
int num2 = 2;
System.out.println("Before invoking the swap method, num1 is "
+ num1 + " and num2 is " + num2);
// Invoke the swap method to attempt to swap two variables
swap(num1, num2);
System.out.println("After invoking the swap method, num1 is " +
num1 + " and num2 is " + num2);
}
Pass by Value, Cont.
/** Swap two variables */
public static void swap(int n1, int n2) {
System.out.println(" Inside the swap method");
System.out.println(" Before swapping n1 is " + n1
+ " n2 is " + n2);
// Swapping n1 with n2
int temp = n1;
n1 = n2;
n2 = temp;
System.out.println(" After swapping n1 is " + n1
+ " n2 is " + n2);
}
}
Scope of Local Variables
A local variable : 宣告在 method 中的變數.
Scope : 變數的 scope 指的是該變數在整個程式中可被參照到
的部分 (可使用到這個變數的區域).
一個 local variable 的 scope 從這個變數的宣告開始,一直到包
含這個變數的 block 結束為止
同一名稱的變數可宣告在不互相包含於的 blocks 中,但不可在
”nested blocks” 中宣告兩次
Scope of Local Variables, cont.
// Fine with no errors
public static void correctMethod() {
int x = 1;
int y = 1;
// i is declared
for (int i = 1; i < 10; i++) {
x += i;
}
// i is declared again
for (int i = 1; i < 10; i++) {
y += i;
}
}
Scope of Local Variables, cont.
// With compilation errors
public static void incorrectMethod() {
int x = 1;
int y = 1;
for (int i = 1; i < 10; i++) {
int x = 0;
x += i;
}
}
Method Abstraction
You can think of the method body as a black box that
contains the detailed implementation for the
method.
Optional return
value
Optional Input
Method Signature
Method body
Black Box
Download