1 / 26

類別的繼承

Vehicle. Car. 類別的繼承. Vehicle. Car. Motorcycle. Truck. SportsCar. Jeep. 類別的繼承. 類別的繼承. class Car { private int engineNum; private int color; public void setNum(int num){engineNum = num;} public void setColor(int color){ this.color = color;} public void printVehicle(){ } }.

wayne
Download Presentation

類別的繼承

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. Vehicle Car 類別的繼承

  2. Vehicle Car Motorcycle Truck SportsCar Jeep 類別的繼承

  3. 類別的繼承 class Car { private int engineNum; private int color; public void setNum(int num){engineNum = num;} public void setColor(int color){ this.color = color;} public void printVehicle(){ } }

  4. 類別的繼承 class 子類別 名稱 extends 父類別名稱 { ....//額外的成員資料和方法 } class Nissan extends Car { private double price; public Nissan (int num, int color , double price) { } public void printNissan() { } }

  5. 類別的繼承 • 子類別不能存取父類別宣告成private 的成員資料和方法。 • 父類別的建構子不屬於類別的成員, 所以子類別並不能繼承父類別的建構 子,只能呼叫父類別的建構子。

  6. 類別的繼承 class Car { ....... public static void printName(){...}//隱藏 ....... public void printCar(){...}//覆寫 } class Nissan extends Car { ....... public Static void printName(){....} ....... public void printCar() {....} } 覆寫(override)與 隱藏(hide)父類別的方法

  7. 類別的繼承 隱藏父類別的成員資料 class Car { ….. public int color; ….. } class Nissan extends Car { private String color; ….. }

  8. 類別的繼承 使用父類別的建構子 class Car extends Vehicle // Car類別宣告 { private String name; // 隱藏父類別的成員變數 private int engineNo; private int doors; // 幾門 public Car(String name, int no, int doors) { super(name, no); // 呼叫父類別的建構子 ……… } public void printVehicle() { ………. super.printVehicle(); System.out.println("名稱(父): " + super.name); System.out.println("引擎號碼(父): " + super.engineNo); ……….. } } class Vehicle // Vehicle類別宣告 { // 成員資料 public static int count; // 車輛數 public int engineNo; // 引擎號碼 public String name; // 名稱 // 建構子 public Vehicle(String name, int no) {…………………} // 成員方法: 顯示車輛資料 public void printVehicle() {…………………} }

  9. 常數與抽象類別 • final class Customer extends Person • { • ..... • } • class Person • { • ..... • final char getName(){return nmae;} • final int getAge(){return age;} • final void setName(char c){ name = c; } • final void setAge(int age) { this.age = age; } • }

  10. <<abstract>> Shape +x:double +y:double area() 常數與抽象類別 • abstract class Shape • { • public double x; • public double y; • abstract void area(); • } • class Circle extends Shape • { • public double r; • public Circle(double x, double y, double r) { ..... } • public void area() • { System.out.println("圓面積: " + 3.1416*r); } • } Circle +r:double +area()

  11. 多形 • class Circle extends Shape • { • ....... • public void area(){.....} • ....... • } • class Rectangle extends Shape • { • ....... • public void area(){.....} • ....... • } • class Triangel extends Shape • { • ....... • public void area(){.....} • ....... • } Circle Shape.area() ch6_3 Shape.area() Rectangle Shape.area() Triangle

  12. 隨堂練習 Abstract<<Test>> MidTerm +max : int +min : int +avg : float MidTerm m = new MideTerm(90, 45, 67.8); Quiz q = new Quiz(70, 34, 78.5); Final f = new Final(87, 64, 76.3); Test t = m; t.print(); t = q; t.print(); t = f; t.print(); print() print() Quiz Final print() print()

  13. 隨堂練習 Abstract<<Triangle>> T1 +a : int +b : int +c : int T1 t1 = new T1(6, 6, 6); T2 t2 = new T2(3, 4, 5); T3 t3 = new T3(8, 8, 12); T t = t1; t.area(); t = t2; t.area(); t = t3; t.area(); +area : float area() T3 T2 +area : float +area : float

  14. import java.lang.*; • public class Ch1_4_1 • { // 主程式 • public static void main(String[] args) • { // 顯示訊息 • System.out.println(Math.sqrt(9)); • } • }

  15. <<interface>> IPrice Car getPrice():double -price:double -name:String +getPrice():double +getName():String 類別的繼承 Java的介面

  16. 類別的繼承 介面的宣告 public interface 介面名稱 { final 資料型態 常數值 ; ….. 傳回值型態 介面方法(參數列) ; ….. } Interface IArea { final double PI=3.1415926; void area(); }

  17. 類別的繼承 類別實作介面 Class 類別名稱 implements 介面名稱1、介面名稱2 { ….. //實作介面的方法 } Class Circle implements IArea { …… public void area() { System.out.println(“圓面積 :”+PI*r*r); } }

  18. <<interface>> IArea Circle -x:double -y:double -r :double area() +area() 類別的繼承 Ch_9_2_2

  19. <<interface>> IArea <<interface>> IShow area() show() 類別的繼承 Ch_9_2_3 在類別實作多個介面 Circle -x:double -y:double -r :double +area() +show()

  20. Vehicle -engineNo:int -name:String -price:double Car +print() <<interface>> IPrint print() -door:int +print() 類別的繼承 類別架構與介面 Ch_9_2_4

  21. <<interface>> IArea <<interface>> IShape area() perimeter() 類別的繼承 Ch9_3 Circle -x:double -y:double -r :double +area() +perimeter()

  22. 類別的繼承 介面的多重繼承 interface 介面名稱 extends 繼承介面1,繼承介面2 { …..//額外的常數和方法 } interface Ishape extends IArea, IShow { void perimeter(); }

  23. <<interface>> IShow <<interface>> IArea <<interface>> IShape show() perimeter() area() Circle -x:double -y:double -r :double +area() +show() +perimeter() 類別的繼承 Ch_4_2

  24. Student Class Student { private int stdNo; … public float score; public Student(…) { ….} public int getNo() {…..} public String getName() {…..} public String getAddress() {…..} } Class GraduateStudent { private String department; public GraduateStudent(…) { super.stdNo = no; …… } public int getNo() { System.out.println(“No” + super.getNo()); …… } }

  25. <<interface>> I1 <<interface>> I2 sum() printout() IArray FloatArray -buf: int [] -no : int -buf: float[] -no : int +sum() +printout() +sum() 練習

  26. <<interface>> IArea area() Triangle Triangle1 Triangle2 Triangle3 -a: Point -b : Point -c: Point +area() +printout() +area() +area() +area() 練習

More Related