Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java 程式設計 講師:FrankLin.

Similar presentations


Presentation on theme: "Java 程式設計 講師:FrankLin."— Presentation transcript:

1 Java 程式設計 講師:FrankLin

2 物件導向 上次內容的補充 類別、物件、方法、欄位

3 封裝(Encapsulation) 繼承(Inheritance) 多型(Polymorphism) 封裝:
   基本上我們不需要了解過於底層的程式碼編寫   只需要會運用那些別人封裝好的套件或是類別   舉例,Scanner 繼承:    上次有簡單提到繼承的概念   繼承最簡單來講就是像是父子繼承的概念 多型:    程式碼解釋

4 繼承 交通工具 int 輪胎數; void 前進(); void 後退(); ….

5 封裝 以 Scanner 舉例 Scanner sc = new Scanner(System.in); sc.nextInt();
sc.nextLine(); Sc.nextFloat();

6 多型 由繼承的關係而來的方法,而根據類別的不同會有不同的表現方式 汽車.前進() 機車.前進() 自行車.前進()

7 覆寫(Override)、多載(Overload)
跟多型有關,會很常出現在繼承的時候 多載 同名的方法(method) 不同參數應對不同回傳型態

8 public class Book { private String name; private int price; private int page; private String author; public Book(String author, String name, int price, int page) { this.name = name; this.author = author; this.page = page; this.price = price; } public Book() { this.name = "";this.author = ""; this.page = 0;this.price = ""; @Override public void toString() { System.out.printf("Book name:\t%s\n", this.name); System.out.printf("Author:\t%s\n", this.author); System.out.printf("Page:\t%d\n", this.page); System.out.printf("Price:\t$%d\n", this.price); public int getPrice(){return this.price;} public int getPage(){return this.page;} public String getName(){return this.name;} public String getAuthor(){return this.author;} public void setPrice(int p){ if (p >= 0){ this.price = p; } else { System.err.println("Price Error"); public void setPage(int p){ this.page = p; System.err.println("Page Error"); public void setName(String n){this.name = n;} public void setAuthor(String a){this.author = a;}


Download ppt "Java 程式設計 講師:FrankLin."

Similar presentations


Ads by Google