1. บทที่ 7 วิธีการเชิงวัตถุและการออกแบบคลาส
Srinagarindra The Princess Mother
School Kanchanaburi
1
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
2. (Object Oriented) Object
ความสัมพันธและปฏิสัมพันธระหวางวัตถุ
2
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
3. ตารางเปรียบเทียบการวิเคราะหและออกแบบระบบวิธีเดิมกับวิธีเชิงวัตถุ
วิธีเดิม วิธีเชิงวัตถุ
เริ่มตนจากการวิเคราะหเอกสาร เริ่มตนการวิเคราะหจาก Objects ที่
ผลลัพธ และการทํางานของ สามารถเห็นไดชัดเจน
ระบบงานเดิม
แตกการทํางานออกเปนหนวย แบงกลุมของ Object ตาม
ยอยๆ คุณลักษณะ
องคประกอบตางๆของระบบ เชน แตละ Object เปนอิสระตอกัน การ
การประมวลผล การออกรายงาน เปลี่ยนแปลงจะไมกระทบกัน
การคํานวณ จะเกียวพันกัน การ
่
เปลี่ยนแปลงจะกระทบซึ่งกันและ
กัน
3
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
4. ตารางเปรียบเทียบการวิเคราะหและออกแบบระบบวิธีเดิมกับวิธีเชิงวัตถุ
วิธีเดิม วิธีเชิงวัตถุ
การปรับเปลี่ยนระบบตองแกไข การปรับเปลี่ยนระบบ ทําไดโดย
Source Code การเปลี่ยน Attributes, Functions ของ
Object
Tools ที่สนับสนุนมีนอยลง Tools ที่สนับสนุนมีมากขึ้น
4
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
5. (Object)
การสื่อสารระหวาง Object เรียกวา Message
5
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
6. Class Object
Object Class
Class Object
เดียวกันหรือไม ในความเปนจริง Class ถือวาเปน นามธรรม (Abstract)
ในขณะที่ Object นั้นเปนสิ่งที่มีตัวตน (Concrete) กลาวคือ Class เปน
เหมือนพิมพเขียวของ Object โดยที่ Class จะไมสามารถทํางานได แต
Object สามารถทํางานได
การทํางานของ Object จะเปนไปตามคุณสมบัติที่กําหนดไวใน Class
และ Object ทุกตัวก็ตองอยูใน Class ดังนัน Class และ Object จึงเปนสิง
้ ่
คูกนเสมอ
ั
6
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
7. Student
Class Class
StudentID
Attributes)
FirstName
(Operations Methods) LastName
Address
Telephone
Name Birthdate
Faculty
Attributes GPA
Register()
Methods Drop()
Withdraw()
ตัวอยาง Class Student
7
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
8. (Inheritance) Subclass
(Attributes) Superclass
Multiple Inheritance
สัญญลักษณทใช คือ ลูกศรหัวรูปสามเหลี่ยมใส ชี้จาก Subclass ไปยัง
่ี
Superclass
8
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
9. Transaction
TransactionNumber
EmployeeNumber
TransactionDate
TransactionTime
BarcodeNumber
Price
SalesTax
Sales Transaction Rental Transaction
----inherited---- ----inherited----
TransactionNumber TransactionNumber
EmployeeNumber EmployeeNumber
Without Inheritance TransactionDate
TransactionTime
TransactionDate
TransactionTime
BarcodeNumber BarcodeNumber
Price Price
SalesTax SalesTax
----local---- ----local----
QuantitySold MemberNumber
With Inheritance
9
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
10. Object
ผูออกแบบระบบงานเชิงวัตถุสามารถออกแบบระบบงานขนาดใหญได
โดยการอาศัย Object ที่มีการนิยามไวกอนหรือที่มีผูอื่นทําการออกแบบ
ไวกอนแลว ซึ่งเปนที่มาของการนํากลับมาใชใหม (Reusability)
10
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
11.
1.
2.
3. ลดคาใชจายในการพัฒนาระบบ
11
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
12. Polymorphism Object
Function/ Message
Class Class
จาก Class รูปหลายเหลียม โดยทังคูมี Function ทีชอ draw() เหมือนกัน
่ ้ ่ ่ื
แตเมื่อมีการเรียกใช function ดังกลาว Object ทีสรางจาก Class รูป
่
สี่เหลี่ยมจะมีการวาดรูปสี่เหลี่ยม ขณะที่ Object ที่สรางจาก Class รูป
สามเหลี่ยมจะมีการวาดรูปสามเหลี่ยม
12
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
13. • Polymorphism
code
รูปหลายเหลียม
่
draw()
ชุดคําสังทัวไป และมอบหนาที่
่ ่
รายละเอียดของการนําไปใชแก รูปสีเหลียม
่ ่ รูปสามเหลียม
่
Object ทีเ่ กียวของจัดการ
่ draw() draw()
13
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
15. Object
Public (+) ซึ่งสามารถเขาถึงไดโดยตรงจากภายนอก
Private (#) ซึงจะถูกใชงานจากภายใน Class เทานัน
่ ้
Protected (-) ซึงจะสามารถเห็นหรือเขาถึงไดจากภายใน Subclass เทานัน
่ ้
15
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
16. Object Class
16
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
17. Object Class “Whole-Part”
“is part of” Class Object Class
17
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
18. Object
Class Text
Class ซึงเปนองคประกอบของ
่ Windows Button
Class อืนทีใหญกวา
่ ่
เมือ Class ทีใหญกวาถูกทําลาย
่ ่ Menu
Class ทีเ่ ปนองคประกอบก็จะถูก
ทําลายไปดวย
18
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
19. Person
Lastname
Firstname
Object Class
Birthdate
Gender
Walk()
Jump()
Talk()
Class หนึ่ง (Superclass) ไปยังอีก
Sleep()
Eat()
Class หนึ่ง (Subclass)
Student Teacher
Department Position
Year Expertise
GPA
Lecture()
Enroll() Comment()
Study()
Exam()
Graduate()
19
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
20. บทที่ 12 การเขียนโปรแกรมโดยใช NetBeans
Srinagarindra The Princess Mother
School Kanchanaburi
20
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
21. NetBeans IDE Freeware Editor
NetBeans
Java, php, C/C++
สามารถ download PlugIn ตางๆมาใชงานรวมกับตัว NetBeans เองดวย
ยกตัวอยางเชน iReport เปน Plugin ทีใชในการสรางreport หรือ Visual
่
Web Page Layouts ใชในการแสดงตัวอยางหนา web page
21
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
22. NetBeans download NetBeans
http://netbeans.org/downloads/index.html NetBeans version Platform
Platform version
download .exe file
ยัง directory ที่ตองการจะทําการติดตั้งไดเลย หลังจากติดตั้งโปรแกรมเสร็จสมบูรณแลว เราสา
มามารถเขาโปรแกรม NetBeans ไดจากหนา desktop
22
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
23. download .exe file
directory
โปรแกรม NetBeans ไดจากหนา desktop
23
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
24. JAVA
download JDK Java Development Kit
download
http://www.oracle.com/technetwork/java/javase/downloads/
index.html
เมือ download JDK แลวก็ตองทําการ Set class path เพือใหสามารถ Compile
่ ่
และ Run Java ได โดยวิธทาสามารถติดตามไดจาก Link ดานลางเลยครับ
ีํ
http://archive.oracle.in.th/2008/02/set-path-class-path-j2se-
dos.htmlหรือ
http://www.dominixz.com/blog/software-tips/set-java-to-
compile-and-run-all-places-in-dos/
24
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
25. NetBeans
ออกเปน 4 สวนใหญๆดังนีครับ
้
25
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
26. Tool
Project
Browse Project File
เปนสวนที่ใชในการเขียน Code
เปนสวนที่ใชในการแสดงผลการทํางานตาง เมื่อทําการ Run โปรแกรม
และยังมีสวนของการ debug โปรแกรมดวย
26
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
27. GUI NetBeans
New Project
File >> New Project
27
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
28. Java Java
Application Next
28
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
29. Project Project
Name Project NetBeansTutorial Main Class MainClass
Finish
Project NetBeansTutorial File MainClass.java
ตอไปเราจะลองสราง class TestObj ขึ้นมา เพื่อลองใช Funtion บางอยางของNetBeans ครับ
NetBeans
โดยการสราง class ใหมนั้น เราสามารถทําไดโดย คลิกขวาที่ชื่อ Project แลวเลือกNew >>
Java Class ดังภาพ
29
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
30. Class TestObj Finish
class TestObj code
public class TestObj {
private String firstName;
private String lastName;
public void showName() {
System.out.println("THIS IS METHOD showName()");
System.out.println("My name is "+firstName+" "+lastName);
}
}
30
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
31. Attribute Class TestObj private
Getter and Setter NetBeans Getter and
Setter Attribute code
เลือก Insert Code
Codeจะปรากฏหนาตางขึนมาใหเลือกที่ Getter and Setter
้
หนาตางสําหรับการ Generate Getters and Setters จะขึนมา ใหเราทําการ
้
เลือกวาจะสราง Getters and Setters ใหกบ Attribute ใดบาง เมื่อเลือกเสร็จ
ั
เรียบรอยใหกดGenerate
31
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
32. code class TestObj public void setFirstName(String
public class TestObj { firstName) {
private String firstName; this.firstName = firstName;
private String lastName; }
public void showName() { public String getLastName() {
System.out.println("THIS IS METHOD showName()"); return lastName;
System.out.println("My name is "+firstName+" "+lastName); }
}
public void setLastName(String
public String getFirstName() { lastName) {
return firstName; this.lastName = lastName;
} }
}
32
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
33. Generate Getters and Setters Function NetBeans
class TestObj code MainClass
public class MainClass {
public static void main(String[] args) {
TestObj temp = new TestObj();
temp.setFirstName("Itthi");
temp.setLastName("Kruenarongkul");
temp.showName();
temp.setFirstName("Monthinee");
temp.setLastName("Buntawee");
temp.showName();
}
33
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
34. code CTRL+SpaceBar
method Attribute NetBeans
Code Run
เขียวที่อยูดานลางแถบเมนู
34
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/
35. NetBeans Save Run NetBeans
compile error
35
Created with Print2PDF. To remove this line, buy a license at: http://www.software602.com/