lec07 :: oop วิชา :: ...
DESCRIPTION
Lec07 :: OOP วิชา :: การเขียนโปรแกรมเชิงวัตถุ. โดน อ. นัฐพงศ์ ส่งเนียม [email protected] http://www.siam2dev.com. Agenda. Imparative Programming : การเขียนโปรแกรมแบบดั้งเดิม OOP Programming : การเขียนโปรแกรมเชิงวัตถุ Imparative VS OOP : เปรียบเทียบข้อแตกต่าง - PowerPoint PPT PresentationTRANSCRIPT
Lec07 :: OOPLec07 :: OOPวิ�ชา วิ�ชา :: :: การเขียนโปรแกรมการเขียนโปรแกรม
เช�งวิ�ตถุ�เช�งวิ�ตถุ�โดน อโดน อ . . น�ฐพงศ์� ส่�งเนยมน�ฐพงศ์� ส่�งเนยม
[email protected]@hotmail.com
http://www.siam2dev.comhttp://www.siam2dev.com
Agenda Agenda
► Imparative Programming : Imparative Programming : การเขียนโปรแกรมแบบการเขียนโปรแกรมแบบด��งเด�มด��งเด�ม
► OOP ProgrammingOOP Programming : : การเขียนโปรแกรมเช�งวิ�ตถุ�การเขียนโปรแกรมเช�งวิ�ตถุ�► Imparative VS OOPImparative VS OOP : : เปรยบเทียบขี!อแตกต�างเปรยบเทียบขี!อแตกต�าง► Fundamental of OOP : Fundamental of OOP : หลั�กการส่$าคั�ญขีอง หลั�กการส่$าคั�ญขีอง OOPOOP
Class : Class : คัวิามหมายขีอง คัวิามหมายขีอง ObjectObject Object : Object : คัวิามหมายขีอง คัวิามหมายขีอง classclass InheritanceInheritance PolymorphismPolymorphism OverloadingOverloading
-Imperative VS Object orien -Imperative VS Object orientedted
ImperativeImperative► - Top down design- Top down design► -Procedure oriented-Procedure oriented► AlgorithmAlgorithm► Share global data Share global data► - Program data separ- Program data separ
ationation► Data transportation Data transportation► Single flow Single flow
-Object oriented-Object oriented► -Bottom up-Bottom up► -Object oriented-Object oriented► BehaviorBehavior► Information hiding Information hiding► EncapsulationEncapsulation► Communication with Communication with
messagesmessages► Multiple objects Multiple objects
OOPOOPImparativeImparative
การเขียนโปรแกรมแบบ การเขียนโปรแกรมแบบ imparativeimparative
ขี!อดขีอง ขี!อดขีอง OOPOOP
► เขี!าใจง�าย เพราะการที$างานเปรยบเส่ม+อนการจ$าลัองเขี!าใจง�าย เพราะการที$างานเปรยบเส่ม+อนการจ$าลัองเหม+อนในโลักจร�ง โดยอาศ์�ยการมองที�กอย�างเป,น เหม+อนในโลักจร�ง โดยอาศ์�ยการมองที�กอย�างเป,น object object ซึ่./งแต�ลัะต�วิมหน!าที/แลัะคัวิามหมายในต�วิซึ่./งแต�ลัะต�วิมหน!าที/แลัะคัวิามหมายในต�วิ
►บ$าร�งร�กษา แลัะแก!ไขีโปรแกรมได!ง�ายบ$าร�งร�กษา แลัะแก!ไขีโปรแกรมได!ง�าย►มคัวิามปลัอดภั�ยส่3ง เพราะจ�ดการก�บ มคัวิามปลัอดภั�ยส่3ง เพราะจ�ดการก�บ error error ได!ดได!ด►น$ากลั�บมาใช!ใหม�ได! น$ากลั�บมาใช!ใหม�ได! (reusability) (reusability) ลัดขี��นตอนในลัดขี��นตอนใน
การเขียนโปรแกรมการเขียนโปรแกรม► โปรแกรมมคั�ณภัาพส่3ง ใช!ได!หลัาย โปรแกรมมคั�ณภัาพส่3ง ใช!ได!หลัาย platformplatform
ขี!อเส่ยขีอง ขี!อเส่ยขีอง OOPOOP
► เขี!าใจยาก ส่$าหร�บผู้3!เร�/มต!นเขียนโปรแกรม หร+อเขี!าใจยาก ส่$าหร�บผู้3!เร�/มต!นเขียนโปรแกรม หร+อถุน�ดเขียนโปรแกรม ถุน�ดเขียนโปรแกรม แบบ แบบ proceduralprocedural
►ที$างานได!ช!ากวิ�าภัาษาโปรแกรมอ+/นที$างานได!ช!ากวิ�าภัาษาโปรแกรมอ+/น►ภัาษามคัวิามก$ากวิม ถุ!ามลั�กษณะ ภัาษามคัวิามก$ากวิม ถุ!ามลั�กษณะ multiple multiple
inheritanceinheritance
หลั�กการของการพั�ฒนาหลั�กการของการพั�ฒนา ซอฟท์�แวร�ด้�วย ซอฟท์�แวร�ด้�วย Object-Oriented Object-Oriented
ProgrammingProgramming คื�อการแบ่�งซอฟท์�แวร�ออกเป็�นส่�วนๆ เร�ยกว�า คื�อการแบ่�งซอฟท์�แวร�ออกเป็�นส่�วนๆ เร�ยกว�า class class โด้ยโด้ย
การน!ยาม การน!ยาม class class แลัะ แลัะ object object ท์�$งน�$เพั�%อท์&าให�ส่ามารถน&าท์�$งน�$เพั�%อท์&าให�ส่ามารถน&าส่�วนของซอฟท์�แวร�น�$นกลั�บ่มาเร�ยกใช้�ได้�อ�กส่�วนของซอฟท์�แวร�น�$นกลั�บ่มาเร�ยกใช้�ได้�อ�ก(reuseable) (reuseable)
ลัด้คืวามซ&$าซ�อนแลัะเวลัาลังได้� ลัด้คืวามซ&$าซ�อนแลัะเวลัาลังได้�
การท์&างานของ การท์&างานของ class class จะถ,กก&าหนด้โด้ยส่�วนอ!นเตอร�เฟส่จะถ,กก&าหนด้โด้ยส่�วนอ!นเตอร�เฟส่ ของ ของ method method ส่�วนการท์&างานของส่�วนท์�%เป็�นโคื�ต จะไม�ถ,ก ส่�วนการท์&างานของส่�วนท์�%เป็�นโคื�ต จะไม�ถ,ก
คื&าน.งถ.งในการออกแบ่บ่คื&าน.งถ.งในการออกแบ่บ่. . ภาษา ภาษา OOP OOP ส่นใจเฉพัาะข�อม,ลัท์�%ส่นใจเฉพัาะข�อม,ลัท์�% จะถ,กป็ระมวลัผลัมากกว�าฟ3งก�ช้�%นท์�%ท์&าการป็ระมวลัข�อม,ลัน�$น จะถ,กป็ระมวลัผลัมากกว�าฟ3งก�ช้�%นท์�%ท์&าการป็ระมวลัข�อม,ลัน�$น
หลั�หลั�กการท์�%กการท์�% ส่&าคื�ญบ่างป็ระการของ ส่&าคื�ญบ่างป็ระการของ OOPOOP
• • Class and Subclass Class and Subclass
• • Encapsulate Encapsulate
• • Inheritance Inheritance
• • Polymorphism Polymorphism
• • Abstract Data Type Abstract Data Type
เหต5ผลัท์�% เหต5ผลัท์�% OOP OOP ม�บ่ท์บ่าท์มากข.$นม�บ่ท์บ่าท์มากข.$น
• • ง�าย แลัะรวด้เร6ว ท์&าให�ลัด้เวลัาในการพั�ฒนาลังไป็ได้� ง�าย แลัะรวด้เร6ว ท์&าให�ลัด้เวลัาในการพั�ฒนาลังไป็ได้�• • เพั!%มป็ร!มาณงานท์�%ได้� แลัะม�คืวามน�าเช้�%อถ�อมากกว�า เพั!%มป็ร!มาณงานท์�%ได้� แลัะม�คืวามน�าเช้�%อถ�อมากกว�า• • ส่ามารถน&า ส่ามารถน&า code code กลั�บ่มาใช้�ได้�อ�ก กลั�บ่มาใช้�ได้�อ�ก (( เร�ยกใช้� เร�ยกใช้� class) class)
• • ท์&าต�นแบ่บ่ ท์&าต�นแบ่บ่ (Prototyping) (Prototyping) ได้�รวด้เร6วกว�า ได้�รวด้เร6วกว�า• • ลัด้ต�นท์5นในการส่ร�าง แลัะบ่&าร5งร�กษาซ ลัด้ต�นท์5นในการส่ร�าง แลัะบ่&าร5งร�กษาซอฟต�อฟต� แวร� แวร�• • การเป็ลั�%ยนแป็ลังแก�ไข ไม�ท์&าให�เก!ด้ผลักระท์บ่ไป็ย�ง การเป็ลั�%ยนแป็ลังแก�ไข ไม�ท์&าให�เก!ด้ผลักระท์บ่ไป็ย�ง
ภายนอก ภายนอก class class
มาร3 !จ�กคัลัาส่แลัะวิ�ตถุ�ก�นมาร3 !จ�กคัลัาส่แลัะวิ�ตถุ�ก�น►คัลัาส่เป,นเหม+อนส่�วินขีองแม�พ�มพ� เพ+/อบอกลั�กษณะคัลัาส่เป,นเหม+อนส่�วินขีองแม�พ�มพ� เพ+/อบอกลั�กษณะ
วิ�าวิ�ตถุ�ในคัลัาส่น�มลั�กษณะแลัะพฤต�กรรมเป,นวิ�าวิ�ตถุ�ในคัลัาส่น�มลั�กษณะแลัะพฤต�กรรมเป,นอย�างไรอย�างไร
►วิ�ตถุ�เป,นส่�/งที/ถุ3กส่ร!างขี.�นมาจากคัลัาส่เพ+/อใช!งาน วิ�ตถุ�เป,นส่�/งที/ถุ3กส่ร!างขี.�นมาจากคัลัาส่เพ+/อใช!งาน ก$าหนดคั�าต�างๆ ส่�งขี!อคัวิามส่�/งให!วิ�ตถุ�ที$างานก$าหนดคั�าต�างๆ ส่�งขี!อคัวิามส่�/งให!วิ�ตถุ�ที$างาน
คืลัาส่ส่�%เหลั�%ยมผ�นผ�าคั�ณส่มบ�ต�:กวิ!าง, ยาวิเมธทีอด:คั$านวิณพ+�นที/
ว�ตถ5A:ส่�%เหลั�%ยมผ�นผ�า กวิ!าง 2 ยาวิ 3
เมธทีอด:คั$านวิณพ+�นที/
Class Class คื�ออะไรคื�ออะไร
คื�อกลั5�ม คื�อกลั5�ม (category)(category) ขีอง ขีอง objects objects ที/มที/ม คั�ณส่มบ�ต�แลัะพฤต�กรรมที/เหม+อนก�น โดย คั�ณส่มบ�ต�แลัะพฤต�กรรมที/เหม+อนก�น โดย class class จะจะ
ต!องประกอบไปด!วิย ต!องประกอบไปด!วิย data, behavior data, behavior แลัะ แลัะinterface interface
คื�อต�นแบ่บ่ คื�อต�นแบ่บ่ (prototype)(prototype) หร+อพ�มพ�เขียวิ ที/ หร+อพ�มพ�เขียวิ ที/ ก$าหนดต�วิแปรแลัะวิ�ธการ เพ+/อน$าไปใช!ได!ในที�ก ก$าหนดต�วิแปรแลัะวิ�ธการ เพ+/อน$าไปใช!ได!ในที�ก
object object ขีอง ขีอง class class
เปรยบเทียบคัลัาส่ก�บวิ�ตถุ�เปรยบเทียบคัลัาส่ก�บวิ�ตถุ�
ClassClass
ยานพัาหนะยานพัาหนะ
ท์างบ่กท์างบ่ก ท์างอากาศท์างอากาศ
ท์างบ่กท์างบ่ก
รถส่องลั�อรถส่องลั�อ รถส่ามลั�อรถส่ามลั�อ รถส่�%ลั�อรถส่�%ลั�อ รถส่!บ่ลั�อรถส่!บ่ลั�อ
ObjectsObjects
คั+อ ส่�/งใดๆ ก9ตาม ซึ่./งมคั�ณลั�กษณะ คั+อ ส่�/งใดๆ ก9ตาม ซึ่./งมคั�ณลั�กษณะ (State) (State) บ�งบอกถุ.งคัวิามบ�งบอกถุ.งคัวิาม เป,นต�วิขีองม�นเองในขีณะน��น แลัะส่ามารถุแส่ดงพฤต�กรรม เป,นต�วิขีองม�นเองในขีณะน��น แลัะส่ามารถุแส่ดงพฤต�กรรม
(Behavior) (Behavior) ขีองต�วิเองออกมาได! เช�น รถุยนต�ส่น$�าเง�น ขีองต�วิเองออกมาได! เช�น รถุยนต�ส่น$�าเง�น : : มม คัวิามหมายคั+อ วิ�ตถุ�ประเภัทีรถุยนต� มคั�ณลั�กษณะขีองส่เป,น คัวิามหมายคั+อ วิ�ตถุ�ประเภัทีรถุยนต� มคั�ณลั�กษณะขีองส่เป,น
ส่น$�าเง�น แลัะมพฤต�กรรมที/แส่ดงถุ.งการเคัลั+/อนที/ แลัะหย�ดได! ส่น$�าเง�น แลัะมพฤต�กรรมที/แส่ดงถุ.งการเคัลั+/อนที/ แลัะหย�ดได! หร+อกลั�าวิได!วิ�า หร+อกลั�าวิได!วิ�า object object ก9คั+อขี!อม3ลัขีอง ก9คั+อขี!อม3ลัขีอง class (class (เป,นเป,นentities entities
ขีอง ขีอง class) class) ซึ่./งที�กๆ อย�างจะจ�ดเป,น ซึ่./งที�กๆ อย�างจะจ�ดเป,น objects objects โดยต!องประกอบโดยต!องประกอบ ไปด!วิย ไปด!วิย
► ช+/อ ช+/อ (Identity) (Identity) ► ส่ถุานะ ส่ถุานะ (State) (State) คั�ณส่มบ�ต� หร+อคั�าขีองขี!อม3ลั ซึ่./งแทีนด!วิย คั�ณส่มบ�ต� หร+อคั�าขีองขี!อม3ลั ซึ่./งแทีนด!วิย
value value ► พฤต�กรรม พฤต�กรรม (Behavior) (Behavior) ที/ระบ�วิ�าส่ามารถุที$าอะไรได!บ!าง ซึ่./ง ที/ระบ�วิ�าส่ามารถุที$าอะไรได!บ!าง ซึ่./ง
แทีนด!วิย แทีนด!วิย method method
ต�วิอย�างชน�ดขีองวิ�ตถุ�แลัะวิ�ตถุ�ต�วิอย�างชน�ดขีองวิ�ตถุ�แลัะวิ�ตถุ�
MobilePhone
String bandnameButton buttonScreen screenAntenna antennaSimm simNet
call()receive()hangup()sendSMS()
:siemens
:ericsson
:nokia
MethodMethod คื�อ คื�อ function function ท์�%บ่�งบ่อกพัฤต!กรรมของ ท์�%บ่�งบ่อกพัฤต!กรรมของ object object ว�าว�า
ท์&าอะไรได้�บ่�าง ก&าหนด้ไว�ใน ท์&าอะไรได้�บ่�าง ก&าหนด้ไว�ใน class class โด้ยต�องป็ระกอบ่โด้ยต�องป็ระกอบ่ ด้�วย ช้�%อของ ด้�วย ช้�%อของ method method เร�ยกว�า เร�ยกว�า IdentifierIdentifier ตามตาม
ด้�วยเคืร�%องหมายวงเลั6บ่ ด้�วยเคืร�%องหมายวงเลั6บ่ () () โด้ยในวงเลั6บ่อาจม� โด้ยในวงเลั6บ่อาจม�parameter list parameter list อย,�หร�อไม�ก6ได้� เช้�น อย,�หร�อไม�ก6ได้� เช้�น
getBalance() getBalance() raiseSalary( float Salary, float Percent ) raiseSalary( float Salary, float Percent )
Constructor Method Constructor Method
คื�อเมธอด้ท์�%ใช้�ส่&าหร�บ่ส่ร�าง คื�อเมธอด้ท์�%ใช้�ส่&าหร�บ่ส่ร�าง instance object instance object ของคืลัาส่น�$นๆ โด้ยท์�% ช้�%อเมธอด้น�$ต�องเหม�อนก�บ่ช้�%อ ของคืลัาส่น�$นๆ โด้ยท์�% ช้�%อเมธอด้น�$ต�องเหม�อนก�บ่ช้�%อ
คืลัาส่ แลัะใช้�ส่&าหร�บ่ คืลัาส่ แลัะใช้�ส่&าหร�บ่ initialize initialize ข�อม,ลัให�ก�บ่ ข�อม,ลัให�ก�บ่instance variable instance variable โด้ยจะไม�ม�การโด้ยจะไม�ม�การถ�ายท์อด้ถ�ายท์อด้ให�ให�
ก�บ่ ก�บ่ subclass subclass แลัะไม�ม�การ แลัะไม�ม�การ return return คื�า คื�า
Message Message
คั+อคั$าส่�/งหร+อขี!อคัวิามที/จะให!ขี!อม3ลัหร+อต�วิแปรใดที$างาน คั+อคั$าส่�/งหร+อขี!อคัวิามที/จะให!ขี!อม3ลัหร+อต�วิแปรใดที$างาน ก9คั+อ ก9คั+อ parameter parameter ในภัาษาอ+/นที/ไม�ใช� ในภัาษาอ+/นที/ไม�ใช� OOP OOP คั+อใช!เพ+/อคั+อใช!เพ+/อ
น$าส่�งคั�าขี!อม3ลัระหวิ�าง น$าส่�งคั�าขี!อม3ลัระหวิ�าง object object โดยใน โดยใน message message น��นน��น ต!องประกอบด!วิย ต!องประกอบด!วิย
Destination Destination ก9คั+อช+/อขีอง ก9คั+อช+/อขีอง object object Method Method Parameters Parameters
Accessibility Accessibility
public :public : เขี!าถุ.งได!ในที�กที/ เขี!าถุ.งได!ในที�กที/ private :private : เขี!าถุ.งได!เฉพาะภัายใน เขี!าถุ.งได!เฉพาะภัายใน class class เที�าน��น ไม� เที�าน��น ไม�
รวิม รวิม sub class sub class protected :protected : เขี!าถุ.งได!เฉพาะภัายใน เขี!าถุ.งได!เฉพาะภัายใน class class แลัะ แลัะ sub sub
class class ที/ส่+บทีอดก�นมา ที/ส่+บทีอดก�นมา (Inherit) (Inherit) defaultdefault : : ถุ!าไม�ระบ� จะเขี!าถุ.งขี!อม3ลัภัายใน ถุ!าไม�ระบ� จะเขี!าถุ.งขี!อม3ลัภัายใน class class แลัะแลัะ
อย3�เพ9กเกจเดยวิก�น อย3�เพ9กเกจเดยวิก�น
Public
Class
Sub Class
EncapsulateEncapsulate
คื�อ การป็<ด้บ่�ง หร�อจ&าก�ด้การเข�าถ.งข�อม,ลับ่างอย�าง คื�อ การป็<ด้บ่�ง หร�อจ&าก�ด้การเข�าถ.งข�อม,ลับ่างอย�าง(Information hiding) (Information hiding) ท์�%ไม�จ&าเป็�นต�องให�ส่�วนอ�%นร�บ่ร,� ยก ท์�%ไม�จ&าเป็�นต�องให�ส่�วนอ�%นร�บ่ร,� ยก
ต�วอย�างเช้�น เราจะไม�ส่นใจหร�อมองเ ต�วอย�างเช้�น เราจะไม�ส่นใจหร�อมองเห6ห6 นได้�ว�า เคืร�%องเลั�น นได้�ว�า เคืร�%องเลั�น CD CD จะจะ แป็ลังส่�ญญาณด้!จ!ตอลั ออกมาเป็�นเพัลังได้�อย�างไร เราใช้�แลัะต!ด้ต�อ แป็ลังส่�ญญาณด้!จ!ตอลั ออกมาเป็�นเพัลังได้�อย�างไร เราใช้�แลัะต!ด้ต�อ
ก�บ่เคืร�%องแคื�คืวบ่คื5มการท์&างานผ�านแผงคืวบ่คื5ม เช้�น เป็<ด้ ก�บ่เคืร�%องแคื�คืวบ่คื5มการท์&างานผ�านแผงคืวบ่คื5ม เช้�น เป็<ด้-- ป็<ด้ เลั�น ป็<ด้ เลั�น เร�งเส่�ยง เป็ลั�%ยนแท์ร=กไป็ข�างหน�า ย�อนกลั�บ่ เป็�นต�น โด้ยเราต�อง เร�งเส่�ยง เป็ลั�%ยนแท์ร=กไป็ข�างหน�า ย�อนกลั�บ่ เป็�นต�น โด้ยเราต�อง
ออกแบ่บ่คืวบ่คื5มกฏเกณฑ์�ต�างๆ ของซอฟท์�แวร�ให�ส่อด้คืลั�องก�บ่ ออกแบ่บ่คืวบ่คื5มกฏเกณฑ์�ต�างๆ ของซอฟท์�แวร�ให�ส่อด้คืลั�องก�บ่ คืวามเป็�นจร!ง คืวามเป็�นจร!ง
Encapsulate Encapsulate เป็�นคื5ณส่มบ่�ต!ของ เป็�นคื5ณส่มบ่�ต!ของ object object ซ.%งม�ลั�กษณะด้�งน�$ ซ.%งม�ลั�กษณะด้�งน�$ ก&าหนด้ขอบ่เขตท์�%ช้�ด้เจนให�ก�บ่ ก&าหนด้ขอบ่เขตท์�%ช้�ด้เจนให�ก�บ่ object object ก&าหนด้อ!นเตอร�เฟส่ว�าจะต!ด้ต�อก�บ่ ก&าหนด้อ!นเตอร�เฟส่ว�าจะต!ด้ต�อก�บ่ object object อ�%นๆ อย�างไร อ�%นๆ อย�างไร ส่�วนอ!มพัลั!เมนต�ไม�ส่ามารถเข�าถ.งได้�ภายนอกขอบ่เขตของ ส่�วนอ!มพัลั!เมนต�ไม�ส่ามารถเข�าถ.งได้�ภายนอกขอบ่เขตของ class class
EncapsulateEncapsulate
InheritanceInheritance
คั+อการถุ�อยทีอดขี!อม3ลั คั+อการถุ�อยทีอดขี!อม3ลั (( ซึ่./งก9คั+อ ซึ่./งก9คั+อ state state แลัะ แลัะbehavior) behavior) จาก จาก class class ลั$าด�บที/ส่3งกวิ�า ลั$าด�บที/ส่3งกวิ�า (super (super class class หร+อ หร+อ parent class) parent class) ไปย�งลั$าด�บที/ต$/ากวิ�า ไปย�งลั$าด�บที/ต$/ากวิ�า(subclass) (subclass) โดยที/ โดยที/ subclass subclass น��นส่ามารถุน��นส่ามารถุ
เปลั/ยนแปลัง หร+อแทีนที/ขี!อม3ลั เปลั/ยนแปลัง หร+อแทีนที/ขี!อม3ลั (override) (override) ที/ได!ร�บการที/ได!ร�บการถุ�ายทีอดมาน��นได!ถุ�ายทีอดมาน��นได!
Class AnimalClass Animal
Animal
-name-leg
-Eat()-Sleep()
Inheritance : Inheritance : ส่�บ่ท์อด้คื5ณส่มบ่�ต!ส่�บ่ท์อด้คื5ณส่มบ่�ต!
►ต�วิอย�าง ต�วิอย�าง Tiger Class Tiger Class ส่ามารถุส่+บทีอดคั�ณส่มบ�ต�ส่ามารถุส่+บทีอดคั�ณส่มบ�ต�จาก จาก Animal ClassAnimal Class
Animal Class
sub Class
super Class
PolymorphisimPolymorphisim
การที$าให! การที$าให! message message อ�นหน./งส่ามารถุส่�งให! อ�นหน./งส่ามารถุส่�งให! object object แต�ลัะต�วิใน แต�ลัะต�วิใน class class แลัะ แลัะ subclass subclass ตอบส่นองต�อ ตอบส่นองต�อ
message message อ�นเดยวิก�น ในลั�กษณะที/เหมาะส่บก�บ อ�นเดยวิก�น ในลั�กษณะที/เหมาะส่บก�บclass class ขีองต�วิเอง ขีองต�วิเอง
ยกต�วิอย�างเช�น ยกต�วิอย�างเช�น method print method print น�ส่ามารถุส่�งให!ที�ก น�ส่ามารถุส่�งให!ที�กobject object ขีอง ขีอง class class แลัะ แลัะ subclass subclass ที/ที$าให! ที/ที$าให! object object
น��นร3 !จ�ก น��นร3 !จ�ก method print method print แลัะแต�ลัะ แลัะแต�ลัะ object object ที/ต�างก�นที/ต�างก�น จะตอบส่นองต�อ จะตอบส่นองต�อ message message น�ต�างก�นออกไป ตาม น�ต�างก�นออกไป ตาม
คัวิามส่ามารถุในการใช! คัวิามส่ามารถุในการใช!
การถ�ายท์อด้ให�เก!ด้ลั�กษณะของพั�องร,ป็การถ�ายท์อด้ให�เก!ด้ลั�กษณะของพั�องร,ป็Shape
+draw()+erase()
Circle
+draw()+erase()
Line
+draw()+erase()
Rectangle
+draw()+erase()
PolymorphisimPolymorphisim
โคืรงส่ร�างของ โคืรงส่ร�างของ Class Class
โปรแกรมที/ส่ร!างจากภัาษาจาวิา ต!องมออปเจ9คัหร+อคัลัาส่อย�างน!อยหน./งต�วิ โปรแกรมที/ส่ร!างจากภัาษาจาวิา ต!องมออปเจ9คัหร+อคัลัาส่อย�างน!อยหน./งต�วิ โดยมร3ปแบบโคัรงส่ร!างด�งน� โดยมร3ปแบบโคัรงส่ร!างด�งน�
classclass Class_Name Class_Name
{ Data_Member { Data_Member
Method_Member } Method_Member }
//// ร,ป็แบ่บ่ของ ร,ป็แบ่บ่ของ Data MemberData Member
[ Accessibility ] [ final ] [ static ] Data_Type Data_Name [ Accessibility ] [ final ] [ static ] Data_Type Data_Name
เช�น เช�น private static Stringprivate static String stu_name stu_name ;;
ร,ป็แบ่บ่ของ ร,ป็แบ่บ่ของ Data MemberData Member
[ Accessibility ] [ final ] [ static ] Data_Type Data_Name[ Accessibility ] [ final ] [ static ] Data_Type Data_Name
เช�น เช�น private static Stringprivate static String stud_name stud_name ;;
Data Data คั+อส่�วินประกอบส่�วินหน./งขีองคัลัาส่ ถุ3กก$าหนดเพ+/อใช! คั+อส่�วินประกอบส่�วินหน./งขีองคัลัาส่ ถุ3กก$าหนดเพ+/อใช! ส่$าหร�บเก9บขี!อม3ลั เช�นคัลัาส่ ส่$าหร�บเก9บขี!อม3ลั เช�นคัลัาส่ Pen Pen มดาต!า มดาต!า Color Color ไวิ!เก9บขี!อม3ลัไวิ!เก9บขี!อม3ลั
ส่ส่class Penclass Pen
{{String Color = “red”; // Data MemberString Color = “red”; // Data Member
}}
finalfinal
► final : final : เป็�นคื�ย�เว!ร�ด้ต�วหน.%งซ.%งใช้�บ่อกว�าด้าต�าต�วน�$นเป็�นคื�ย�เว!ร�ด้ต�วหน.%งซ.%งใช้�บ่อกว�าด้าต�าต�วน�$น ใช้�ส่&าหร�บ่เก6บ่ข�อม,ลั โด้ยท์�%ข�อม,ลัจะไม�ส่ามารถท์&าการ ใช้�ส่&าหร�บ่เก6บ่ข�อม,ลั โด้ยท์�%ข�อม,ลัจะไม�ส่ามารถท์&าการ
เป็ลั�%ยนแป็ลังได้�ไม�ว�าในกรณ�ใด้ๆ ป็กต!จะใส่�คื�ย�เว!ร�ด้น�$ เป็ลั�%ยนแป็ลังได้�ไม�ว�าในกรณ�ใด้ๆ ป็กต!จะใส่�คื�ย�เว!ร�ด้น�$ ไว�เม�%อต�องการให�ด้าต�าเก6บ่ข�อม,ลัท์�%เป็�นข�อม,ลัท์�%คืงท์�% ไว�เม�%อต�องการให�ด้าต�าเก6บ่ข�อม,ลัท์�%เป็�นข�อม,ลัท์�%คืงท์�%
(Constant) (Constant) ตลัอด้การท์&างานของโป็รแกรมตลัอด้การท์&างานของโป็รแกรม► class CircleArea {class CircleArea {
public static void main(String args[])public static void main(String args[])
{{
final double pi = 3.14;final double pi = 3.14;final double r = 10;final double r = 10;
System.out.println(“Circle Area = " + pi*r*r); System.out.println(“Circle Area = " + pi*r*r);
}}
}}
staticstatic
► staticstatic : : เป,นคัย�เวิ�ร�ด ส่$าหร�บใช!บอกถุ.งคั�ณลั�กษณะพ�เศ์ษในการใช!งาน เป,นคัย�เวิ�ร�ด ส่$าหร�บใช!บอกถุ.งคั�ณลั�กษณะพ�เศ์ษในการใช!งาน เม+/อมการก$าหนดน$าหน!าดาต!าใดๆ แลั!วิ ดาต!าน��นจะมคั�ณลั�กษณะด�งน� เม+/อมการก$าหนดน$าหน!าดาต!าใดๆ แลั!วิ ดาต!าน��นจะมคั�ณลั�กษณะด�งน�
• • ด้าต�าด้าต�า จะถุ3กโหลัดลังในหน�วิยคัวิามจ$าแลัะพร!อมที/จะถุ3กใช!งานในที�นที จะถุ3กโหลัดลังในหน�วิยคัวิามจ$าแลัะพร!อมที/จะถุ3กใช!งานในที�นที เม+/อมการอ!างถุ.ง ตามขี!อก$าหนดขีองระด�บการเขี!าถุ.ง เม+/อมการอ!างถุ.ง ตามขี!อก$าหนดขีองระด�บการเขี!าถุ.ง (Accessibility)(Accessibility)
• • ด้าต�าด้าต�า จะอย3�ในหน�วิยคัวิามจ$าเพยงต�วิเดยวิ ไม�มการ จะอย3�ในหน�วิยคัวิามจ$าเพยงต�วิเดยวิ ไม�มการ copy static copy static method method ไปก�บ ไปก�บ instance object instance object คั+อจะผู้3กต�ดอยคั+อจะผู้3กต�ดอย3�3� ก�บคัลัาส่เที�าน��น ก�บคัลัาส่เที�าน��นด�งน��นจ.งส่ามารถุใช!ดาต!าเป,นที/เก9บขี!อม3ลัรวิมขีองกลั��มคัลัาส่เดยวิก�นได!ด�งน��นจ.งส่ามารถุใช!ดาต!าเป,นที/เก9บขี!อม3ลัรวิมขีองกลั��มคัลัาส่เดยวิก�นได!• ไม�ส่ามารถุเรยกใช!• ไม�ส่ามารถุเรยกใช! instance variables instance variables หร+อ หร+อ instance instance method method ได!ได!• ไม�ส่ามารถุใช!คัย�เวิ�ร�ด• ไม�ส่ามารถุใช!คัย�เวิ�ร�ด thisthis ได!ได!
โคืรงส่ร�างของ โคืรงส่ร�างของ ObjectObject
►เม+/อกลั�าวิถุ.งคั$าวิ�าออปเจ9คัที�กคันจะเขี!าใจวิ�าเป,นช+/อเม+/อกลั�าวิถุ.งคั$าวิ�าออปเจ9คัที�กคันจะเขี!าใจวิ�าเป,นช+/อเรยกที/ใช!เรยกส่�วินที/เรยกที/ใช!เรยกส่�วินที/ส่ร�างส่ร�างจากโคัรงร�างขีองคัลัาส่ จากโคัรงร�างขีองคัลัาส่ ซึ่./งออปเจ9คัถุ3กส่ร!างโดยการใช!คั$าส่�/งในภัาษาจาวิาซึ่./งออปเจ9คัถุ3กส่ร!างโดยการใช!คั$าส่�/งในภัาษาจาวิาส่ร!างขี.�น จากขี��นตอนส่องขี��นตอน คั+อส่ร!างขี.�น จากขี��นตอนส่องขี��นตอน คั+อ
1. 1. ข�$นตอนป็ระกาศช้�%อต�วแป็รอ�างถ.งข�$นตอนป็ระกาศช้�%อต�วแป็รอ�างถ.ง(Reference (Reference Variable)Variable)
2. 2. ข�$นตอนการส่ร�างอ!นส่แตนซ� ข�$นตอนการส่ร�างอ!นส่แตนซ� (Instance of Class)(Instance of Class)
1. 1. ข�$นตอนป็ระกาศช้�%อต�วแป็รอ�างถ.ง ข�$นตอนป็ระกาศช้�%อต�วแป็รอ�างถ.ง (Reference (Reference Variable) Variable)
คั+อขี��นตอนที/ใช!ก$าหนดช+/อต�วิแปรเพ+/อใช!ในการอ!างถุ.งไปย�งคั+อขี��นตอนที/ใช!ก$าหนดช+/อต�วิแปรเพ+/อใช!ในการอ!างถุ.งไปย�ง พ+�นที/ในหน�วิยคัวิามจ$า โดยพ+�นที/ส่�วินน�เป,นส่�วินที/จ�ดเก9บอ�นส่ พ+�นที/ในหน�วิยคัวิามจ$า โดยพ+�นที/ส่�วินน�เป,นส่�วินที/จ�ดเก9บอ�นส่
แตนซึ่�ที/ส่ร!างขี.�นมาในภัายหลั�ง โดยมร3ปแบบการก$าหนดด�งน� แตนซึ่�ที/ส่ร!างขี.�นมาในภัายหลั�ง โดยมร3ปแบบการก$าหนดด�งน�
Class_Name Reference_Name Class_Name Reference_Name
►Class_Name : Class_Name : คั+อช+/อขีองคัลัาส่ซึ่./งอ!างถุ.งด!วิย คั+อช+/อขีองคัลัาส่ซึ่./งอ!างถุ.งด!วิยReference_Name Reference_Name ได!ได!
►Reference_Name : Reference_Name : คั+อช+/อต�วิแปรที/ถุ3กก$าหนดขี.�นโดยผู้3!เขียนคั+อช+/อต�วิแปรที/ถุ3กก$าหนดขี.�นโดยผู้3!เขียน โปรแกรม โปรแกรม
2. 2. ข�$นตอนการส่ร�างอ!นส่แตนซ� ข�$นตอนการส่ร�างอ!นส่แตนซ� (Instance of Class) (Instance of Class)
อ�นส่แตนซึ่� คั+อพ+�นที/ในหน�วิยคัวิามจ$าซึ่./งส่ร!างตามแบบอ�นส่แตนซึ่� คั+อพ+�นที/ในหน�วิยคัวิามจ$าซึ่./งส่ร!างตามแบบโคัรงร�างขีองคัลัาส่ใดๆ ด�งน��นอ�นส่แตนซึ่�คั+อพ+�นที/ที/โคัรงร�างขีองคัลัาส่ใดๆ ด�งน��นอ�นส่แตนซึ่�คั+อพ+�นที/ที/ส่ามารถุเก9บขี!อม3ลัแลัะประมวิลัผู้ลัตาม ส่�วินที/ส่ามารถุส่ามารถุเก9บขี!อม3ลัแลัะประมวิลัผู้ลัตาม ส่�วินที/ส่ามารถุประมวิลัผู้ลัได!น�/นเอง มร3ปแบบการส่ร!างด�งน�ประมวิลัผู้ลัได!น�/นเอง มร3ปแบบการส่ร!างด�งน�
newnew Class_Constructor( Class_Constructor(Parameter_ListParameter_List ) )
- - newnew : : คั+อคัย�เวิ�ร�ดขีองภัาษาจาวิา เพ+/อเป,นคั$าส่�/งให!มการ คั+อคัย�เวิ�ร�ดขีองภัาษาจาวิา เพ+/อเป,นคั$าส่�/งให!มการ จ�ดส่รรพ+�นที/หน�วิยคัวิามจ$า ตามโคัรงส่ร!างขีองคัลัาส่จากที/ จ�ดส่รรพ+�นที/หน�วิยคัวิามจ$า ตามโคัรงส่ร!างขีองคัลัาส่จากที/
ก$าหนดไวิ!แลั!วิในขี��นตอนที/ผู้�านมา ก$าหนดไวิ!แลั!วิในขี��นตอนที/ผู้�านมา - Class_Constructor : - Class_Constructor : คั+อเมธรอดขีองคัลัาส่ที/เป,นคัอนคั+อเมธรอดขีองคัลัาส่ที/เป,นคัอน
ส่ตร�กเตอร� ส่ตร�กเตอร� ((เมธรอดที/เป,นคัอนส่ตร�กเตอร�มช+/อเดยวิก�บช+/อเมธรอดที/เป,นคัอนส่ตร�กเตอร�มช+/อเดยวิก�บช+/อคัลัาส่เส่มอคัลัาส่เส่มอ) ) โดยคัลัาส่หน./งคัลัาส่ส่ามารถุมเมธรอดคัอนส่ตร�กโดยคัลัาส่หน./งคัลัาส่ส่ามารถุมเมธรอดคัอนส่ตร�ก
เตอร�น�ได!หลัายเมธรอด เตอร�น�ได!หลัายเมธรอด ((ช+/อเดยวิก�นแต�พาราม�เตอร�ต�างก�นช+/อเดยวิก�นแต�พาราม�เตอร�ต�างก�น) ) --Parameter_ListParameter_List : : คั+อพาราม�เตอร� คั+อพาราม�เตอร� ((ต�วิแปรหร+อคั�าคังที/ต�วิแปรหร+อคั�าคังที/
ใดๆใดๆ) ) ที/ผู้�านขี!อม3ลัเขี!าไปส่3�เมธรอดคัอนส่ต�กเตอร� ซึ่./งจ$านวิน ที/ผู้�านขี!อม3ลัเขี!าไปส่3�เมธรอดคัอนส่ต�กเตอร� ซึ่./งจ$านวิน หร+อชน�ดขี!อม3ลัที/เป,นพาราม�เตอร� ขี.�นอย3�วิ�าผู้3!เขียนโปรแกรม หร+อชน�ดขี!อม3ลัที/เป,นพาราม�เตอร� ขี.�นอย3�วิ�าผู้3!เขียนโปรแกรม เรยกใช!คัอนส่ตร�กเตอร�แบบไหน เรยกใช!คัอนส่ตร�กเตอร�แบบไหน
ต�วแป็รอ�างถ.งแลัะอ!นส่แตนซ�ต�วแป็รอ�างถ.งแลัะอ!นส่แตนซ� (Reference variable and (Reference variable and
Instance)Instance) ในภัาษาจาวิาต�วิแปรอ!างถุ.งแลัะอ�นแตนซึ่�มคัวิามส่�มพ�นธ�ในภัาษาจาวิาต�วิแปรอ!างถุ.งแลัะอ�นแตนซึ่�มคัวิามส่�มพ�นธ�
ซึ่./งก�นแลัะก�น กลั�าวิคั+อต�วิแปรอ!างถุ.งถุ3กก$าหนดขี.�น เพ+/อ ซึ่./งก�นแลัะก�น กลั�าวิคั+อต�วิแปรอ!างถุ.งถุ3กก$าหนดขี.�น เพ+/อประโยชน�ในการอ!างไปย�งต$าแหน�งขีองอ�นส่แตนซึ่�ที/อย3�ในประโยชน�ในการอ!างไปย�งต$าแหน�งขีองอ�นส่แตนซึ่�ที/อย3�ใน
หน�วิยคัวิามจ$า โดยในหน�วิยคัวิามจ$าบนเคัร+/อง หน�วิยคัวิามจ$า โดยในหน�วิยคัวิามจ$าบนเคัร+/อง คัอมพ�วิเตอร� บางคัร��งส่ามารถุส่ร!างอ�นส่แตนซึ่�ได!หลัายๆ คัอมพ�วิเตอร� บางคัร��งส่ามารถุส่ร!างอ�นส่แตนซึ่�ได!หลัายๆ
อ�นส่แตนซึ่�ขีองการที$างานแต�ลัะคัร��ง จ.งจ$าเป,นต!องใช!ช+/อ อ�นส่แตนซึ่�ขีองการที$างานแต�ลัะคัร��ง จ.งจ$าเป,นต!องใช!ช+/อต�วิแปรอ!างถุ.งเพ+/อให!ส่ามารถุเขี!าใช!งานดาต!าแลัะเมธรอดต�วิแปรอ!างถุ.งเพ+/อให!ส่ามารถุเขี!าใช!งานดาต!าแลัะเมธรอด
ในอ�นส่แตนซึ่�ได! ต�วิอย�างเช�น ในอ�นส่แตนซึ่�ได! ต�วิอย�างเช�น
อ�นส่แตนซึ่� อ�นส่แตนซึ่� (instance)(instance)
►ถุ!าพ�จารณาถุ.งลั3กโป;งส่วิรรคั� เปรยบก�บคันขีายถุ!าพ�จารณาถุ.งลั3กโป;งส่วิรรคั� เปรยบก�บคันขีายลั3กโป;งส่วิรรคั�ตามงานที�/วิไป ถุ!าให!ลั3กโป;งแต�ลัะลั3กลั3กโป;งส่วิรรคั�ตามงานที�/วิไป ถุ!าให!ลั3กโป;งแต�ลัะลั3กเป,นอ�นส่แตนซึ่� แลัะให!เช+อกแต�ลัะเส่!นคั+อต�วิแปรอ!างเป,นอ�นส่แตนซึ่� แลัะให!เช+อกแต�ลัะเส่!นคั+อต�วิแปรอ!างถุ.ง ด�งน��นเม+/อต!องการลั3กโป;งลั3กใดๆ ก9ให!หย�บเส่!นถุ.ง ด�งน��นเม+/อต!องการลั3กโป;งลั3กใดๆ ก9ให!หย�บเส่!นเช+อกขี.�นมาที$าให!ร3 !วิ�าเป,นเช+อกที/ผู้3กอย3�ก�บลั3กโป;งลั3กเช+อกขี.�นมาที$าให!ร3 !วิ�าเป,นเช+อกที/ผู้3กอย3�ก�บลั3กโป;งลั3กใด ใด
instatnce
reference
ต�วอย�าง การส่ร�างต�วอย�าง การส่ร�างอ!นส่แตนซ�อ!นส่แตนซ�
► หร�อจากคื&าส่�%ง หร�อจากคื&าส่�%ง Integer x = new Integer x = new Integer(237)Integer(237)
หมายถ.งก&าหนด้ต�วแป็รอ�างถ.งช้�%อ หมายถ.งก&าหนด้ต�วแป็รอ�างถ.งช้�%อ x x โด้ยม�การโด้ยม�การ อ�างถ.งไป็ย�ง อ!นส่แตนซ�ท์�%เก!ด้จากคืลัาส่ท์�%ช้�%อ อ�างถ.งไป็ย�ง อ!นส่แตนซ�ท์�%เก!ด้จากคืลัาส่ท์�%ช้�%อ
Integer Integer แลัะให�อ!นส่แตนซ�น�$นเก6บ่คื�า แลัะให�อ!นส่แตนซ�น�$นเก6บ่คื�า 237 237 ไว� ไว� โด้ยท์�% โด้ยท์�% Integer Integer คื�อช้�%อคืลัาส่ท์�%ใช้�ส่&าหร�บ่จ�ด้การคื�อช้�%อคืลัาส่ท์�%ใช้�ส่&าหร�บ่จ�ด้การ
ข�อม,ลัต�วเลัขจ&านวนเต6ม ข�อม,ลัต�วเลัขจ&านวนเต6ม
ตต..ยย . . การส่ร�าง คืลัาส่ แลัะ ออบ่เจ6กต�การส่ร�าง คืลัาส่ แลัะ ออบ่เจ6กต�
class rectangle {class rectangle { int w=5;int w=5; int h=10;int h=10; public double CalArea() {public double CalArea() { return w*h;return w*h; }}}}
public class testRecpublic class testRec{{
public static void main(String args[]) {public static void main(String args[]) { rectangle r = rectangle r = newnew rectangle(); rectangle(); System.out.println(r.CalArea());System.out.println(r.CalArea()); }}
};};
5
10
ตต..ยย . . การส่ร�าง คืลัาส่ แลัะ ออบ่เจ6กต� การส่ร�าง คืลัาส่ แลัะ ออบ่เจ6กต� rectangle & rectangle & constructor constructor
class rectangle {class rectangle { int width;int width; int height;int height; rectangle(int w , int h) {rectangle(int w , int h) { width = w;width = w; height = h;height = h;}} public double CalArea() {public double CalArea() { return width*height;return width*height; }}}}
public class testRecpublic class testRec{{
public static void main(String args[]) {public static void main(String args[]) { rectangle r = rectangle r = newnew rectangle(5,10); rectangle(5,10); System.out.println(r.CalArea());System.out.println(r.CalArea()); }}
};};
5
10
InheritanceInheritance
class father {class father { void homeaddr(){void homeaddr(){
System.out.println(“lp”);System.out.println(“lp”);}}
}}
class child extends father {class child extends father { public static void main (String args[]) {public static void main (String args[]) { homeaddr();homeaddr(); }}}}
EncapsulationEncapsulationclass friend {class friend { private int val;private int val; public int getter(){ public int getter(){
return this.val;return this.val;}} public void setter(int a)public void setter(int a) { {
this.val=a;this.val=a;}}
}}
class child {class child { public static void main (String args[]) {public static void main (String args[]) { friend x = new friend(); friend x = new friend(); x.setter(5);x.setter(5); System.out.println(x.getter());System.out.println(x.getter()); }}}}
ตต..ยย . . เร�ยก เร�ยก object object จากอ�ก จากอ�ก class class หน.%งในแฟ@ม หน.%งในแฟ@ม x.java x.java เด้�ยวก�น แต�เด้�ยวก�น แต�ไม�ม� ไม�ม� constructor constructor
:: :: ม� ม� 2 2 กลั�อง ให�เร�ยก กลั�อง ให�เร�ยก object object จากอ�กกลั�องมาท์&างาน แส่ด้งว!ธ�เร�ยก จากอ�กกลั�องมาท์&างาน แส่ด้งว!ธ�เร�ยก 2 2 ว!ธ� ว!ธ�
class xxx { class xxx { void prtxx() { System.out.println("xx"); void prtxx() { System.out.println("xx");
} }}} class x { class x {
public static void main(String args[]) { public static void main(String args[]) { System.out.println("x"); System.out.println("x");
new xxx().prtxx(); new xxx().prtxx(); xxx a = new xxx(); // xxx a = new xxx(); // ไม�ม� ไม�ม� constructor constructor ใน ใน xxx a.prtxx(); xxx a.prtxx();
}} }} // Result of this program // x // xx // xx // Result of this program // x // xx // xx
ตต..ยย . . ConstructorConstructor
► Inheritance Inheritance แลัะ แลัะ constructor : constructor : runrun constructor constructor ขีอง ขีอง superclass superclass ตามด!วิย ตามด!วิย subclass subclass
:: :: ปกต� ปกต� default constructor default constructor คั+อ คั+อ object object ที/มช+/อเดยวิที/มช+/อเดยวิก�บ ก�บ class class แลัะไม�มส่�วินขียายใด ๆ แลัะไม�มส่�วินขียายใด ๆ
:: a.java :: a.java ม ม constructor constructor เป,นโปรแกรมที/ เป,นโปรแกรมที/ compile compileผู้�าน ผู้�าน run run ไม�ได! แต�มไวิ!ให!ถุ3กเรยกใช!ได! ไม�ได! แต�มไวิ!ให!ถุ3กเรยกใช!ได!
:: b.java :: b.java ม ม constructor constructor แลัะที$างานภัายใต! แลัะที$างานภัายใต! main mainเรยก เรยก constructor constructor ที��ง ที��ง 2 2 มาที$างาน มาที$างาน
:: constructor :: constructor คั+อ คั+อ default object default object จะส่�งคั�าให! จะส่�งคั�าให! constconst ructor ructor ก9ได! ก9ได!
:: :: การแปลั การแปลั b.java b.java จะเรยก จะเรยก a.java a.java มาแปลัด!วิยถุ!าไม�พบ มาแปลัด!วิยถุ!าไม�พบ a.class a.class แต�ถุ!าพบ แลัะ แต�ถุ!าพบ แลัะ a.java a.java ไม�เปลั/ยน ก9จะไม�แปลั ไม�เปลั/ยน ก9จะไม�แปลั a.jaa.ja
va va ใหม� ใหม�
// a.java // a.java public class a { public class a {
a() { a() { System.out.println("a constructor"); System.out.println("a constructor");
}} } }
// b.java // b.java public class b extends a { public class b extends a {
b(int i) { b(int i) { System.out.println("b constructor " + i); System.out.println("b constructor " + i);
} }
public static void main (String[] args) { public static void main (String[] args) { b xxx = new b(100); } b xxx = new b(100); }
} }
// Result of this program // Result of this program // a constructor // a constructor // b constructor 100 // b constructor 100
•Class Design in Java Class Design in Java OverloadingOverloadingProgramming with Multiple ClassesProgramming with Multiple ClassesInheritanceInheritanceInheritance and Access ControlInheritance and Access Control
Nattapong Songneam
OverloadingOverloading
Overloading MethodsOverloading Methods การป็ระกาศเมท์ธอด้ซ&$าซ�อน (Overloading Methods)
คื�อ ว!ธ�การเช้!งว�ตถ5 ท์�%ม�ไว�เพั�%อน&าช้�%อเมท์ธอด้(Methods Name) กลั�บ่มาใช้�ใหม�(reuse)
ภาษาโป็รแกรมจาวาส่น�บ่ส่น5น Overloading Methods โด้ย
อน5ญาตให�ป็ระกาศเมท์ธอด้ท์�%ม�ช้�%อเด้�ยวก�นซ&$า ก�นได้� แต�ม�เง�%อนไขด้�งน�$
ช้น!ด้ หร�อ จ&านวน หร�อ ลั&าด้�บ่ ของargument จะต�องต�างก�น return type ของเมท์ธอด้จะเหม�อน
ก�น หร�อแตกต�างก�นก6ได้�
public void deposit(double amount); public void deposit(int amount); public void deposit(double amount, double interest); public boolean deposit(double amount);
Overloading Overloading ConstructorsConstructors
การป็ระกาศคือนส่ตร�คืเตอร�ซ&$าซ�อน(Overloading Constructors)
คื�อ ว!ธ�การเช้!งว�ตถ5 ท์�%ม�ไว�เพั�%อเพั!%มคืวาม ย�ด้หย5�นให�ก�บ่การส่ร�างว�ตถ5 กรณ�ท์�%ไม�ส่ามารถ
ก&าหนด้ข�อม,ลัเร!%มต�นท์�%แน�นอน ในขณะส่ร�างว�ตถ5
ภาษาโป็รแกามจาวาส่น�บ่ส่น5น Overloading Constructors โด้ย
อน5ญาตให�ป็ระกาศคือนส่ตร�คืเตอร�ท์�%ม�ช้�%อเด้�ยวก�น ซ&$าก�นได้� โด้ยม�เง�%อนไขด้�งต�อไป็น�$
ช้น!ด้ หร�อ จ&านวน หร�อ ลั&าด้�บ่ ของargument จะต�องต�างก�น
public BankAccount (double initAmount) {/*…*/ }public BankAccount (String customerName) {/*…*/ }public BankAccount ( ) { /*…*/ }public BankAccount (double initAmount, String customerName) {/*…*/ }
public class OverLoadedBankAccount { private double balance;
public OverLoadedBankAccount( ) { balance = 0.0;}
public OverLoadedBankAccount(double intBalance) { balance = initBalance;}
public void deposit(int amount) { balance = balance + amount;} public void deposit(double amount) { balance = balance + amount;}
}
น$าช+/อเมทีธอดกลั�บมาใช!ไหม� โดยระบ� “ ”ชน�ด ขีอง Argument
เป,นประเภัทีขี!อม3ลัที/แตกต�างก�น
การก$าหนดคัอนส่ร�คัเตอร�ซึ่$�า ซึ่!อน มประโยชน�ในกรณที/
ไม�ส่ามารถุก$าหนดคั�าจ$านวินเง�นฝากเร�/มต!นเม+/อเป=ดบ�ญช
ที$าให!การใช!งานโปรแกรมมคัวิามย+ดหย��น
An Example of An Example of OverloadingOverloading
OverloadedBankAccount.java
public class OverLoadedAccountTester { public static void main(String[] args) { BankAccount myAccount =
new BankAccount2( ); BankAccount yourAccount =
new BankAccount2(100.0);
myAccount.deposit(5000.50); myAccount.withdrawn(2000); System.out.println("my balance:"
+myAccount.getBalance());
yourAccount.deposit(300.50); yourAccount.withdrawn(200); System.out.println("your balance:”
+ yourAccount.getBalance()); }}
OverLoadedTester.java
BankAccount BankAccount ExampleExample
public class BankAccount2 { private double balance;
public BankAccount2( ) { balance = 0.0; }
public BankAccount2(double intBalance) { balance = initBalance; } public void deposit(int amount) { balance = balance + amount; } public void deposit(double amount) { balance = balance + amount; }
:
Programming with Programming with Multiple ClassesMultiple Classes
account
1
BankAccount
- balance : double
+BankAccount(initBalance:double)+ getBalance : double+ deposit(amt : double)+ withdraw(amt : double)
Customer
- firstName : String- lastName : String- account : BankAccount
+Customer(f:String, l:String)+ getFirstName : String+ getLastName : String
+ setAccount( acct:BankAccount)+ getAccount( ) : BankAccount
Class Diagram with Multiple Class Diagram with Multiple ClassesClasses
Aggregation Association Name
Direction indicator
Multiplicity
Class Diagram of “Customer.java” and “BankAccount.java”
Customer ClassCustomer Class public class Customer { private String firstName;
private String lastName;private BankAccount account;public Customer(String f, String l) {
this.firstName = f;this.lastName = l;this.account = null;
}public String getName() {
return (this.firstName + " " + this.lastName);}public BankAccount getAccount() { return this.account; }public void setAccount(BankAccount acct) { this.account = acct; }
}
BankAccount ClassBankAccount Class public class BankAccount {
private double balance = 0.0;
public BankAccount(double amount) {balance = amount;
}public void deposit(double amount) {
balance = balance + amount;}public void withdrawn(double amount) {
balance = balance - amount;}public double getBalance() {
return balance;}
}
Programming with Multiple Programming with Multiple ClassesClasses
public class TestBanking { public static void main(String[] args) {
Customer cust = new Customer("Joe","Goodman"); cust.setAccount(new BankAccount(3000.0);); System.out.println("customer : " + cust.getName()
+ " : open account with balance = " + cust.getAccount().getBalance() + " baht.");
cust.getAccount().deposit(1250.25); System.out.println("customer : " + cust.getName()
+ " : deposit 1250.25 baht :" + " current balance = " + cust.getAccount().getBalance() + " baht.");
} }
TestBankingmainmain
Object Diagram of“TestBanking.java” “Customer.java” and “BankAccount.java”
Programming with Multiple Programming with Multiple ClassesClasses
CustomerCustomer
getAccountgetAccount
getNamegetName
firstName=“John”firstName=“John”
cust
lastName=“Goodman”lastName=“Goodman”
account=nullaccount=null
setAccountsetAccount After Line 3
MyDate.java
Class Diagram
TestBankingmainmain
Programming with Multiple Programming with Multiple ClassesClasses
Executing Line 4CustomerCustomer
getAccountgetAccount
getNamegetName
firstName=“John”firstName=“John”
cust
lastName=“Goodman”lastName=“Goodman”
account=nullaccount=null
setAccountsetAccount
acct
BankAccountBankAccount
withdrawwithdraw
depositdeposit
balance=3000.0balance=3000.0
acct
getBalancegetBalance
BankAccountBankAccount
withdrawwithdraw
depositdeposit
balance=3000.0balance=3000.0
acct
getBalancegetBalance
account=acctaccount=acct
TestBankingmainmain
Programming with Multiple Programming with Multiple ClassesClasses
CustomerCustomer
getAccountgetAccount
getNamegetName
firstName=“John”firstName=“John”
cust
lastName=“Goodman”lastName=“Goodman”
account=acctaccount=acct
setAccountsetAccount
After Line 4
BankAccountBankAccount
withdrawwithdraw
depositdeposit
balance=3000.0balance=3000.0
acct
getBalancegetBalance
Class Diagram
TestBankingmainmain
Programming with Multiple Programming with Multiple ClassesClasses
BankAccountBankAccount
withdrawwithdraw
depositdeposit
balance=3000.0balance=3000.0
acct
getBalancegetBalance
Executing Line 6CustomerCustomer
getAccountgetAccount
getNamegetName
firstName=“John”firstName=“John”
cust
lastName=“Goodman”lastName=“Goodman”
account=acctaccount=acct
setAccountsetAccount
deposit(1250.25)
1250.25
balance=4250.25balance=4250.25
MyDate.java
Class Diagram
TestBankingmainmain
Programming with Multiple Programming with Multiple ClassesClasses
CustomerCustomer
getAccountgetAccount
getNamegetName
firstName=“John”firstName=“John”
cust
lastName=“Goodman”lastName=“Goodman”
account=acctaccount=acct
setAccountsetAccount
After Line 6
BankAccountBankAccount
withdrawwithdraw
depositdeposit
balance=4250.25balance=4250.25
acct
getBalancegetBalance
Class Diagram with Multiple Class Diagram with Multiple ClassesClasses
Class Diagram of “Customer.java” and “BankAccount.java”
account
*
BankAccount
- balance : double
+BankAccount(initBalance:double)+ getBalance : double+ deposit(amt : double)+ withdraw(amt : double)
Customer
- firstName : String- lastName : String- account : BankAccount []
+Customer(f:String, l:String)+ getFirstName : String+ getLastName : String
+ setAccount( acct:BankAccount)+ getAccount( ) : BankAccount
Multiplicity“account” is an array of BankAccount
InheritanceInheritance
InheritanceInheritance การร�บ่ถ�ายท์อด้การร�บ่ถ�ายท์อด้
คื5ณส่มบ่�ต! คื5ณส่มบ่�ต!(Inheritance)(Inheritance)
หมายถ.ง การท์�%คืลัา ส่หน.%งๆ ร�บ่ถ�าย
คื5ณส่มบ่�ต! (inherit) ท์�$งคื�าคื5ณลั�กษณะ
(Attribute) แลัะ พัฤต!กรรม
(Method) มาจาก อ�กคืลัาส่หน.%ง
แต�ไม�รวมถ.ง คือน ส่ตร�คืเตอร�
(Constructor)
Em ployee
+ nam e : String+ salary : double+ birthDate : M yDate
+ getDetails( ) : String
M anager
+ departm ent : String
InheritanceInheritance
Class Diagram of “BankAccount” class and “SavingsAccount” class
BankAccount
+ BankAccount( intibalance:double)+ getBalance( ) : double+ deposit(amount : double)+ w ithdraw (amount : double)
- balance : double
SavingsAccount
- interestRate : double
+ SavingsAccount( intibalance:double, rate:double)
ร�บถุ�ายทีอด คั�ณส่มบ�ต�
(inherit)
SubclassingSubclassing พั!จารณาคืลัาส่ พั!จารณาคืลัาส่ EmployeeEmployee
เราส่ามารถส่ร�างว�ตถ5ท์�%ม�คืลัาส่เป็�น พัน�กงาน ได้�โด้ยการก&าหนด้คืลัาส่
“Employee” ถ�าเราต�องการส่ร�างคืลัาส่ของ“Manager” ท์�%ย�งคืงคื5ณส่มบ่�ต!เป็�น“Employee” คืนหน.%ง แต�ม�คื5ณส่มบ่�ต!บ่างอย�างเพั!%มเต!ม
public class Employee { public String name; public double salary; public MyDate birthDate;
public String getDetails( ) { … }}
Em ployee
+ nam e : String+ salary : double+ birthDate : M yDate
+ getDetails( ) : String
พั!จารณาคืลัาส่ พั!จารณาคืลัาส่ ManagerManager
“โคืรงส่ร�างคืลัาส่ Manager” ก�บ่“Employee” ซ&$าซ�อนก�น เราใช้�ว!ธ�การเช้!งว�ตถ5ส่ร�างคืลัาส่ใหม� จาก
คืลัาส่เด้!มท์�%ม�อย,�แลั�ว ว!ธ�การน�$เราเร�ยกว�า“Subclassing”
public class Manager { public String name; public double salary; public MyDate birthDate; public String department;
public String getDetails( ) { … }}
M anager
+ nam e : String+ salary : double+ birthDate : M yDate+ departm ent : String
+ getDetails( ) : String
SubclassingSubclassing
UML DiagramUML DiagramSubclassingSubclassing
Em ployee
+ nam e : String+ salary : double+ birthDate : M yDate
+ getDetails( ) : String
M anager
+ departm ent : String
การส่ร�างคืลัาส่ใหม�จากคืลัาส่เด้!มท์�%ม�อย,� แลั�ว ส่น�บ่ส่น5นแนวคื!ด้ ของการน&า
Source Code กลั�บ่มาใช้�ใหม�
Superclass
Subclass(Child Class)
(Parent Class)
InheritanceInheritance การร�บ่ถ�ายท์อด้คื5ณส่มบ่�ต! การร�บ่ถ�ายท์อด้คื5ณส่มบ่�ต!(Inheritance)(Inheritance)
หมายถ.ง การท์�%คืลัาส่ห น.%งๆ ร�บ่ถ�ายคื5ณส่มบ่�ต!
(inherit) ท์�$งคื�า คื5ณลั�กษณะ
(Attribute) แลัะ พัฤต!กรรม (Method)
มาจากอ�กคืลัาส่หน.%ง แต�ไม�รวมถ.ง คือนส่ตร�คื
เตอร� (Cosntructor)
► คั+อการที/ คั+อการที/ classclass ที/ต�างก�นม ที/ต�างก�นม AttributesAttributes แลัะ แลัะMethodsMethods ที/เหม+อนก�นที/เหม+อนก�น
Em ployee
+ nam e : String+ salary : double+ birthDate : M yDate
+ getDetails( ) : String
M anager
+ departm ent : String
Single InheritanceSingle Inheritance Single Single InheritanceInheritance
หมายถ.ง การท์�%คืลัาส่ห น.%งๆ ร�บ่ถ�ายท์อด้คื5ณส่มบ่�ต!
(inherit) ท์�$งAttribute แลัะMethod มาจากอ�กคืลัาส่หน.%ง เราเร�ยกว�าคืลัาส่“Manager” ร�บ่
ถ�ายท์อด้คื5ณส่มบ่�ต!(inherit) มาจาก คืลัาส่“Employee”
Em ployee
+ nam e : String+ salary : double+ birthDate : M yDate
+ getDetails( ) : String
M anager
+ departm ent : String
Multiple Multiple InheritanceInheritance Multiple InheritanceMultiple Inheritance
หมายถ.ง การท์�%คืลัาส่หน.%งๆ ร�บ่ถ�ายท์อด้ คื5ณส่มบ่�ต! (inherit) ท์�$งคื�าคื5ณลั�กษณะ
แลัะพัฤต!กรรมมาจากคืลัาส่มากกว�า 1 คืลัาส่People
+ Name : String+ id : String+ birthDate : M yDate
+ getDetails( ) : String
Em ployee
+ company : String+ salary : double+ startDate : M yDate
+ getDetails( ) : String
M anager
+ department : String
ภาษาจาวาภาษาจาวา ไม�ส่น�บ่ส่น5น
การท์&าMultiple Inheritance แต�ส่ามารถร�บ่ส่�บ่ท์อด้คื5ณส่มบ่�ต!จากหลัายคืลัาส่ได้�
โด้ยใช้�“Interface”
UML UML DiagramDiagram
Java InheritanceJava Inheritance
Em ployee
+ nam e : String+ salary : double+ birthDate : M yDate
+ getDetails( ) : String
M anager
+ departm ent : String
public class Manager extends Employee { public String department; }
จาวาจาวาpublic class Employee { public String name; public double salary; public MyDate birthDate;
public String getDetails( ) { … }}
การก&าหนด้คืลัาส่ท์�%ร�บ่คื5ณส่มบ่�ต!ส่�บ่ท์อด้ในการก&าหนด้คืลัาส่ท์�%ร�บ่คื5ณส่มบ่�ต!ส่�บ่ท์อด้ใน ภาษาจาวา ส่ามารถท์&าได้�ด้�งน�$ ภาษาจาวา ส่ามารถท์&าได้�ด้�งน�$
<modifier> <modifier> classclass child-class child-class
extends extends parent-class parent-class {{
::
}}
ต�วอย�างต�วอย�าง
Defining Extending Class in Defining Extending Class in JavaJava
public class Manager extends Employee { public String department;
}
““super” and “this” super” and “this” keywordkeyword
““this” keywordthis” keyword ใช้�อ�างอ!งถ.งคืลัาส่ป็3จจ5บ่�น ส่ามารถใช้�อ�างอ!งถ.ง Attribute หร�อMethod ของคืลัาส่ป็3จจ5บ่�น ได้�โด้ยใส่� .
แลั�วตามด้�วยช้�%อ Attribute หร�อ ช้�%อMethod
“ “super” keywordsuper” keyword ใช้�อ�างอ!งถ.ง superclass ของคืลัาส่ป็3จจ5บ่�น
super( ) อ�างอ!งถ.ง constructor ของ superclass
super.method( ) อ�างอ!งถ.งmethod ของ superclass
Top-Middle-Bottom (1)Top-Middle-Bottom (1)class Top { public Top( ) { System.out.println("Top()"); }}
class Middle extends Top { public Middle( ) { System.out.println("Middle()"); }}
class Bottom extends Middle { public Bottom( ) { System.out.println("Bottom()"); }}
public class Tester1 { public static void main(String[] args) { new Bottom(); }}
Top-Middle-Bottom (2)Top-Middle-Bottom (2)class Top { public Top( ) { System.out.println("Top()"); }}
class Middle extends Top { public Middle( ) { super( ) ; System.out.println("Middle()"); }}
class Bottom extends Middle { public Bottom( ) { super( ); System.out.println("Bottom()"); }}
public class Tester2 { public static void main(String[] args) { new Bottom(); }}
Overriding MethodsOverriding Methods subclass/child class ส่ามารถ
เป็ลั�%ยนแป็ลังพัฤต!กรรม ท์�% ร�บ่ถ�ายท์อด้ (inherit)
มาจากsuperclass/parent class ได้� subclass ส่ามารถ
ก&าหนด้ Method ท์�%ม�หน�าท์�%/ พัฤต!กรรม ต�างจาก Method ของsuperclass ได้�แต�ต�องม�ส่!%งต�อไป็น�$ท์�%เหม�อนก�น
Method Name Return Type Argument list
Em ployee
+ nam e : String+ salary : double+ birthDay : M yDate
+ getDetails( ) : String
M anager
- departm ent : String
+ getDetails( ) : String
Overriding Method (1)Overriding Method (1)public class Employee { public String name; public double salary; public String birthDate;
public Employee(String n, double s, MyDate bd) {name = n;salary = s;birthDate = bd;
}
public String getDetails( ) {return "name:"+name+",salary:"+salary
+"bd:"+birthDate.toString(); }}
Overriding Methods Overriding Methods (2)(2)
public class Manager extends Employee { public String department;
public Manager(String n, double s, MyDate bd, String dept) {name = n;salary = s;birthDate = bd;department = dept;
}
public String getDetails( ) {return "name:"+name+",salary:"+salary
+"bd:"+birthDate.toString() ++", department:"+this.department;
}}
Super & Overriding Super & Overriding Methods Methods
public class Manager extends Employee { public String department;
public Manager(String n, double s, MyDate bd, String dept) {name = n;salary = s;birthDate = bd;department = dept;
}
public String getDetails( ) {return super.getDetails()
+", department:"+this.department; }}
Super & Overriding Super & Overriding MethodsMethods
public class Manager extends Employee { public String department;
public Manager(String n, double s, MyDate bd, String dept) {super(n, s, bd);department = dept;
}
public String getDetails( ) {return super.getDetails()
+", department:"+this.department; }}
TestCompanyTestCompanypublic class TestCompany { public static void main(String[] args) { Employee jeff = new Employee( "Jeffrey" , 3000.0
, new MyDate(2, 3, 1970) ); Manager rob = new Manager(" Robert"
, 5000.0 , new MyDate(3, 4, 1965) , ”Sales");
System.out.println(jeff.getDetails( )); System.out.println(rob.getDetails( )); }} “TestCompany.java”
TestCompanymainmain
Employee & Manger ExampleEmployee & Manger Example
EmployeeEmployee
name=“Jeffrey”name=“Jeffrey”
jeff
Salary=1000.0Salary=1000.0
birthday=jeffdatebirthday=jeffdate
getDetailsgetDetails
MyDateMyDate
toStringtoStringsetDatesetDate
jeffdate
day=2;day=2;
month=3;month=3;
year=1970;year=1970;
MyDateMyDate
toStringtoStringsetDatesetDate
robdate
day=3;day=3;
month=4;month=4;
year=1965;year=1965;
ManagerManager
name=“Robert”name=“Robert”
rob
Salary=5000.0Salary=5000.0
birthday=Robdatebirthday=Robdate
getDetailsgetDetails
department=“Sales”department=“Sales”
Access ControlAccess Control การคืวบ่คื5มการเข�าถ.งข�อม,ลั (Access Control)
คื�อ ว!ธ�การเช้!งว�ตถ5 ท์�%ภาษาจาวาม�ไว�เพั�%อ ใช้� เข�ยนโป็รแกรมส่น�บ่ส่น5นคื5ณส่มบ่�ต!
Encapsulation แลัะ Information Hiding การระบ่5คื�า Access Control Specifier เป็�นการคืวบ่คื5มการเข�าถ.งข�อม,ลั โด้ยจะใช้�ระบ่5ก�อนหน�า ต�วแป็ร คื�าคืงท์�% เมท์ธอด้ แลัะคืลัาส่ คื�า Access Control Specifier ได้�แก�
public - เป็<ด้ให�เข�าถ.งข�อม,ลัได้�จากท์5กๆท์�% private - เป็<ด้ให�เข�าถ.งข�อม,ลัได้�ภายในขอบ่เขตท์�%ก&าหนด้ protected - เป็<ด้ให�เข�าถ.งข�อม,ลัได้�จากการส่�บ่ท์อด้คื5ณส่มบ่�ต!
class BankAccount { private double balance;
public BankAccount(double initAmount) { balance = initAmount;}
public void deposit(double amount) { balance = balance + amount;}
public void withdrawn(double amount) { balance = balance - amount;}
public int getBalance() { return balance;}
}
ประกาศ์การเขี!าถุ.งขี!อม3ลัขีองต�วิแปรภัายในคัลัาส่เป,น private เพ+/อให!ส่ามารถุเขี!าถุ.งได!จากภัายในคัลัาส่
ประกาศ์การเขี!าถุ.งขี!อม3ลัขีองคัอนส่ตร�คัเตอร�ขีองคัลัาส่เป,น public เพ+/อให!ผู้3!ขีอใช!บร�การจากวิ�ตถุ�ที/ถุ3กส่ร!างขี.�นจากวิ�ตถุ�น�ส่ามารถุส่ร!างวิ�ตถุ�น�ได!
ประกาศ์การเขี!าถุ.งขี!อม3ลัขีองเมทีธอดเป,น public
เพ+/อให!เมทีธอดขีองคัลัาส่ส่ามารถุให!บร�การแก�ผู้3!ขีอ
ใช!บร�การนอกขีอบเขีตขีองคัลัาส่
An Example of Access An Example of Access ControlControl
Note : กรณที/ประกาศ์การเขี!าถุ.งขี!อม3ลัขีองคัลัาส่เป,น public คัลัาส่น��น
จะต!องถุ3กบ�นที.กในไฟลั�ที/ม+อช+/อเดยวิก�บ คัลัาส่ “BankAccout.java” แยกจาก
ไฟลั�ที/ระบ�ผู้3!ส่ร!างวิ�ตถุ�จากคัลัาส่ หร+อขีอร�บบร�การจากคัลัาส่น�
public
Inheritance & Access ControlInheritance & Access Control
การระบ่5คื�าการเข�า ถ.งข�อม,ลั (Access
Control) ให�เป็�นpublic ท์&าให�คืลัา
ส่อ�%นๆ ท์5กคืลัาส่ ส่ามารถเข�าถ.ง
Attributes ของ คืลัาส่ Employee
ได้� ถ�าต�องการให�เฉพัาะ คืลัาส่Manager ส่ามารถ
เข�าถ.ง Attributes ของ Employee จะ
ท์&าอย�างไร?
Em ployee
+ name : String+ salary : double+ birthDate : M yDate
+ getDetails( ) : String
M anager
- department : String
พั!จารณา
Inheritance & Access Inheritance & Access ControlControl
ส่ามารถก&าหนด้เข�า ถ.งข�อม,ลั (Access
Control) ให�ส่ามารถเข�าถ.งได้�ผ�านท์างการส่�บ่ท์อด้คื5ณส่มบ่�ต!เท์�าน�$น
ท์&าได้�โด้ยระบ่5คื�าAccess Control Specifier เป็�นprotected
พั!จารณาEmployee
# name : String# salary : double# birthDate : MyDate
- getDetails( ) : String
Manager
- department : String
protected
Employee & Manager Employee & Manager ExampleExample
public class Manager extends Employee { private String department; public String getDetails( ) {
return super.getDetails( ) + ", departmentt:"+department); }}
public class Employee { protected String name; protected double salary; public String getDetails( ) {
return ("name:"+name+ ”, salary:"+salary); }}
““Point” ClassPoint” Classpublic class public class Point {
protected int x,y;public Point() {
setPoint(0, 0);}public Point(int a, int b) {
setPoint(a, b);}public void setPoint(int a, int b) {
x = a;y = b;
}public int getX() { return x; }public int getY() { return y; }public String toString() { return "("+x+","+y+")"; }
}
““Circle” ClassCircle” Classpublic class Circle extends Point {
protected double radius;public Circle() {
setRadius(0.0);}public Circle(double r, int a, int b) {
super(a , b);setRadius(r);
}public void setRadius(double r) { radius = r; }public double getRadius() { return radius; }public double area() { return Math.PI * radius * radius; }public String toString() {
return "Center = "+"("+ x +","+ y +")" +"; Radius = "+ radius;
}}
““Cylinder” ClassCylinder” Classpublic class Cylinder extends Circle {
protected double height;public Cylinder() { setHeight(0); }public Cylinder(double r, double h, int a, int b) { super(r, a , b); setHeight(h);}public void setHeight(double h) { height = h; }public double getHeight() { return height; }public double area() {
return 2 * super.area() +2 * Math.PI * radius * height; }public String toString() { return super.toString()+ "; Height = " + height;}
}
““TestInheritance” ClassTestInheritance” Classpublic class TestInheritance { public static void main(String[] args) {
Cylinder c = new Cylinder(5.7, 2.5, 12, 23);System.out.println("X coordinate is "+ c.getX() +
"\nY coordinate is "+ c.getY() +"\nRadius is "+ c.getRadius() +"\nHeight is ”+c.getHeight() +"\nCylinder Area = "+ c.area());
c.setHeight(10);c.setRadius(4.25);c.setPoint(2,2);System.out.println("\n\nThe new Location : "+
"\nX coordinate is "+ c.getX() +"\nY coordinate is "+ c.getY() +"\nRadius is "+ c.getRadius() +"\nHeight is ”+ c.getHeight() + "\nCylinder Area = "+ c.area());
}
Inheritance Inheritance Relationships Relationships
of of Point, Circle and Point, Circle and
CylinderCylinderC ircle
# radius : double
+ Circle()+ Circle(r:double, a: int, b: int)+ setRadius(r:double)+ getRadius() : double+ area() : double+ toString() : String
Cylinder
# height : double
+ Cylinder()+ Cylinder(r:double, h:double, a: int, b: int)+ setHeight(h:double)+ getHeight( ) : double+ area() : double+ toString() : String
Point
# x : int# y : int
+ Po int( )+ Po int(a: int, b: int)+ setPo int(a : int, b : int)+ getX() : int+ getY () : int+ toString() : String
Object Diagram of Object Diagram of TestInheritanceTestInheritance
TestInheritancemainmain
CylinderCylinder
x = 12x = 12
c
radius = 5.7radius = 5.7
height = 2.5height = 2.5
getXgetX
y = 23y = 23
getYgetY
getRadiusgetRadius
getHieghtgetHieght
setRadiussetRadius
setHeightsetHeight
setPointsetPoint
areaarea
toStringtoString
SpecializationSpecialization Subclass/Child Class ร�บ่ถ�ายท์อด้
คื5ณส่มบ่�ต! (inherit) ท์�$ง Attributes แลัะMethods มาจาก Superclass/Parent Class
Em ployee
+ nam e : String+ salary : double+ birthDate : M yDate
+ getDetails( ) : String
M anager
+ departm ent : StringManagManag
erer
EmployeeEmployee
GeneralizationGeneralization
Em ployee
+ nam e : String+ salary : double+ birthDate : M yDate
+ getDetails( ) : String
M anager
+ departm ent : String
เป็�น flip-side ของ Specialization การท์�% class ท์�%ต�างก�นม� Attributes แลัะMethods ท์�%เหม�อนก�น
IS - IS - AA
EmployeeEmployee
ManagManagerer
Class HierarchiesClass HierarchiesPoint
Circle Square
Cylinder Sphere Box Cubic
ลั,กศรใน Class Hierararchies แส่ด้งคืวามส่�มพั�นธ�ของการร�บ่ถ�ายท์อด้คื5ณส่มบ่�ต!
Cylinder extends Circle Circle extends Point
IS-A RelationshipsIS-A Relationships การถ�ายท์อด้คื5ณส่มบ่�ต!เป็�นคื5ณลั�กษณะ
แบ่บ่ transitive Snake IS-A Reptile แลัะReptile IS-A Animal น�%นคื�อ Snake IS-A AnimalAnimal
Reptile Mammal
Snake Lizard Dog Bat
Bird
Eagle
IS-A
IS-A
Adopting a DesignAdopting a Design การออกแบ่บ่ Class Hierarchies ท์�%
เหมาะส่ม เป็�นส่!%งส่&าคื�ญการการออกแบ่บ่ระบ่บ่เช้!งว�ตถ5 Alternatives Hierarchies ข.$นก�บ่ระบ่บ่ท์�%ต�องการออกแบ่บ่Animal
Mammal
Dog Bat
Bird
Eagle
หร�อ
Flying Creature
BirdMosquito Bat
Eagle
The “Object”ClassThe “Object”Class ในภาษาจาวา
คืลัาส่ “Object” เป็�นคืลัาส่บ่นส่5ด้ของ คืลัาส่ท์�$งหมด้ท์�%ป็ระกอบ่ก�นข.$นเป็�น Class
Hierarchies คืลัาส่ท์�%ป็ระกาศ โด้ยไม�ใช้�คื&าเฉพัาะ“extends” ระบ่5การส่�บ่ท์อด้คื5ณส่มบ่�ต!
จากคืลัาส่ใด้ๆ ถ�อว�าเป็�นการลัะ“extends Object” ไว�
public class Employee { ...}
public class Employee extends Object { ...}
เหม+อนก�บ
Methods defined in Methods defined in “Object”Class“Object”Class
Methods ท์�%ถ,กก&าหนด้ไว�ในคืลัาส่“Object” ให�คืลัาส่ท์�%ร�บ่ส่�บ่ท์อด้คื5ณส่มบ่�ต!
ส่ามารถเร�ยกใช้� หร�อท์&าการ Overriding Methods เหลั�าน�$ ได้� ต�วอย�างเช้�น
equals(Object object) : Indicates whether some other object is "equal to" this one. toString() : Returns a string representation of the object Clone() : Creates and returns a copy of this object. ...etc.
ด้,รายลัะเอ�ยด้ใน Java & API Language Specification
Two Approaches to reuse Two Approaches to reuse SoftwareSoftware
InheritanceInheritance IS-AIS-A relationship relationship
CompositionComposition HAS-AHAS-A relationship relationship
HAS-AHAS-ACustomer BankAccount
Em ployee
M anager
IS-AIS-A
Benefits of InheritanceBenefits of Inheritance Avoiding redundanciesAvoiding redundancies
Source Code Source Code ในส่�วนท์�%เหม�อนก�นจะถ,กในส่�วนท์�%เหม�อนก�นจะถ,กเข�ยนข.$นเพั�ยงคืร�$งเด้�ยวเข�ยนข.$นเพั�ยงคืร�$งเด้�ยว
Code reuse & Code SharingCode reuse & Code Sharing ส่ามารถน&าไป็ใช้�ใหม�ได้� ส่ามารถน&าไป็ใช้�ใหม�ได้� (reuse) (reuse) ได้�โด้ยการร�บ่ได้�โด้ยการร�บ่
ถ�ายท์อด้คื5ณส่มบ่�ต!ถ�ายท์อด้คื5ณส่มบ่�ต! ส่ามารถส่ร�างคืลัาส่ใหม�จากคืลัาส่เด้!มท์�%ม�อย,�ส่ามารถส่ร�างคืลัาส่ใหม�จากคืลัาส่เด้!มท์�%ม�อย,�
แลั�วได้� แลั�วได้� Reduced code size Reduced code size
Source Code Source Code ถ,กเข�ยนข.$นเพั�ยงคืร�$งเด้�ยว ถ,กเข�ยนข.$นเพั�ยงคืร�$งเด้�ยว แลัะถ,กน&าไป็ใช้�ใหม�ได้�ไม�จ&าก�ด้ แลัะถ,กน&าไป็ใช้�ใหม�ได้�ไม�จ&าก�ด้
Costs of InheritanceCosts of Inheritance Message-passing overheadMessage-passing overhead
เน�%องจากอน5ญาตให�ม� เน�%องจากอน5ญาตให�ม� Code Reuse Code Reuse ท์&าให�ท์&าให� ต�องม�การส่�งต�อคืวามต�องการไป็ย�งคืลัาส่อ�%นๆ ต�องม�การส่�งต�อคืวามต�องการไป็ย�งคืลัาส่อ�%นๆ
ท์�%ส่�มพั�นธ�ก�นด้�วยการถ�ายท์อด้คื5ณส่มบ่�ต!ท์�%ส่�มพั�นธ�ก�นด้�วยการถ�ายท์อด้คื5ณส่มบ่�ต! Execution SpeedExecution Speed
คืวามเร6วในการป็ระมวลัผลัช้�าลัง เน�%องจาก คืวามเร6วในการป็ระมวลัผลัช้�าลัง เน�%องจากต�องส่�งต�อคืวามต�องการไป็ขอร�บ่บ่ร!การจากต�องส่�งต�อคืวามต�องการไป็ขอร�บ่บ่ร!การจากคืลัาส่อ�%นๆคืลัาส่อ�%นๆ
Program ComplexityProgram Complexity เพั!%มคืวามซ�บ่ซ�อนของโป็รแกรมเพั!%มคืวามซ�บ่ซ�อนของโป็รแกรม
SummarySummary• Overloading Methods and Constructors• Programming with multiple classes• Inheritance• Overriding Methods and Constructor• Access control and inheritance