Presentation is loading. Please wait.

Presentation is loading. Please wait.

類別的繼承 Vehicle Car.

Similar presentations


Presentation on theme: "類別的繼承 Vehicle Car."— Presentation transcript:

1 類別的繼承 Vehicle Car

2 類別的繼承 Vehicle Truck Car Motorcycle 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 { ....... 覆寫(override)與
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 Vehicle // Vehicle類別宣告 { // 成員資料
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("圓面積: " *r); } <<abstract>> Shape +x:double +y:double area() Circle +r:double +area()

11 多形 ch6_3 Circle Shape.area() Shape.area() Shape.area() Triangle
class Circle extends Shape { public void area(){.....} } class Rectangle extends Shape class Triangel extends Shape 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 = f; print() print() Quiz Final print() print()

13 隨堂練習 T1 +a : int +b : int +c : int T1 t1 = new T1(6, 6, 6);
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 = t3; +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>>
類別的繼承 Java的介面 <<interface>> IPrice getPrice():double Car -price:double -name:String +getPrice():double +getName():String

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

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

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

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

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

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

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

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

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 sum() <<interface>> I2
printout() FloatArray -buf: float[] -no : int +sum() IArray -buf: int [] -no : int +sum() +printout()

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


Download ppt "類別的繼承 Vehicle Car."

Similar presentations


Ads by Google