Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Programming Lecture 13 Classes I OO Programming.

Similar presentations


Presentation on theme: "Introduction to Java Programming Lecture 13 Classes I OO Programming."— Presentation transcript:

1 Introduction to Java Programming Lecture 13 Classes I OO Programming

2 Objects and Classes  OO Programming Concepts ( 物件導向程式設計的概念 )  Creating Objects and Object Reference Variables ( 建立物件與物件變數 )  Differences between primitive data type and object type  Automatic garbage collection  Constructors ( 建構子 )

3 OO Programming Concepts

4 Class and Objects

5 Class Declaration class 類別名稱 { 資料型態 field 名稱 ; … 傳回值的資料型態 method 名稱 ( 參數清單 ) { 程式敘述 ; … return 運算式 ; } … };

6 Class Declaration class Car { int num; double gas; void show() { System.out.println(" 車號是 " + num + " 。 "); System.out.println(" 汽油量是 " + gas + " 。 "); } // 汽車類別 class 汽車 { 車號 ; 汽油量 ; 換車牌的功能 … 加油的功能 … 顯示車號及汽油量 }

7 Declaring and Creating Objects ClassName objectReference; objectReference = new ClassName(); ( 類別名稱 變數名稱 ; 變數名稱 = new 類別名稱 (); ) Example: Car myCar; myCar = new Car(); Declaring/Creating Objects in a Single Step : Car myCar = new Car(); // ( 在同一行宣告、建立物件 )

8 Differences between variables of primitive Data types and object types

9 Copying Variables of Primitive Data Types and Object Types

10 Garbage Collection 在上個例子中, 我們可以看到 在 c1 = c2 執行後, c1 也指向 c2 所參照的物件. 原來 c1 所參照的物件就無法再被使用到 ( 但卻 會占用記憶體 ). “This object is known as garbage. Garbage is automatically collected by JVM.” 如果知道不會再用到某一個物件, 可以指定 “null” 給這個物件的 reference variable. Java VM 會自動的把佔用的記憶體空間式放出來.

11 Accessing Objects 新物件建立完成後,就可以對物件的成員(或 field )進 存取的動作,包括將特定數值指定給 field 儲存 public static void main(String args[]) { Car car1; car1 = new Car(); car1.num = 1234; car1.gas = 20.5;... } class Car { int num; double gas; }

12 A Simple Car Class // 汽車類別 class Car { int num; double gas; } class Sample1 { public static void main(String args[]) { Car car1; car1 = new Car(); car1.num = 1234; car1.gas = 20.5; System.out.println(" 車號是 " + car1.num + " 。 "); System.out.println(" 汽油量是 " + car1.gas + " 。 "); }

13 Car Class with a Method // 汽車類別 class Car { int num; double gas; void show() { System.out.println(“ 車號 是 " + num + " 。 "); System.out.println(“ 汽油量 是 " + gas + " 。 "); } class Sample2 { public static void main(String args[]) { Car car1; car1 = new Car(); car1.num = 1234; car1.gas = 20.5; car1.show(); }

14 Car Class with Methods // 汽車 class class Car { int num; double gas; void show() { System.out.println(" 車號是 " + num ); System.out.println(" 汽油量是 " + gas ); } void showCar() { System.out.println(" 現在開始顯示汽 車的資料。 "); show(); } class Sample3 { public static void main(String args[]) { Car car1; car1 = new Car(); car1.num = 1234; car1.gas = 20.5; car1.showCar(); }

15 Car Class with “set” Method // 汽車 class class Car { int num; double gas; void setNum(int n) { num = n; System.out.println(" 使車號為 " + num + " 。 "); } void setGas(double g) { gas = g; System.out.println(" 使汽油量為 " + gas + " 。 "); } void show() { System.out.println(" 車號是 " + num + " 。 "); System.out.println(" 汽油量是 " + gas + " 。 "); } class Sample4 { public static void main(String args[]) { Car car1 = new Car(); car1.setNum(1234); car1.setGas(20.5); }

16 More Car Classes Sample5.java Sample6.java


Download ppt "Introduction to Java Programming Lecture 13 Classes I OO Programming."

Similar presentations


Ads by Google