งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 Java Programming Language.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 Java Programming Language."— ใบสำเนางานนำเสนอ:

1 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 Java Programming Language Chapter 8 Objects,Classes,inheritance and Exception

2 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 2 OO Programming Concepts Object-oriented programming (OOP) จะเป็นการ เขียนโปรแกรมที่มีความเกี่ยวข้องกับการใช้งาน Object. การสร้าง object(instance) โดยใช้คำสั่ง new โดยจะ ทำการเรียก Constructor ของคลาสนั้นๆขึ้นมา ทำงานเป็นอันดับแรก สามารถสร้าง object และสามารถเรียกใช้ตัวแปรหรือ method ในคลาสนั้นๆได้โดยเรียกผ่านชื่อ Object ที่ สร้าง Object จะเป็นตัวแทนของ Class ประกอบด้วย data fields (Attribute) และ methods. (Behavior)

3 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 3 Objects สามารถสร้าง object ได้จาก class ที่สร้างไว้โดย object จะเป็นตัวแทนของ class นั้น และสามารถ เข้าถึงตัวแปรและเมธอดของคลาสนั้นได้ผ่านชื่อของ object.

4 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 4 Classes

5 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 5 UML Class Diagram

6 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 6 Constructors Circle() { } Circle(double newRadius) { radius = newRadius; } Constructors เป็น methods ชนิดพิเศษ และจะถูกเรียกใช้ ทันทีที่มีการสร้าง objects. Constructors เป็นเมธอดที่มีชื่อเดียวกับชื่อ คลาส. Constructors เป็นเมธอดที่ไม่มีการ return ค่า และไม่ต้องใส่ type เป็น void.

7 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 7 Declaring Object Reference Variables รูปแบบการประกาศตัวแปร object : ClassName objectRefVar; Example: Circle myCircle;

8 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 8 Declaring/Creating Objects in a Single Step ClassName objectRefVar = new ClassName(); Example: Circle myCircle = new Circle(); Create an object Assign object reference รูปแบบการสร้าง object.

9 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 9 Accessing Objects F การอ้างอิง Variable ของ object’s : objectRefVar.data e.g., myCircle.radius F การเรียกใช้ method ของ object’s : objectRefVar.methodName(arguments) e.g., myCircle.getArea()

10 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 10 The Date Class Java provides a system-independent encapsulation of date and time in the java.util.Date class. You can use the Date class to create an instance for the current date and time and use its toString method to return the date and time as a string.

11 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 11 The Date Class Example For example, the following code java.util.Date date = new java.util.Date(); System.out.println(date.toString()); displays a string like Sun Mar 09 13:50:19 EST 2003.

12 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 12 The this Keyword F ใช้ในการอ้างอิงถึง data field ในคลาส เดียวกัน. F ใช้ในการเรียก overloaded constructor ใน คลาสเดียวกัน.

13 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 13 Serving as Proxy to the Calling Object

14 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 14 Calling Overloaded Constructor

15 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 15 Example: The Course Class Course ให้นักศึกษาสร้าง Class และเมธอดจาก UML ที่กำหนดให้

16 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 16 inheritance F เป็นการถ่ายทอดคุณสมบัติจากคลาสหนึ่งสู่ อีกคลาสหนึ่ง F คลาสที่ถ่ายทอดเรียกว่าคลาสแม่ (superclass) F คลาสที่ได้รับการถ่ายทอดเรียกว่าคลาสลูก (subclass) F ภาษาจาวาสนับสนุนกลไกนี้โดยมีคำสั่งการ ถ่ายทอดคุณสมบัติโดยใช้ keyword “extends” ดัง รูปแบบดังนี้ class subclass extends superclass F subclass จะได้รับการถ่ายทอดคุณสมบัติของตัวแปรและ method จาก superclass ทุกประการยกเว้น ตัวแปร private และ constructor

17 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 17 inheritance F รูปแบบการเขียนโปรแกรม class Subclass extends SuperClass { Data Member; Method Member; }  Subclass : คือชื่อคลาสที่ถูกตั้งขึ้นเพื่อเป็นคลาสสืบ ทอด  SuperClass : คือชื่อคลาสที่ต้องการนำมาเป็นคลาส สืบทอด  extends : เป็นคีย์เวิรด์ที่บอกว่าเป็นการสร้างซับ คลาส

18 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 18 super F ใช้อ้างชื่อสมาชิกของคลาสกรณีที่ subclass กับ superclass ใช้สมาชิกชื่อเดียวกัน F subclass สามารถเรียกใช้ constructor ของ superclass ได้เลยโดยที่ subclass ไม่ต้องสร้าง constructor ขึ้นมาใหม่ F รูปแบบ super.member; super(parameter);

19 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 19 Final F Keyword “final” ใช้นำหน้าคลาส, ตัวแปร, เมธอด ซึ่งมีประโยชน์ดังนี้  ถ้านำหน้าตัวแปรทำให้ตัวแปรนั้นเป็นค่าคงที่ ( เปลี่ยนแปลงไม่ได้ )  ถ้านำหน้าเมธอดทำให้เมธอดนั้นไม่สามารถถูก override ได้  ถ้านำหน้าคลาสทำให้คลาสนั้นไม่สามารถ inheritance ได้

20 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 20 Abstract Class F เป็นคลาสที่มี keyword “abstract” อยู่หน้า class F สมาชิกภายในคลาสเป็น abstract method F ไม่สามารถนำไปสร้าง object ได้จนกว่าจะทำการ override method นั้นๆให้ครบก่อน F รูปแบบ abstract class classname{.... }

21 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 21 Abstract Class F รูปแบบการเขียนโปรแกรม abstract class classname{ abstract Data Member; abstract Method Member; } F abstract : เป็นคีย์เวิรด์สำหรับกำหนดให้คลาส มีคุณสมบัติ abstract

22 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 22 Abstract Class F การใช้งาน abstract class เมื่อมีการ extends จากคลาสที่ เป็น abstract ต้องทำการ Override Abstract method ทุกตัว ใน Abstract Class ให้ครบทุกตัวจึงจะสามารถสร้าง Object จากคลาสนั้นได้ F รูปแบบการทำการ Override Method class classname extends AbstractClass { AbstractMethodMember(){ Statement; } ตัวอย่างการเขียนโปรแกรม Test3.javaTest3.java

23 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 23 Interface Class F คือคลาสที่ใช้ keyword “interface” แทน keyword “class” F สมาชิกภายในจะมีเฉพาะตัวแปรที่เป็นค่าคงที่ (final) และ abstract method เท่านั้น F ต้องทำการ override method ภายใน interface class ให้ครบจึงสามารถจะทำงานได้ F มีประโยชน์ในการทำ multiple inheritance โดยใช้ keyword “implements” เช่น class ActionEvent1 extends JFrame implements ActionListener

24 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 24 Interface Class F รูปแบบการเขียนโปรแกรม public interface interfaceName{ Data Member; public void Methodname(); } –interace : เป็นคีย์เวิรด์สำหรับกำหนดให้เป็น interace class –interaceName : คือชื่อคลาสอินเตอร์เฟส – ตัวอย่างการสร้าง TestInterface.java TestInterface.java

25 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 25 Interface Class F การใช้งานอินเตอร์เฟส เมื่อมีการถ่ายทอด คุณสมบัติจาก interface class F รูปแบบ class ClassName extends superclass implements InterfaceName{ Data Member; AbstractMethodName{ statement; } F implements : คือคีย์เวิรด์สำหรับการนำ interface class มาใช้

26 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 26 Exception Handler  เป็นกลไกของภาษาเมื่อมีความผิดพลาด เกิดขึ้นในขณะที่โปรแกรมทำงานจะมีการโยน (throw) ออกมาที่ตำแหน่งนั้น  ถ้าไม่มีการดักจับ throw ตัวนี้จะถูกโยนไป ตามลำดับจนถึง Java Interpreter  ภาษาจาวามีกลไกในการดักจับ throw เรียกว่า Exception Handing มีรูปแบบดังนี้

27 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 27 Exception Handler F รูปแบบ try { Statement; // ที่สงสัยว่าจะเกิด Exception } catch (ExceptionClass e) { Statement; // ที่จะให้กระทำเมื่อเกิด Exception }

28 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 28 The finally Clause try { statements; } catch(TheException ex) { handling ex; } finally { finalStatements; }

29 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 29 Trace a Program Execution animation try { statements; } catch(TheException ex) { handling ex; } finally { finalStatements; } Next statement; Suppose no exceptions in the statements

30 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 30 Trace a Program Execution animation try { statements; } catch(TheException ex) { handling ex; } finally { finalStatements; } Next statement; The final block is always executed

31 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 31 Trace a Program Execution animation try { statements; } catch(TheException ex) { handling ex; } finally { finalStatements; } Next statement; Next statement in the method is executed

32 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 32 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } finally { finalStatements; } Next statement; Suppose an exception of type Exception1 is thrown in statement2

33 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 33 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } finally { finalStatements; } Next statement; The exception is handled.

34 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 34 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } finally { finalStatements; } Next statement; The final block is always executed.

35 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 35 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } finally { finalStatements; } Next statement; The next statement in the method is now executed.

36 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 36 Exception Handler  ประโยคใน Try Block คือประโยคที่ส่ง exception ออกมาเมื่อเกิดความผิดปกติตอน โปรแกรมทำงาน  ประโยคใน catch block คือประโยดที่จัดการ กับ exception ที่เกิดขึ้นในประโยค try block  การดักจับ exception สามารถใช้ class Exception ที่เป็นคลาสแม่ตั้งรับได้ซึ่ง สามารถรับ exception ของคลาสใดๆ


ดาวน์โหลด ppt Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 Java Programming Language.

งานนำเสนอที่คล้ายกัน


Ads by Google