National Taiwan University Department of Computer Science and

  • Slides: 40
Download presentation
National Taiwan University Department of Computer Science and Information Engineering Interfaces and Packages Lecturer:楊昌樺

National Taiwan University Department of Computer Science and Information Engineering Interfaces and Packages Lecturer:楊昌樺

National Taiwan University Department of Computer Science and Information Engineering Outline 抽象(Abstract) 抽象類別(Abstract Classes)

National Taiwan University Department of Computer Science and Information Engineering Outline 抽象(Abstract) 抽象類別(Abstract Classes) 抽象方法(Abstract Methods) 巢狀類別(Nested Classes) 靜態巢狀類別(static nested classes) 內部類別(inner classes) Your Turn 介面(Interfaces) 套件(Packages) Your Turn

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) 定義 類別內部有尚未定義實作內容的方法,則此類別必須宣

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) 定義 類別內部有尚未定義實作內容的方法,則此類別必須宣 告為 abstract class,該方法需宣告為 abstract method 宣告 abstract class Number { … } 特性 宣告為 abstract 的類別無法產生實體。例如,您無法對上 述類別做: Number my. Number = new Number(); // 會發生錯誤

National Taiwan University Department of Computer Science and Information Engineering 抽象方法(Abstract Methods) 沒有實作內容的 methods

National Taiwan University Department of Computer Science and Information Engineering 抽象方法(Abstract Methods) 沒有實作內容的 methods (即沒有 method body) abstract classes 至少必須提供一個完整或部分程式碼的 method 若一個 abstract class 都只有宣告 abstract methods 的話, 那就必須改宣告成介面(interface) 抽象類別主要是用來在繼承上,繼承的 subclass 必須將 abstract classes 中的 abstract methods 實作內容補上

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) 範例: Graphics.

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) 範例: Graphics. Object 是一個抽象類別,提供所有 subclass 必備 的成員變數與 methods abstract class Rectangle Graphics. Object Circle Point Origin; move. To(); abstract draw(); Line // 重心 // 移動 // 繪出

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) public abstract

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) public abstract class Graphics. Object protected Point Origin; { public Graphics. Object() { Origin = new Point(0, 0); } public void move. To(int new. X, int new. Y) { Origin. x = new. X; Origin. y = new. Y; } public abstract void draw(); // abstract method protected void finalize() throws Throwable { Origin = null; super. finalize(); } }

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) class Rectangle

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) class Rectangle extends Graphics. Object { public int Length; public int Height; public Rectangle() { Length = 0; Height = 0; } public Rectangle(int Left, int Top, int Len, int High) { Origin. x = Left; Origin. y = Top; Length = Len; Height = High; } public void draw() { // 必須填上 abstract method 的實作內容 System. out. println("Rectangle has been drawn at n" + "(" + Origin. x + ", " + Origin. y + ") - (" + (Origin. x+Length) + ", " + Origin. y + ")n" + "(" + Origin. x + ", " + (Origin. y+Height) + ") - (" + (Origin. x+Length) + ", " + (Origin. y+Height) + ")"); } }

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) class Circle

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) class Circle extends Graphics. Object public int Radius; { public Circle() { Radius = 0; } public Circle(int a, int b, int r) { Origin. x = a; Origin. y = b; Radius = r; } public void draw() { // 必須填上 abstract method 的實作內容 System. out. println("Circle has been drawn at n" + "Center: (" + Origin. x + ", " + Origin. y + ")n" + "Radius: " + Radius); } }

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) class Line

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) class Line extends Graphics. Object { public Point End; public Line() { End = new Point(0, 0); } public Line(int a, int b, int c, int d) { Origin. x = a; Origin. y = b; End = new Point(c, d); } public Line(Point p 1, Point p 2) { Origin. x = p 1. x; Origin. y = p 1. y; End = new Point(p 2. x, p 2. y); } public void draw() { // 必須填上 abstract method 的實作內容 System. out. println ("Line: (" + Origin. x + ", " + Origin. y + ") - (" + End. x + ", " + End. y + ")"); } }

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) 測試主程式:Draw. Demo.

National Taiwan University Department of Computer Science and Information Engineering 抽象類別(Abstract Classes) 測試主程式:Draw. Demo. java public class Draw. Demo { public static void main(String[] args){ Rectangle rect. Obj = new Rectangle(1, 2 , 5); Circle cir. Obj = new Circle(1, 1, 5); Line line. Obj = new Line(1, 1, 2, 2); rect. Obj. draw(); cir. Obj. draw(); line. Obj. draw(); } }

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) Java 中

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) Java 中 可以將一個類別變成另一個類別的成員 如下的程式碼稱為巢狀類別 class Enclosing. Class { … class ANested. Class { … } }

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 巢狀類別 如同其他成員一樣,巢狀類別也可以宣告成

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 巢狀類別 如同其他成員一樣,巢狀類別也可以宣告成 static 宣告為 static 的巢狀類別,稱為靜態巢狀類別(static nested class) 非 static 的巢狀類別,稱為內部類別(inner class) class Enclosing. Class {. . . static class Static. Nested. Class {. . . } class Inner. Class {. . . } }

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 靜態巢狀類別(static nested

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 靜態巢狀類別(static nested class) 與 class variables, class method 一樣都是屬於類別,而非 屬於物件 與 class method 一樣,在靜態巢狀類別中也不能直接使用 實體變數(instance variables) 反應的是兩個類別之間的關連性 宣告 存取範圍 public protected (空白) private 類別特性 static abstract final 繼承宣告 class 類別名稱 extends 父類別名稱 implements 父介面名稱

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 靜態巢狀類別(static nested

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 靜態巢狀類別(static nested class) 使用前不用 new 即可使用。 可擁有任何 static 成員。 這個Class也是在Load進JVM的記憶體後就存在 主類別名稱. 內部類別名稱. 方法(參數); 如 Line 2. Data. show. Data("Hi. . . ");

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 內部類別(inner class)

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 內部類別(inner class) 與 instance variables, instance methods 一樣都是屬於物件 反應的是兩個類別實體物件之間的關連性 宣告 存取範圍 public protected (空白) private 類別特性 abstract final 繼承宣告 class 類別名稱 extends 父類別名稱 implements 父介面名稱

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 內部類別(inner class)

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 內部類別(inner class) Inner. Class 的實體只能存活在 Enclosing Class 物件之內 Inner. Class 可以直接使用 Enclosing. Class 物件的實體變數 與 methods(包括宣告成 private 的) class Enclosing. Class {. . . class Inner. Class {. . . } }

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 內部類別(inner class)

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) 內部類別(inner class) 使用前必須先 new 才能使用 不可以擁有 static 成員,但可有 static final 成員。 可以繼承 static 成員。 new一個內部物件 物件instance. 類別名稱 = 物件instance. new 類別名稱(參數); 如 my. Line 2. Style = my. Line 2. new CStyle();

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) Example: Line

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) Example: Line 2. java import java. lang. Math; public class Line 2 { public Point Start; public Point End; public CStyle = null; public Line 2() { Start = new Point(0, 0); End = new Point(0, 0); } public Line 2(int a, int b, int c, int d) { Start = new Point(a, b); End = new Point(c, d); } public Line 2(Point p 1, Point p 2) { Start = new Point(p 1. x, p 1. y); End = new Point(p 2. x, p 2. y); }

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) public class

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) public class CStyle { int Thickness; int Pattern; int Color; public CStyle() { Thickness = 0; Pattern = 0; Color = 0; } public CStyle(int th, int pt, int cl) { Thickness = th; Pattern = pt; Color = cl; } public String draw() { return "Thickness: " + Thickness + "n. Pattern: " + Pattern + "n. Color: " + Color; } }

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) public void

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) public void Create. Style(int th, int pt, int cl) { Style = new CStyle(th, pt, cl); } public void draw() { System. out. println ("Line: (" + Start. x + ", " + Start. y + ") - (" + End. x + ", " + End. y + ")"); } public double area() { return Math. sqrt(Math. pow(End. x - Start. x, 2) + Math. pow(End. y - Start. y, 2)); } protected void finalize() throws Throwable { Start = null; End = null; super. finalize(); } }

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) Line 2

National Taiwan University Department of Computer Science and Information Engineering 巢狀類別(Nested Classes) Line 2 的測試主程式:Draw. Line 2. java public class Draw. Line 2 { public static void main(String[] args) { Line My. Line = new Line(1, 1, 5, 5); My. Line. draw(); System. out. println("Length = " + My. Line. area()); My. Line. Create. Style(3, 3, 3); System. out. println(My. Line. Style. draw()); } }

National Taiwan University Department of Computer Science and Information Engineering Your Turn 假設給定以下類別:Parcel. java,試著在此類別中寫

National Taiwan University Department of Computer Science and Information Engineering Your Turn 假設給定以下類別:Parcel. java,試著在此類別中寫 一個 main() 來測試其中所有巢狀類別的方法。 public class Parcel { class Contents { private int i = 11; public int value() { return i; } } class Destination { private String label; Destination(String where. To) { label = where. To; } String read. Label() { return label; } } static class Data { static void show. Data(String data){ System. out. println(data); } } }

National Taiwan University Department of Computer Science and Information Engineering 介面(Interface) 定義介面 public interface

National Taiwan University Department of Computer Science and Information Engineering 介面(Interface) 定義介面 public interface Stockwatcher { final String sun. Ticker = “SUNW”; final String oracle. Ticker = “ORCL”; final String cisco. Ticker = “CSCO”; void value. Changed(String ticker. Symbol, double new. Value); … }

National Taiwan University Department of Computer Science and Information Engineering 介面(Interface) 範例: Graphics. Object

National Taiwan University Department of Computer Science and Information Engineering 介面(Interface) 範例: Graphics. Object Rectangle Point Origin; move. To(); abstract draw(); Circle Point Origin; move. To(); abstract draw(); Paintable Point Origin; move. To(); abstract draw(); Line Point Origin; move. To(); abstract draw(); void fill. Color (int Color); double get. Area(); // 重心 // 移動 // 繪出

National Taiwan University Department of Computer Science and Information Engineering 介面(Interface) 範例:Paintable. java public

National Taiwan University Department of Computer Science and Information Engineering 介面(Interface) 範例:Paintable. java public interface Paintable { public void fill. Color(String Color); public double get. Area(); }

National Taiwan University Department of Computer Science and Information Engineering 介面(Interface) 範例:Rectangle 2. java

National Taiwan University Department of Computer Science and Information Engineering 介面(Interface) 範例:Rectangle 2. java public class Rectangle 2 extends Graphics. Object implements Paintable { public int Length; public int Height; public Rectangle 2() { Length = 0; Height = 0; } public Rectangle 2(int Left, int Top, int Len, int High) { Origin. x = Left; Origin. y = Top; Length = Len; Height = High; }

National Taiwan University Department of Computer Science and Information Engineering 介面(Interface) public void draw()

National Taiwan University Department of Computer Science and Information Engineering 介面(Interface) public void draw() { System. out. println("Rectangle has been drawn at n" + "(" + Origin. x + ", " + Origin. y + ") - (" + (Origin. x+Length) + ", " + Origin. y + ")n" + "(" + Origin. x + ", " + (Origin. y+Height) + ") - (" + (Origin. x+Length) + ", " + (Origin. y+Height) + ")"); } public void fill. Color(String Color) { System. out. println("Rectangle has been filled with " + Color); } public double get. Area() { return Length * Height; } }

National Taiwan University Department of Computer Science and Information Engineering 套件(Package) 定義 性質相似之類別的集合 宣告套件的方法

National Taiwan University Department of Computer Science and Information Engineering 套件(Package) 定義 性質相似之類別的集合 宣告套件的方法 在原始檔案的第一行寫下: package 套件名稱; 套件示意圖 Graphics 套件 XXX. java YYY. java ZZZ. java package Graphics; Class XXX { } Class YYY { } Class ZZZ { }

National Taiwan University Department of Computer Science and Information Engineering 套件(Package) 套件目的 防止兩位程式師取了相同的類別名稱 套件命名習慣

National Taiwan University Department of Computer Science and Information Engineering 套件(Package) 套件目的 防止兩位程式師取了相同的類別名稱 套件命名習慣 將公司的網際網路名稱倒過來寫 範例:package tw. com. pcschool. Graphics 有時會再加上部門名稱 範例:package tw. com. pcschool. rd. Graphics

National Taiwan University Department of Computer Science and Information Engineering 套件(Package) 在 Java 中規定,.

National Taiwan University Department of Computer Science and Information Engineering 套件(Package) 在 Java 中規定,. class 檔案真正存在的地方必須與 套件的命名結構相對應 Point. java Point. class package tw. com. pcschool. Graphics; class Point { …. . } tw com pcschool Graphics Point. class

National Taiwan University Department of Computer Science and Information Engineering 套件(Package) package 與目錄結構 所有屬於

National Taiwan University Department of Computer Science and Information Engineering 套件(Package) package 與目錄結構 所有屬於 packagename 類別庫的. class 檔案都必須儲存在 packagename 資料夾下 packagename 必須唯一,類別庫名稱可使用句點分隔不同層次的資料 夾名稱 • 例: package tw. com. pcschool. Graphics package tw. com. pcschool. rd. Graphics 使用 javac 之參數 語法:javac –classpath user-class-path –d des-path xxx. java -classpath:指定使用者自訂類別路徑,至存放類別目錄的上一層 -d:指定編譯完成之. class 檔案產生路徑 若指定路徑中的名稱包含空白,必須使用雙引號括起來

National Taiwan University Department of Computer Science and Information Engineering 套件(Package) Source 與 Class

National Taiwan University Department of Computer Science and Information Engineering 套件(Package) Source 與 Class 檔的管理 Source 檔與 Class 不一定要放在同一個目錄下 將 source 與 class 檔分開放

National Taiwan University Department of Computer Science and Information Engineering 註:套件(Package)Command版 編譯時能夠抓到我們自訂的套件,要用 classpath 參數告訴

National Taiwan University Department of Computer Science and Information Engineering 註:套件(Package)Command版 編譯時能夠抓到我們自訂的套件,要用 classpath 參數告訴 javac 到哪裡去找我們要的套件 在 javac 後方直接用 –classpath 參數 javac –classpath. ; c: jdk 1. 3lib; c: mylibtwcom pcschoolGraphics Point. java 使用 jar 壓縮檔案 語法:jar cvf jar-file-name –C directory-path/. 在 cmd 視窗中,輸入 jar,並參閱其說明 使用類別庫 語法:java –classpath. ; user-class-path main-class 用分號「; 」區隔多個類別庫 類別庫可為目錄路徑、zip 檔案或 jar 檔案 目錄擺放結構一定要正確!

National Taiwan University Department of Computer Science and Information Engineering 用eclipse作套件jar檔 使用Fat Jar的plug-in 將plug-in目錄放在eclipse目錄

National Taiwan University Department of Computer Science and Information Engineering 用eclipse作套件jar檔 使用Fat Jar的plug-in 將plug-in目錄放在eclipse目錄 可在命令列執行eclipse –clean 讓plug-in反映上去 成功後以後Project按右鍵會多 Build Fat Jar選項 選擇Finish即可 別的專案創造時選Library 別的程式import其class

National Taiwan University Department of Computer Science and Information Engineering Your Turn 製作套件及使用練習: 試著將此課堂中所講解的程式碼:Graphics.

National Taiwan University Department of Computer Science and Information Engineering Your Turn 製作套件及使用練習: 試著將此課堂中所講解的程式碼:Graphics. Object. java、 Point. java、Circle. java、Rectangle. java 以及 Line. java 全部都 整合到 tw. com. Graphics 套件中 最前面加 package tw. com. Graphics; 因為要跨專案,所以別程式用到的Point, Circle, Rectangle 等類別要變成public 創造新專案裡面放 Draw. Demo 程式 • 一開始要import tw. com. Graphics. *; • 確認可以正確執行 Draw. Demo裡面的main程式