Introduction to OOP
Download
Report
Transcript Introduction to OOP
Lec07 :: OOP
วิชา :: การเขียนโปรแกรมเชิงวัตถุ
โดน อ. นัฐพงศ์ ส่ งเนียม
[email protected]
http://www.siam2dev.com
Agenda
► Imparative
Programming : การเขียนโปรแกรมแบบดั้งเดิม
► OOP Programming : การเขียนโปรแกรมเชิงวัตถุ
► Imparative VS OOP : เปรี ยบเทียบข้อแตกต่าง
► Fundamental of OOP : หลักการสาคัญของ OOP
Class : ความหมายของ Object
Object : ความหมายของ class
Inheritance
Polymorphism
Overloading
Imperative VS Object-oriented
Imperative
Object-oriented
► Top-down
► Bottom-up
design
► Procedure-oriented
► Algorithm
► Share global data
► Program-data
separation
► Data transportation
► Single flow
► Object-oriented
► Behavior
► Information
hiding
► Encapsulation
► Communication
messages
► Multiple objects
with
การเขียนโปรแกรมแบบ imparative
Imparative
OOP
ข้อดีของ OOP
► เข้าใจง่าย เพราะการทางานเปรี ยบเสมือนการจาลองเหมือนในโลก
จริ ง โดยอาศัยการมองทุกอย่างเป็ น object ซึ่งแต่ละตัวมีหน้าที่
และความหมายในตัว
► บารุ งรักษา และแก้ไขโปรแกรมได้ง่าย
► มีความปลอดภัยสู ง เพราะจัดการกับ error ได้ดี
► นากลับมาใช้ใหม่ได้ (reusability) ลดขั้นตอนในการเขียน
โปรแกรม
► โปรแกรมมีคุณภาพสู ง ใช้ได้หลาย platform
ข้อเสี ยของ OOP
► เข้าใจยาก สาหรับผูเ้ ริ่ มต้นเขียนโปรแกรม หรื อถนัดเขียนโปรแกรม
แบบ procedural
► ทางานได้ชา้ กว่าภาษาโปรแกรมอื่น
► ภาษามีความกากวม ถ้ามีลกั ษณะ multiple inheritance
หลักการของการพัฒนาซอฟท์แวร์ด้วย
Object-Oriented Programming
คือการแบ่งซอฟท์แวร์ออกเป็นส่วนๆ เรียกว่า class โดยการ
นิยาม class และ object ทั้งนี้เพื่อทาให้สามารถนาส่วน
ของซอฟท์แวร์นั้นกลับมาเรียกใช้ได้อีก (reuseable) ลด
ความซ้าซ้อนและเวลาลงได้
การทางานของ class จะถูกกาหนดโดยส่วนอินเตอร์เฟส
ของ method ส่วนการทางานของส่วนที่เป็นโค้ต จะไม่ถูก
คานึงถึงในการออกแบบ. ภาษา OOP สนใจเฉพาะข้อมูลที่
จะถูกประมวลผลมากกว่าฟังก์ชั่นที่ทาการประมวลข้อมูลนั้น
หลักการทีส
่ าคัญบางประการของ OOP
•
•
•
•
•
Class and Subclass
Encapsulate
Inheritance
Polymorphism
Abstract Data Type
เหตุผลที่ OOP มีบทบาทมากขึน้
• ง่าย และรวดเร็ว ทาให้ลดเวลาในการพัฒนาลงไปได้
• เพิ่มปริมาณงานที่ได้ และมีความน่าเชือ
่ ถือมากกว่า
• สามารถนา code กลับมาใช้ได้อก
ี (เรียกใช้ class)
• ทาต้นแบบ (Prototyping) ได้รวดเร็วกว่า
• ลดต้นทุนในการสร้าง และบารุงรักษาซอฟต์แวร์
• การเปลี่ยนแปลงแก้ไข ไม่ทาให้เกิดผลกระทบไปยัง
ภายนอก class
มารู้จกั คลาสและวัตถุกนั
► คลาสเป็ นเหมือนส่ วนของแม่พิมพ์ เพื่อบอกลักษณะว่าวัตถุในคลาสนี้ มี
ลักษณะและพฤติกรรมเป็ นอย่างไร
► วัตถุเป็ นสิ่ งที่ถูกสร้างขึ้นมาจากคลาสเพื่อใช้งาน กาหนดค่าต่างๆ ส่ ง
ข้อความสัง่ ให้วตั ถุทางาน
คลาสสี่ เหลีย่ มผืนผ้า
คุณสมบัติ:กว้าง, ยาว
เมธทอด:คานวณพื้นที่
วัตถุA:สี่ เหลีย่ มผืนผ้า
กว้าง 2 ยาว 3
เมธทอด:คานวณพื้นที่
Class คืออะไร
คือกลุ่ม (category) ของ objects ที่มีคุณสมบัติ
และพฤติกรรมทีเ่ หมือนกัน โดย class จะต้อง
ประกอบไปด้วย data, behavior และ
interface
คือต้นแบบ (prototype) หรือพิมพ์เขียว ที่
กาหนดตัวแปรและวิธีการ เพื่อนาไปใช้ได้ใน
ทุก object ของ class
เปรี ยบเทียบคลาสกับวัตถุ
Class
ยานพาหนะ
ทางอากาศ
ทางบก
ทางบก
รถสองล้อ
รถสามล้อ
รถสี่ ล้อ
รถสิ บล้อ
Objects
คือ สิ่ งใดๆ ก็ตาม ซึ่ งมีคุณลักษณะ (State) บ่งบอกถึงความเป็ นตัวของมันเองใน
ขณะนั้น และสามารถแสดงพฤติกรรม (Behavior) ของตัวเองออกมาได้ เช่น
รถยนต์สีน้ าเงิน : มีความหมายคือ วัตถุประเภทรถยนต์ มีคุณลักษณะของสี เป็ นสี น้ า
เงิน และมีพฤติกรรมที่แสดงถึงการเคลื่อนที่ และหยุดได้
หรื อกล่าวได้วา่ object ก็คือข้อมูลของ class (เป็ นentities ของ class)
ซึ่ งทุกๆ อย่างจะจัดเป็ น objects โดยต้องประกอบไปด้วย
►ชื่อ (Identity)
►สถานะ (State) คุณสมบัติ หรื อค่าของข้อมูล ซึ่ งแทนด้วย value
►พฤติกรรม (Behavior) ที่ระบุวา่ สามารถทาอะไรได้บา้ ง ซึ่ งแทนด้วย
method
ตัวอย่างชนิดของวัตถุและวัตถุ
:siemens
MobilePhone
String bandname
Button button
Screen screen
Antenna antenna
Simm simNet
call()
receive()
hangup()
sendSMS()
:ericsson
:nokia
Method
คือ function ทีบ่ ่ งบอกพฤติกรรมของ object ว่ าทาอะไรได้ บ้าง
กาหนดไว้ ใน class โดยต้ องประกอบด้ วย ชื่อของ method เรียกว่ า
Identifier ตามด้ วยเครื่องหมายวงเล็บ () โดยในวงเล็บอาจมี
parameter list อยู่หรือไม่ กไ็ ด้ เช่ น
getBalance()
raiseSalary( float Salary, float Percent )
Constructor Method
คือเมธอดทีใ่ ช้ สาหรับสร้ าง instance object ของคลาสนั้นๆ โดยที่
ชื่อเมธอดนีต้ ้ องเหมือนกับชื่อคลาส และใช้ สาหรับ initialize ข้ อมูลให้ กบั
instance variable โดยจะไม่ มีการถ่ ายทอดให้ กบั
subclass และไม่ มีการ return ค่ า
Message
คือคาสั่งหรื อข้อความที่จะให้ขอ้ มูลหรื อตัวแปรใดทางาน ก็คือ parameter
ในภาษาอื่นที่ไม่ใช่ OOP คือใช้เพื่อนาส่ งค่าข้อมูลระหว่าง object โดยใน
message นั้นต้องประกอบด้วย
Destination ก็คือชื่อของ object
Method
Parameters
Accessibility
public : เข้าถึงได้ในทุกที่
private : เข้าถึงได้เฉพาะภายใน class เท่านั้น ไม่รวม sub class
protected : เข้าถึงได้เฉพาะภายใน class และ sub class ที่
สื บทอดกันมา (Inherit)
default : ถ้าไม่ระบุ จะเข้าถึงข้อมูลภายใน class และอยูเ่ พ็กเกจ
เดียวกัน
Public
Class
Sub Class
Encapsulate
คือ การปิดบัง หรือจากัดการเข้าถึงข้อมูลบางอย่าง
(Information hiding) ที่ไม่จาเป็นต้องให้สว
่ นอื่นรับรู้ ยกตัวอย่าง
เช่น เราจะไม่สนใจหรือมองเห็นได้วา่ เครือ
่ งเล่น CD จะ
แปลงสัญญาณดิจิตอล ออกมาเป็นเพลงได้อย่างไร เราใช้
และติดต่อกับเครือ
่ งแค่ควบคุมการทางานผ่านแผงควบคุม
เช่น เปิด-ปิด เล่น เร่งเสียง เปลี่ยนแทร๊กไปข้างหน้า
ย้อนกลับ เป็นต้น โดยเราต้องออกแบบควบคุมกฏเกณฑ์
ต่างๆ ของซอฟท์แวร์ให้สอดคล้องกับความเป็นจริง
Encapsulate เป็นคุณสมบัตข
ิ อง object ซึ่งมีลักษณะดังนี้
กาหนดขอบเขตที่ชด
ั เจนให้กบ
ั object
กาหนดอินเตอร์เฟสว่าจะติดต่อกับ object อื่นๆ อย่างไร
Encapsulate
Inheritance
คือการถ่อยทอดข้อมูล (ซึ่ งก็คือ state และ behavior) จาก class
ลาดับที่สูงกว่า (super class หรื อ parent class) ไปยังลาดับที่ต่า
กว่า (subclass) โดยที่ subclass นั้นสามารถเปลี่ยนแปลง หรื อ
แทนที่ขอ้ มูล (override) ที่ได้รับการถ่ายทอดมานั้นได้
Class Animal
Animal
-name
-leg
-Eat()
-Sleep()
Inheritance : สื บทอดคุณสมบัติ
► ตัวอย่าง Tiger
Class
super Class
sub Class
Class สามารถสื บทอดคุณสมบัติจาก Animal
Animal Class
Polymorphisim
การทาให้ message อันหนึ่งสามารถส่ งให้ object แต่ละตัวใน
class และ subclass ตอบสนองต่อ message อันเดียวกัน ใน
ลักษณะที่เหมาะสบกับ class ของตัวเอง
ยกตัวอย่างเช่น method print นี้สามารถส่ งให้ทุก object ของ
class และ subclass ที่ทาให้ object นั้นรู ้จกั method print
และแต่ละ object ที่ต่างกันจะตอบสนองต่อ message นี้ต่างกัน
ออกไป ตามความสามารถในการใช้
Polymorphisim
การถ่ ายทอดให้ เกิดลักษณะของพ้ องรู ป
Shape
+draw()
+erase()
Circle
+draw()
+erase()
Line
+draw()
+erase()
Rectangle
+draw()
+erase()
โครงสร้ างของ Class
โปรแกรมที่สร้างจากภาษาจาวา ต้องมีออปเจ็คหรื อคลาสอย่างน้อยหนึ่งตัว โดยมีรูปแบบ
โครงสร้างดังนี้
class Class_Name
{ Data_Member
Method_Member }
//รูปแบบของ Data Member
[ Accessibility ] [ final ] [ static ] Data_Type Data_Name
เช่น private static String stu_name ;
รูปแบบของ Data Member
[ Accessibility ] [ final ] [ static ] Data_Type Data_Name
เช่น private static String stud_name ;
Data คือส่ วนประกอบส่ วนหนึ่งของคลาส ถูกกาหนดเพื่อใช้สาหรับเก็บข้อมูล
เช่นคลาส Pen มีดาต้า Color ไว้เก็บข้อมูลสี
class Pen
{
String Color = “red”; // Data Member
}
final
► final
: เป็ นคีย์เวิร์ดตัวหนึ่งซึ่งใช้ บอกว่ าดาต้ าตัวนั้นใช้ สาหรับเก็บข้ อมูล โดย
ทีข่ ้ อมูลจะไม่ สามารถทาการเปลีย่ นแปลงได้ ไม่ ว่าในกรณีใดๆ ปกติจะใส่ คยี ์เวิร์ดนี้
ไว้ เมือ่ ต้ องการให้ ดาต้ าเก็บข้ อมูลทีเ่ ป็ นข้ อมูลที่คงที่ (Constant) ตลอด
การทางานของโปรแกรม
► class
CircleArea {
public static void main(String args[])
{
final double pi = 3.14;
final double r = 10;
System.out.println(“Circle Area = " + pi*r*r);
}
}
static
► static
: เป็ นคียเ์ วิร์ด สาหรับใช้บอกถึงคุณลักษณะพิเศษในการใช้งาน เมื่อมีการ
กาหนดนาหน้าดาต้าใดๆ แล้ว ดาต้านั้นจะมีคุณลักษณะดังนี้
• ดาต้ า จะถูกโหลดลงในหน่วยความจาและพร้อมที่จะถูกใช้งานในทันทีเมื่อมีการ
อ้างถึง ตามข้อกาหนดของระดับการเข้าถึง (Accessibility)
• ดาต้ า จะอยูใ่ นหน่วยความจาเพียงตัวเดียว ไม่มีการ copy static
method ไปกับ instance object คือจะผูกติดอยูก่ บั คลาสเท่านั้น ดังนั้น
จึงสามารถใช้ดาต้าเป็ นที่เก็บข้อมูลรวมของกลุ่มคลาสเดียวกันได้
• ไม่สามารถเรี ยกใช้ instance variables หรื อ instance
method ได้
• ไม่สามารถใช้คียเ์ วิร์ด this ได้
โครงสร้ างของ Object
►เมื่อกล่าวถึงคาว่าออปเจ็คทุกคนจะเข้าใจว่าเป็ นชื่อเรี ยกที่ใช้เรี ยกส่ วนที่
สร้ างจากโครงร่ างของคลาส ซึ่ งออปเจ็คถูกสร้างโดยการใช้คาสัง่ ใน
ภาษาจาวาสร้างขึ้น จากขั้นตอนสองขั้นตอน คือ
1. ขั้นตอนประกาศชื่อตัวแปรอ้ างถึง(Reference Variable)
2. ขั้นตอนการสร้ างอินสแตนซ์ (Instance of Class)
1. ขั้นตอนประกาศชื่อตัวแปรอ้ างถึง (Reference Variable)
คือขั้นตอนที่ใช้กาหนดชื่อตัวแปรเพื่อใช้ในการอ้างถึงไปยังพื้นที่ในหน่วยความจา
โดยพื้นที่ส่วนนี้เป็ นส่ วนที่จดั เก็บอินสแตนซ์ที่สร้างขึ้นมาในภายหลัง โดยมี
รู ปแบบการกาหนดดังนี้
Class_Name Reference_Name
►Class_Name
: คือชื่อของคลาสซึ่งอ้างถึงด้วย Reference_Name ได้
►Reference_Name : คือชื่อตัวแปรที่ถูกกาหนดขึ้นโดยผูเ้ ขียนโปรแกรม
2. ขั้นตอนการสร้ างอินสแตนซ์ (Instance of Class)
อินสแตนซ์ คือพื้นที่ในหน่วยความจาซึ่ งสร้างตามแบบโครงร่ างของคลาสใดๆ
ดังนั้นอินสแตนซ์คือพื้นที่ที่สามารถเก็บข้อมูลและประมวลผลตาม ส่ วนที่สามารถ
ประมวลผลได้นนั่ เอง มีรูปแบบการสร้างดังนี้
new Class_Constructor(Parameter_List )
- new : คือคียเ์ วิร์ดของภาษาจาวา เพื่อเป็ นคาสั่งให้มีการจัดสรรพื้นที่
หน่วยความจา ตามโครงสร้างของคลาสจากที่กาหนดไว้แล้วในขั้นตอนที่ผา่ นมา
- Class_Constructor : คือเมธรอดของคลาสที่เป็ นคอนสตรักเตอร์
(เมธรอดที่เป็ นคอนสตรักเตอร์ มีชื่อเดียวกับชื่อคลาสเสมอ) โดยคลาสหนึ่งคลาส
สามารถมีเมธรอดคอนสตรักเตอร์ น้ ีได้หลายเมธรอด (ชื่อเดียวกันแต่พารามิเตอร์
ต่างกัน)
-Parameter_List : คือพารามิเตอร์ (ตัวแปรหรื อค่าคงที่ใดๆ) ที่ผา่ น
ข้อมูลเข้าไปสู่ เมธรอดคอนสตักเตอร์ ซึ่ งจานวนหรื อชนิดข้อมูลที่เป็ นพารามิเตอร์
ขึ้นอยูว่ า่ ผูเ้ ขียนโปรแกรมเรี ยกใช้คอนสตรักเตอร์ แบบไหน
ตัวแปรอ้ างถึงและอินสแตนซ์
(Reference variable and Instance)
ในภาษาจาวาตัวแปรอ้างถึงและอินแตนซ์มีความสัมพันธ์ซ่ ึ งกันและกัน กล่าวคือ
ตัวแปรอ้างถึงถูกกาหนดขึ้น เพื่อประโยชน์ในการอ้างไปยังตาแหน่งของ
อินสแตนซ์ที่อยูใ่ นหน่วยความจา โดยในหน่วยความจาบนเครื่ องคอมพิวเตอร์
บางครั้งสามารถสร้างอินสแตนซ์ได้หลายๆอินสแตนซ์ของการทางานแต่ละครั้ง
จึงจาเป็ นต้องใช้ชื่อตัวแปรอ้างถึงเพื่อให้สามารถเข้าใช้งานดาต้าและเมธรอดใน
อินสแตนซ์ได้ ตัวอย่างเช่น
อินสแตนซ์ (instance)
► ถ้าพิจารณาถึงลูกโป่ งสวรรค์ เปรี ยบกับคนขายลูกโป่ งสวรรค์ตามงานทัว่ ไป
ถ้าให้ลูกโป่ งแต่ละลูกเป็ นอินสแตนซ์ และให้เชือกแต่ละเส้นคือตัวแปรอ้าง
ถึง ดังนั้นเมื่อต้องการลูกโป่ งลูกใดๆ ก็ให้หยิบเส้นเชือกขึ้นมาทาให้รู้วา่ เป็ น
เชือกที่ผกู อยูก่ บั ลูกโป่ งลูกใด
instatnce
reference
ตัวอย่ าง การสร้ างอินสแตนซ์
Integer x = new Integer(237)
หมายถึงกาหนดตัวแปรอ้างถึงชื่อ x โดยมีการอ้างถึงไปยัง อินสแตนซ์ ที่
เกิดจากคลาสที่ชื่อ Integer และให้ อนิ สแตนซ์ น้ันเก็บค่ า 237 ไว้
โดยที่ Integer คือชื่อคลาสทีใ่ ช้ สาหรับจัดการข้ อมูลตัวเลขจานวน
เต็ม
► หรือจากคาสั่ ง
ต.ย. การสร้ าง คลาส และ ออบเจ็กต์
class rectangle {
int w=5;
int h=10;
public double CalArea() {
return w*h;
}
}
10
5
public class testRec
{
public static void main(String args[]) {
rectangle r = new rectangle();
System.out.println(r.CalArea());
}
};
ต.ย. การสร้ าง คลาส และ ออบเจ็กต์ rectangle & constructor
class rectangle {
int width;
int height;
rectangle(int w , int h) {
width = w;
height = h;
}
public double CalArea() {
return width*height;
}
}
10
5
public class testRec
{
public static void main(String args[]) {
rectangle r = new rectangle(5,10);
System.out.println(r.CalArea());
}
};
Inheritance
class father {
void homeaddr(){
System.out.println(“lp”);
}
}
class child extends father {
public static void main (String args[]) {
homeaddr();
}
}
Encapsulation
class friend {
private int val;
public int getter(){
return this.val;
}
public void setter(int a)
this.val=a;
}
}
{
class child {
public static void main (String args[]) {
friend x = new friend();
x.setter(5);
System.out.println(x.getter());
}
}
ต.ย. เรียก object จากอีก class หนึ่งในแฟ้ม x.java เดียวกัน แต่ ไม่ มี constructor
:: มี 2 กล่ อง ให้ เรียก object จากอีกกล่ องมาทางาน แสดงวิธีเรียก 2 วิธี
class xxx {
void prtxx() { System.out.println("xx");
}
}
class x {
public static void main(String args[]) {
System.out.println("x");
new xxx().prtxx();
xxx a = new xxx(); // ไม่ มี constructor ใน xxx a.prtxx();
}
}
// Result of this program // x // xx // xx
ต.ย. Constructor
► Inheritance
และ constructor : run constructor ของ
superclass ตามด้วย subclass
:: ปกติ default constructor คือ object ที่มีชื่อเดียวกับ class และ
ไม่มีส่วนขยายใด ๆ
:: a.java มี constructor เป็ นโปรแกรมที่ compile ผ่าน run ไม่ได้
แต่มีไว้ให้ถกู เรี ยกใช้ได้
:: b.java มี constructor และทางานภายใต้ main เรี ยก
constructor ทั้ง 2 มาทางาน
:: constructor คือ default object จะส่ งค่าให้ constructor ก็
ได้
:: การแปล b.java จะเรี ยก a.java มาแปลด้วยถ้าไม่พบ a.class แต่ถา้ พบ
และ a.java ไม่เปลี่ยน ก็จะไม่แปล a.java ใหม่
// a.java
public class a {
a() {
System.out.println("a constructor");
}
}
// b.java
public class b extends a {
b(int i) {
System.out.println("b constructor " + i);
}
public static void main (String[] args) {
b xxx = new b(100); }
}
// Result of this program
// a constructor
// b constructor 100
•Class Design in Java
Overloading
Programming with Multiple Classes
Inheritance
Inheritance and Access Control
Nattapong Songneam
Overloading
Overloading Methods
การประกาศเมทธอดซา
้ ซ้อน (Overloading Methods)
คือ วิธีการเชิงวัตถุ ที่มีไว้เพื่อนาชื่อเมทธอด
(Methods Name) กลับมาใช้ใหม่ (reuse)
ภาษาโปรแกรมจาวาสนับสนุน Overloading Methods
โดย
อนุpublic
ญาตให้
ประกาศเมทธอดที
่มีชื่อเดียวกัน
void deposit(double
amount);
public void deposit(int amount);
voidม
deposit(double
amount,
double interest);
ซา้ กันpublic
ได้
แต่
ี
เ
งื
อ
่
นไขดั
ง
นี
้
public boolean deposit(double amount);
ชนิด หรือ จำนวน หรือ ลำดับของ argument
Overloading Constructors
การประกาศคอนสตรัคเตอร์ซา
้ ซ้อน (Overloading
Constructors)
คือ วิธีการเชิงวัตถุ ที่มีไว้เพื่อเพิ่มความ
ยืดหยุ่นให้กบั การสร้างวัตถุ กรณี ที่ไม่สามารถ
กาหนดข้อมูลเริ่มต้นที่แน่ นอนในขณะสร้าง
วัตถุ
public BankAccount (double initAmount) {/*…*/ }
public BankAccount (String customerName) {/*…*/ }
public
ภาษาโปรแกามจาวาสนั
บสนุน Overloading
BankAccount ( ) { /*…*/ }
public BankAccount (double initAmount, String customerName) {/*…*/ }
Constructors โดย
An Example of Overloading
กำรกำหนดคอนสรัคเตอรซ
์
ซ้อน มีประโยชนในกรณี
ท
์
ไมสำมำรถก
ำหนดคำจ
public OverLoadedBankAccount( ) {
่
่ ำนวน
balance = 0.0;
เงินฝำกเริม
่ ตนเมื
อ
่ เปิ ดบัญช
้
}
ทำให้กำรใช้งำนโปรแกรม
public OverLoadedBankAccount(double intBalance) { มีควำมยืดหยุน
่
public class OverLoadedBankAccount {
private double balance;
balance = initBalance;
}
นำชือ
่ เมทธอดกลับมำใช้ไหม่
โดยระบุ “ชนิด” ของ Argument
เป็ นประเภทขอมู
่ ตกตำงกั
น
้ ลทีแ
่
public void deposit(int amount) {
balance = balance + amount;
}
public void deposit(double amount) {
balance = balance + amount;
}
}
BankAccount Example
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());
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;
}
}
}
:
OverLoadedTester.java
OverloadedBankAccount.java
Programming with
Multiple Classes
Class Diagram with Multiple Classes
Customer
BankAccount
account
- firstName : String
- lastName : String
- account : BankAccount
+
+
+
+
+
1
Customer(f:String, l:String)
getFirstName : String
getLastName : String
setAccount( acct:BankAccount)
getAccount( ) : BankAccount
Aggregation
- balance : double
Association
Name
+
+
+
+
BankAccount(initBalance:double)
getBalance : double
deposit(amt : double)
withdraw(amt : double)
Direction
indicator
Multiplicity
Class Diagram of “Customer.java” and “BankAccount.java”
Customer 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 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 Classes
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.");
}
}
Programming with Multiple Classes
Object Diagram of
“TestBanking.java” “Customer.java”
and “BankAccount.java”
TestBanking
main
cust
Customer
firstName=“John”
lastName=“Goodman”
account=null
getName
getAccount
setAccount
After Line 3
Programming with Multiple Classes
TestBanking
main
acct
cust
Executing
Line 4
Customer
firstName=“John”
lastName=“Goodman”
account=acct
account=null
acct
BankAccount
balance=3000.0
getName
deposit
getAccount
withdraw
setAccount
getBalance
Class Diagram
MyDate.java
Programming with Multiple Classes
TestBanking
main
cust
After Line 4
Customer
firstName=“John”
lastName=“Goodman”
account=acct
acct
BankAccount
balance=3000.0
getName
deposit
getAccount
withdraw
setAccount
getBalance
Programming with Multiple Classes
TestBanking
main
deposit(1250.25)
cust
Executing
Line 6
Customer
acct
firstName=“John”
BankAccount
lastName=“Goodman”
balance=4250.25
balance=3000.0
account=acct
1250.25
getName
deposit
getAccount
withdraw
setAccount
getBalance
Class Diagram
Programming with Multiple Classes
TestBanking
main
cust
Customer
firstName=“John”
lastName=“Goodman”
account=acct
After Line 6
acct
BankAccount
Class Diagram
balance=4250.25
getName
deposit
getAccount
withdraw
setAccount
getBalance
MyDate.java
Class Diagram with Multiple Classes
Customer
- firstName : String
- lastName : String
- account : BankAccount []
+
+
+
+
+
BankAccount
account
Customer(f:String, l:String)
getFirstName : String
getLastName : String
setAccount( acct:BankAccount)
getAccount( ) : BankAccount
“account” is
an array of BankAccount
- balance : double
*
+
+
+
+
BankAccount(initBalance:double)
getBalance : double
deposit(amt : double)
withdraw(amt : double)
Multiplicity
Class Diagram of “Customer.java” and “BankAccount.java”
Inheritance
Inheritance
การรับถ่ายทอด
คุณสมบัติ (Inheritance)
หมายถึง การที่
คลาสหนึ่ งๆ รับถ่าย
คุณสมบัติ (inherit) ทัง้ ค่า
คุณลักษณะ (Attribute)
และพฤติกรรม (Method)
มาจากอีกคลาสหนึ่ ง
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
Inheritance
BankAccount
- balance : double
+BankAccount(intibalance:double)
+getBalance( ) : double
+deposit(amount : double)
+withdraw(amount : double)
SavingsAccount
- interestRate : double
รับถำยทอด
่
คุณสมบัต ิ
(inherit)
+SavingsAccount(intibalance:doub
le, rate:double)
Class Diagram of “BankAccount” class and “SavingsAccount” class
Subclassing
พิจารณาคลาส Employee
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
public class Employee {
public String name;
public double salary;
public MyDate birthDate;
+ getDetails( ) : String
public String getDetails( ) { … }
}
เราสามารถสร้างวัตถุที่มีคลาสเป็ น
พนักงาน ได้โดยการกาหนดคลาส “Employee”
ถ้าเราต้ องการสร้างคลาสของ “Manager” ที่
Subclassing
พิจารณาคลาส Manager
public class Manager {
Manager
+
+
+
+
public String name;
public double salary;
public MyDate birthDate;
public String department;
name : String
salary : double
birthDate : MyDate
department : String
public String getDetails( ) { … }
+ getDetails( ) : String
}
โครงสร้างคลาส “Manager” กับ “Employee”
ซา้ ซ้อนกัน
เราใช้ วิธีการเชิงวัตถุสร้างคลาสใหม่ จาก
Subclassing
UML Diagram
Employee
Superclass
(Parent Class)
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Manager
Subclass
(Child Class)
+ department : String
การสร้างคลาสใหม่จากคลาสเดิมที่มีอยู่
แล้ว สนับสนุนแนวคิด ของการนา Source
Inheritance
การรับถ่ายทอดคุณสมบัติ
(Inheritance)
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
หมายถึง การที่คลาส
หนึ่ งๆ รับถ่ายคุณสมบัติ
(inherit) ทัง้ ค่าคุณลักษณะ
(Attribute) และพฤติกรรม
(Method) มาจากอีกคลาส
► คือการที่ class ที่ต่างกันมี Attributes และ Methods ที่เหมือนกัน
หนึ่ ง
+ getDetails( ) : String
Manager
+ department : String
Single Inheritance
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
Single Inheritance
หมายถึง การที่คลาส
หนึ่ งๆ รับถ่ายทอด
คุณสมบัติ (inherit) ทัง้ Attribute
และ Method มาจากอีกคลาส
หนึ่ ง
เราเรียกว่าคลาส
“Manager” รับถ่ายทอด
Multiple Inheritance
Multiple Inheritance
หมายถึง การที่คลาสหนึ่ งๆ รับถ่ายทอด
คุณสมบัติ (inherit) ทัง้ ค่าคุณลักษณะ และ
ภาษาจาวา
พฤติกรรมมาจากคลาสมากกว่า 1 คลาส
ไม่สนับสนุน
การทา Multiple
Employee
People
+ Name : String
+ id : String
+ birthDate : MyDate
+ company : String
+ salary : double
+ startDate : MyDate
+ getDetails( ) : String
+ getDetails( ) : String
Inheritance
Manager
+ department : String
แต่สามารถรับ
สืบทอด
Java Inheritance
UML Diagram
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
จาวา
public class Employee {
public String name;
public double salary;
public MyDate birthDate;
+ getDetails( ) : String
public String getDetails( ) { … }
}
Manager
+ department : String
public class Manager
extends Employee {
public String department;
}
Defining Extending Class in Java
การกาหนดคลาสทีร่ ับคุณสมบัติสืบทอดในภาษาจาวา สามารถทาได้
ดังนี้
<modifier> class child-class
extends parent-class {
:
}
ตัวอย่ าง
public class Manager extends Employee {
public String department;
}
“super” and “this” keyword
“this” keyword
ใช้อ้างอิงถึงคลาสปัจจุบนั
สามารถใช้อ้างอิงถึง Attribute หรือ Method
ของคลาสปัจจุบนั ได้โดยใส่ . แล้วตามด้วย
ชื่อ Attribute หรือ ชื่อ Method
“super” keyword
ใช้อ้างอิงถึง superclass ของคลาสปัจจุบนั
super( ) อ้างอิงถึง constructor ของ superclass
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)
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 Methods
สามารถ
เปลี่ยนแปลงพฤติกรรม ที่
รับถ่ายทอด (inherit) มาจาก
superclass/parent class ได้
subclass สามารถกาหนด
Method ที่ มีหน้ าที่ /
พฤติกรรม ต่ำงจำก Method
ของ superclass ได้แต่ต้องมี
subclass/child class
Employee
+ name : String
+ salary : double
+ birthDay : MyDate
+ getDetails( ) : String
Manager
- department : String
+ getDetails( ) : String
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 (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 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 Methods
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;
}
}
TestCompany
public 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”
Employee & Manger Example
TestCompany
main
jeff
rob
Employee
name=“Jeffrey”
Salary=1000.0
birthday=jeffdate
getDetails
Manager
jeffdate
name=“Robert”
robdate
MyDate
Salary=5000.0
MyDate
day=2;
birthday=Robdate
day=3;
month=3;
month=4;
department=“Sales”
year=1970;
setDate
toString
getDetails
year=1965;
setDate
toString
Access Control
การควบคุมการเข้าถึงข้อมูล (Access Control)
คือ วิธีการเชิงวัตถุ ที่ภาษาจาวามีไว้เพื่อ
ใช้เขียนโปรแกรมสนับสนุนคุณสมบัติ
Encapsulation และ Information Hiding
การระบุค่า Access Control Specifier เป็ นการ
ควบคุมการเข้าถึงข้อมูล โดยจะใช้ระบุก่อน
หน้ า ตัวแปร ค่ำคงที ่ เมทธอด และคลำส
ค่า Access Control Specifier ได้แก่
An Example of Access Control
public class BankAccount {
private double balance;
ประกำศกำรเขำถึ
้ งขอมู
้ ล
ของคอนสตรัคเตอรของ
์
ประกำศกำรเขำถึ
ง
ข
อมู
ล
้
้
public BankAccount(double initAmount)
{
คลำสเป็
น public เพือ
่ ให้
ของตัวแปรภำยในคลำส balance = initAmount;
ผู้ขอใช้บริกำรจำกวัตถุท
เป็ น private เพือ
่ ให้สำมำรถ}
ถูกสรำงขึ
น
้ จำกวัตถุนี้
้
ขำถึ
ง
ได
จำกภำยในคลำส
้
้
public void deposit(double amount) {
สำมำรถสรำงวั
้ ตถุนี้ได้
balance = balance + amount;
}
ประกำศกำรเขำถึ
ง
ข
อมู
ล
้
้
Note : กรณีทป
ี่ ระกำศกำรเขำถึ
ของเมทธอดเป็ น public public void withdrawn(double
้ ง
amount) {
ขอมู
ลของคลำสเป็ น public คลำสนั้น
เพือ
่ ให้เมทธอดของคลำส balance = balance
้ - amount;
}
จะตองถู
กบันทึกในไฟลที
่ อ
ื ชือ
่ เดียวกับ
สำมำรถให้บริกำรแกผู
้
์ ม
่ ้ขอ
คลำส “BankAccout.java” แยกจำก
ช้บริกำรนอกขอบเขตของpublic int getBalance()
{
ู ำงวั
คลำส
return balance;ไฟลที
้ ตถุจำกคลำส หรือ
์ ร่ ะบุผ้สร
}
ขอรับบริกำรจำกคลำสนี้
}
Inheritance & Access Control
พิจารณา
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Manager
- department : String
การระบุค่าการ
เข้าถึงข้อมูล (Access
Control) ให้เป็ น public ทา
ให้คลาสอื่นๆ ทุก
คลาส สามารถเข้าถึง
Attributes ของคลาส
Employee ได้
Inheritance & Access Control
พิจารณา
Employee
# name : S tring
# salary : double
# birthD ate : M y D ate
- getD etails( ) : S tring
protected
M anager
- department : S tring
สามารถกาหนด
เข้าถึงข้อมูล (Access
Control) ให้สามารถ
เข้าถึงได้ผา่ นทางการ
สืบทอดคุณสมบัติ
เท่านัน้
ทาได้โดยระบุ
Employee & Manager Example
public class Employee {
protected String name;
protected double salary;
public String getDetails( ) {
return ("name:"+name+ ”, salary:"+salary);
}
}
public class Manager extends Employee {
private String department;
public String getDetails( ) {
return super.getDetails( ) + ", departmentt:"+department);
}
}
“Point” Class
public 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” Class
public 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” Class
public 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” Class
public 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());
}
Point
# x : int
# y : int
+
+
+
+
+
+
Point()
Point(a:int, b:int)
setPoint(a : int, b : int)
getX() : int
getY() : int
toString() : String
Inheritance
Relationships
of
Point, Circle and
Cylinder
Circle
# 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
Object Diagram of
TestInheritance
TestInheritance
main
c
Cylinder
x = 12
radius = 5.7
y = 23
height = 2.5
getX
setRadius
getY
setHeight
getRadius
setPoint
getHieght
area
toString
Specialization
รับถ่ายทอดคุณสมบัติ (inherit)
ทัง้ Attributes และ Methods มาจาก Superclass/Parent Class
Subclass/Child Class
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
Empl
oye
e
Mana
ger
Generalization
เป็ น flip-side ของ Specialization
การที่ class ที่ ต่างกันมี Attributes และ Methods ที่
เหมือนกัน
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
Empl
oye
e IS A
Mana
ger
Class Hierarchies
Point
Circle
Cylinder
Square
Sphere
Box
ลูกศรใน Class Hierararchies แสดง
ความสัมพันธ์ของการรับถ่ายทอด
คุณสมบัติ
Cylinder extends Circle
Cubic
IS-A Relationships
การถ่ายทอดคุณสมบัติเป็ นคุณลักษณะ
แบบ transitive
Snake IS-A Reptile และ Reptile IS-A Animal
Animal
นัน
่ คือIS-ASnake IS-A Animal
IS-A
Snake
Reptile
Bird
Lizard
Eagle
Mammal
Dog
Bat
Adopting a Design
การออกแบบ Class Hierarchies ที่เหมาะสม
เป็ นสิ่งสาคัญการการออกแบบระบบเชิง
วัตถุ
Animal
Flying Creature
Alternatives Hierarchies ขึน
้ กับระบบที
่ต้องการ
หรื
ออกแบบ Mammal
Bird
อ Mosquito Bird
Eagle
Dog
Bat
Eagle
Bat
The “Object”Class
ในภาษาจาวา
คลาส “Object” เป็ นคลาสบนสุดของ คลาส
ทัง้ หมดที่ประกอบกันขึน้ เป็ น Class Hierarchies
คลาสที่ ประกาศ โดยไม่ใช้คาเฉพาะ
ิ
“extends” ระบุการสืบpublic
ทอดคุ
ณ
สมบั
ต
จ
าก
class
Employee
extends
Object {
public class Employee { เหมือ
...
...
คลาสใดๆ ถืนกั
อว่บาเป็} นการละ “extends Object”
}
ไว้
Methods defined in
“Object”Class
Methods ที่ ถก
ู กาหนดไว้ในคลาส “Object” ให้
คลาสที่รบั สืบทอดคุณสมบัติสามารถ
เรียกใช้ หรือทาการ 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.
Two Approaches to reuse
Software
Inheritance
IS-A relationship
Employee
IS-A
Manager
Composition
HAS-A relationship
Customer
BankAccount
HASA
Benefits of Inheritance
Avoiding redundancies
Source Code ในส่ วนทีเ่ หมือนกันจะถูกเขียนขึน
้ เพียงครั้งเดียว
Code reuse & Code Sharing
สามารถนาไปใช้ ใหม่ ได้ (reuse) ได้ โดยการรับถ่ ายทอด
คุณสมบัติ
สามารถสร้ างคลาสใหม่ จากคลาสเดิมทีม
่ ีอยู่แล้วได้
Reduced code size
Source Code ถูกเขียนขึน
้ เพียงครั้งเดียว และถูกนาไปใช้ ใหม่
ได้ ไม่ จากัด
Costs of Inheritance
Message-passing overhead
เนื่องจากอนุญาตให้ มี Code Reuse ทาให้ ต้องมีการส่ งต่ อความ
ต้ องการไปยังคลาสอืน่ ๆ ที่สัมพันธ์ กนั ด้ วยการถ่ ายทอดคุณสมบัติ
Execution Speed
ความเร็วในการประมวลผลช้ าลง เนื่องจากต้ องส่ งต่ อความ
ต้ องการไปขอรับบริการจากคลาสอืน่ ๆ
Program Complexity
เพิม
่ ความซับซ้ อนของโปรแกรม
Summary
•
•
•
•
•
Overloading Methods and Constructors
Programming with multiple classes
Inheritance
Overriding Methods and Constructor
Access control and inheritance