1 / 13

Overload

Overload. Overload ( 多載 , 重載 ) -- 只要 method signature( 方法簽章 ) 不一樣 , 雖然名稱一樣 , 也可以成立 Signature : 方法名稱雖一樣 但 引數個數 不一樣 或 引數資料型態 對應不一樣. Method 的語法. Declaring Classes( 宣告類別 ). 宣告類別 : 告訴他人我這個類別將有哪些屬性及方法

ordell
Télécharger la présentation

Overload

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overload Overload (多載, 重載) --只要method signature(方法簽章) 不一樣, 雖然名稱一樣, 也可以成立 Signature: 方法名稱雖一樣 但引數個數不一樣 或 引數資料型態對應不一樣

  2. Method的語法

  3. Declaring Classes(宣告類別) • 宣告類別: 告訴他人我這個類別將有哪些屬性及方法 • extends: 繼承, 下面例子MyClass可以延用MySuperClass的方法implements實作, 下面例子MyClass要將YourInterface中所宣告的方法, 完成定義

  4. Declaring Member Variables (宣告變數及方法) • Member variables in a class(在類別大括號之間宣告的變數稱為)—these are called fields(屬性). • Variables in a method or block of code(在方法-及副程式大括號之間宣告的變數稱為)—these are called local variables 區域變數(). • Variables in method declarations(在方法-及副程式圓括號之間宣告的變數稱為)—these arecalled parameters(參數). • Access Modifiers(修飾元)  public modifier(公用)—the field is accessible from all classes 所有其他類別皆可用到.  private modifier(私有)—the field is accessible only within its own class 只有自己的類別內部的成員, 如屬性和方法, 才能用.

  5. 建構元Constructor • 建構元: 建立物件的方法 • 建構元是特殊的方法, 不傳回任何型態, 也不傳回 void  • 若類別本身沒有建構元, compiler 會給予一個預設建構元=> 類別名稱(){}

  6. abstract class(抽象類別) • 抽象類別必須以abstract宣告 • 類別其中有一個方法為abstract =>沒有實作{}, 就必須宣告為abstract. • 但也可全部方法都不是abstract • abstract class主要不讓 使用者建立物件 • 抽象類別不能被New起來 (ch11, p11) • 繼承抽象類別的類別必須將方法補齊 (ch11, p8)

  7. Override&Overload 練習http://192.192.246.169/~wells/wiki/index.php/SCJP_1.6%E7%89%88%E8%80%83%E9%A1%8C_012

  8. Override&Overload 練習 • http://nikojava.wordpress.com/2008/10/10/free-scjp-mock-exams/

  9. Override 練習 • Explanation: • An overriding method can throw the same exceptions(相同的例外), narrower exceptions(範圍較小的例外), or no exceptions (無例外)and can throw any runtime exceptions(可以丟出RUNTIME例外)whether they were listed in the overridden method or not. • An overriding method cannot reduce the access scope of the overridden method but can widen it.

  10. Override 練習

  11. Override 練習http://www.tutorialspoint.com/java/java_overriding.htm

  12. Override 練習 import java.io.*; class Test { public void display() throws IOException { } } public class Demo extends Test { public void display() throws Exception { } // throwing super class exception } //error, why

  13. Override 練習 import java.io.*; class Test { public void display() throws IOException { } } public class Demo extends Test { public void display() throws IOException { } }

More Related