Presentation is loading. Please wait.

Presentation is loading. Please wait.

方法與類別 in Java.

Similar presentations


Presentation on theme: "方法與類別 in Java."— Presentation transcript:

1 方法與類別 in Java

2 建立新的資料型別:class 決定某一類 objects 的外觀、長相和行為 type ? → class 定義 class
產生此一型別的物件,配置儲存空間 將訊息發送給物件 class ATypeName { /* class body goes there */ } ATypeName a = new ATypeName ();

3 欄位 (fields) 和方法 (methods)
class 中的兩種成員 資料成員 (data members)→欄位 (field) 任何型別物件 基本型別 成員函式 (member functions)→方法 (methods)

4 data members class DataOnly { int i; float f; boolean b; }
DataOnly d = new DataOnly (); d.i = 47; // 參考到一個 object 的 member d.f = 1.1f; // objectReference.member d.b = false;

5 基本成員 (primitive members) 的預設值 (default values)
Primitive type Default boolean false char ‘\u000’ (null) byte (byte)0 short (short)0 int long 0L float 0.0f double 0.0d

6 宣告和建立物件 建立一個 String 的物件 public class App {
public static void main(String[] args) { String s1; s1 = new String(“Hello from Java!”); String s2 = new String(“Hello from Java!”); }

7 String s3 = new String(c1); double double1 = 1.23456789;
char c1[] = {‘H’, ‘i’, ‘,’, ‘t’, ‘h’, ‘e’, ‘r’, ‘e’}; String s3 = new String(c1); double double1 = ; String s4 = String.valueOf(double1); String s5; s5 = s1;

8 範例 class Printer { public void print() {
System.out.println(“Hello from Java!”); } public class App { public static void main(String[] args) { Printer printer1 = new Printer(); printer1.print();

9 說明 同一個檔案裡面宣告並定義兩個類別 Printer 和 App。 同一個檔案裡面只能有一個類別宣告成 public。
用來儲存 public 類別的檔案必須跟該類別的名稱一致,必須叫做 App.java。 一個檔案只能有一個 public 類別,但卻可以有很多個 private 或 protected 類別 。(其實 Java 在編譯階段會將每一個類別都產生一個 .class 檔案出來)

10 資料儲存在類別裡的兩種方式 實體變數 類別變數 與物件相關 對於同一類別的兩個物件(實體),每一個物件裡面的變數與另一個物件無關
對於同一類別的兩個物件,參考到同一個資料並存放同一個值。

11 實體變數的宣告 access class classname [extends …][implements …] {
[access] type instance_variable1; [access] type instance_variableN; }

12 class Data { public String data_string = “Hello from Java!”; } public class App { public static void main(String[] args) { Data data = new Data(); String string = data.data_string; System.out.println(string);

13 class Data { private String data_string = “Hello from Java!”; } public class App { public static void main(String[] args) { Data data = new Data(); String string = data.data_string; System.out.println(string);

14 實體變數的存取性 設定變數的存取性 public: 外界的程式可以存取到它。 private: 只有該成員所在的類別可以存取到它。
protected: 只有該類別本身和同一個類別套件的其他類別以及由該類別衍生出來的類別可以存取,有繼承的概念。 預設: 在同一個 package 均可以存取此變數。

15 圖示說明 David Love Letter David’s GF David’s Son protected (inheritance)
(default) private Others public

16 建立類別變數 一個類別變數的價值就是可以由該類別的所有物件來共享,也就是每一個物件的類別變數都一樣。
由關鍵字 static 來將它宣告為靜態變數。 access class classname [extends …][implements …] { [access] static type instance_variable1; [access] static type instance_variableN; }

17 範例 class Data { public static int intdata = 0; } public class App {
public static void main(String[] args) { Data a, b; a = new Data(); b = new Data(); a.intdata = 1; System.out.println( “The value of b.intdata = ” b.intdata);

18 範例 class Data { public static int intdata = 1;
public static int doubleintdata; static { doubleintdata = 2 * intdata; } } public class App { public static void main(String[] args) { Data a; a = new Data(); System.out.println( “The value of a.doubleintdata = ” + a.doubleintdata);

19 方法 (methods) method: 執行某些事情的方式 決定某個 object 究竟能夠接收什麼樣的訊息 名稱 參數 回傳型別 主體
[access] returnType methodName ( /* argument list */ ) { /* Method body */ }

20 呼叫 method 發送訊息給物件 method 僅能作為 class 的一部份
objectName.methodName(arg1, arg2, arg3) 發送訊息給物件 method 僅能作為 class 的一部份 透過 object,而且能夠執行某個 method,才能呼叫該 method。

21 參數列 (arguments) 外界傳給 method 的資訊,這些資訊以 object 的形式出現,傳遞 object reference。 指定型別和名稱。 如果引數為 string,傳入就必須是個 String object 。 int storage(String s) { return s.length()* 2; }

22 傳文字模式參數給 main 方法 public class App {
public static void main(String[] args) { System.out.println( “Command line arguments …” ); for (int loop_index = 0; loop_index < args.length; loop_index ++) { System.out.println( “Argument” + loop_index + “ = ” + args[loop_index]); }

23 回傳值 (return values) return 的兩件事情 離開這個 method
不打算回傳任何東西:回傳型別指定為 void,可以在任意地點回返。 如果執行過程中產生回傳值,擺在 return 之後。 不論自何處離開,編譯器都會要求回傳適當型別的回傳值。

24 範例 class Calculator { int addem(int op1, int op2) { return op1 + op2;
} public class App { public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println( “addem(2,2) = ” + calc.addem(2,2));

25 關鍵字 static 沒有產生任何 class object,外界可以呼叫 static method,或是取用其 static data。
特定資料和儲存空間只有一份 某個 method 可以獨立出來使用 將關鍵字 static 擺在 data member或 method 定義前,就可以使它們成為靜態。 class StaticTest { static int i = 47; }

26 StaticTest st1 = new StaticTest();
st1.i = ?, st2.i = ? StaticTest.i++; st1.i = ?, st2.i = ?

27 圖示說明 static boss private student: kelven boss: Prof. Yang DCS DBLab
student: yehwh boss: changyi student: shenjh boss: changyi static boss class DBLab{ private String student; private static String boss; } student: chenhl boss: changyi

28 建立類別方法 // 利用關鍵字 static 來進行 class Calculator {
static int addem(int op1, int op2) { return op1 + op2; } // 直接用類別名稱.類別方法的方式來存取這個 addem 方法 public class App { public static void main(String[] args) { System.out.println( “addem(2,2) = ” + Calculator.addem(2,2));

29 類別方法當作物件方法使用 class Calculator { static int addem(int op1, int op2) {
return op1 + op2; } public class App { public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println( “addem(2,2) = ” + calc.addem(2,2));

30 static method 將一個方法宣告成 static(包括程式的 main 方法),它只能夠呼叫靜態方法,只能存取靜態資料。
不能夠使用關鍵字 this (目前所在位置的參考), super (上層父物件的參考)。

31 以 constructor 確保初始化的進行
initialize 在物件被使用之前先被喚起 constructor 當物件被產生時,此式會自動被喚起

32 最簡單的 constructor class Data { private String data_string;
public Data() { data_string = “Hello from Java!”; } public String getData() { return data_string; public class App { public static void main(String[] args) { System.out.println((new Data()).getdata());

33 傳參數給 constructor class Data { private String data_string;
public Data(String s) { data_string = s; } public String getData() { return data_string; public class App { public static void main(String[] args) { System.out.println((new Data( “Hello from Java!” )).getdata());

34 變數的有效範圍 三種變數的有效範圍 類別階層的有效範圍(class-level scope)
資料成員以及傳進去方法的參數都可以在該類別的方法裡存取。 方法階層的有效範圍(method-level scope) 從該方法的進入點開始,直到方法結束為止。 在此範圍所宣告的變數只能在此方法中存取。 程式區塊階層的有效範圍(code-block scope) 限制在程式區塊內。 變數只能該程式區塊中以及它所包含的其他程式區塊來存取。

35 變數 靜態變數 動態變數 存放在程式的資料配置區 (data allocation)裡面 程式執行的時候就是靜態變數的有效範圍
有效範圍執行到這個程式區塊開始,一直到離開程式區塊為止 變數是在該程式區塊的進入點建立並存放於區域堆疊 (local stack) 上,而在離開點銷毀。

36 class Class { int int1 = 1; // 類別階層 public void method(int int2) { // 方法階層 int int3 = 3; // 方法階層 if (int1 != int2) { int int4 = 4; // 程式區塊階層 System.out.println( “int1 = ” + int1 + “int2 = ” + int2 + “int3 = ” + int3 + “int4 = ” + int4); } public class App { public static void main(String[] args) { Class c = new Class(); c.method(2);

37 Method Overloading class Calculator { int addem(int op1, int op2) {
return op1 + op2; } int addem(int op1, int op2, int op3) { return op1 + op2 + op3; public class App { public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println( “addem(2,2) = ” + calc.addem(2,2)); System.out.println( “addem(2,2,2) = ” + calc.addem(2,2,2));

38 Constructor Overloading
class Data { private String data_string; public Data(char[] c) { // constructor 1 data_string = new String(c); } public Data(String s) { // constructor 2 data_string = s; public String getData() { return data_string;

39 Constructor Overloading (Cont.)
public class App { public static void main(String[] args) { char ch1[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’}; System.out.println( (new Data(ch1)).getdata()); (new Data(“Hello from Java!”)).getdata()); }

40 傳物件給 method passing by reference
class Data { String data_string; public Data(String s) { data_string = new String(s); } class Printer { public void rewrite (Data d) { d.data_string = “Hello to Java!”;

41 傳物件給 method passing by reference (Cont.)
public class App { public static void main(String[] args) { Data d = new Data( “Hello from Java!” ); Printer p = new Printer(); p.rewrite(d); System.out.println(d.data_string); }

42 傳陣列給 method class Calculate { public void doubler(int a[]) {
for (int i = 0; i < a.length; i ++) { a[i] *= 2; } public class App { public static void main(String[] args) { int TArray[] = {1, 2, 3, 4, 5}; Calculate c = new Calculate(); // print the content of TArray c.doubler(TArray);

43 關鍵字 this:指到自己這個物件 class Point { // Point 類別 int x, y; // 宣告物件變數
/** constructor */ Point(int x, int y) { this.x = x; // 令變數 x = 參數 x this.y = y; // 令變數 y = 參數 y } void showPoint() { System.out.println( "(" + x + ", " + y + ")" );

44 從方法傳回物件 class ObjectFactory { public String tag = “Hello Java!”;
private ObjectFactory() {} public static ObjectFactory getInstance() { return new ObjectFactory(); } public class App { public static void main(String[] args) { ObjectFactory o = ObjectFactory.getInstance(); System.out.println(o.tag);

45 從方法傳回陣列 class ArrayFactory { public int[] getNewArray() {
int a[] = {1, 2, 3, 4, 5}; return a; } public class App { public static void main(String[] args) { ArrayFactory af = new ArrayFactory(); int TArray[] = af.getNewArray(); for (int i = 0; i < TArray.length; i ++) { System.out.println(TArray[i]);


Download ppt "方法與類別 in Java."

Similar presentations


Ads by Google