1. Computer Programming
Chapter 7 : Objects and Classes
Atit Patumvan
Faculty of Management and Information Sciences
Naresuna University
Tuesday, August 23, 11
2. 2
Structural Programming
01: void main(){
02: printf(“%d”, module(4, -2));
03: }
:
Input Values 15: int module(int n1, int n2){
Procedure 16: int sum;
17: sum = n1 + n2;
18: return sum;
19: }
Input Values
Main Block
Function
Return Values
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
3. 3
Data Abstraction
x : double z : double a x : double
y : int
z : double
y : int
myData
b x : double
y : int
z : double
x : double z : double myData
y : int
myData
c x : double
y : int
z : double
myData
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
4. 4
Object-Oriented Programming
Object2
Object1 Method
Method
Main Program
Data
Method
Method Method
Method
Sub-program1
Data
Method
Sub-program2 Method
Procedure Programming Object-Oriented Programming
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
5. 5
แนวคิดพื้นฐานของการเขียนโปรแกรมเชิงวัตถุ
• ทุกสิ่งทุกอย่างคือวัตถุ (object)
• ไม่ได้จํากัดขอบเขตอยู่ที่สิ่งของที่จับต้องได้ หรือมองเห็น
• เป็นกรอบคิด (conceptualize) เพื่อใช้อธิบายสิ่งที่กําลัง
พิจารณา
• วัตถุมีคุณลักษณะ (property) และพฤติกรรม (method)
• กรอบคิดที่แตกต่างย่อมมีคุณลักษณะและพฤติกรรมไม่เหมือน
กัน
• เป็นการทํางานระหว่างวัตถุ โดยผ่านการส่งสารถึงกัน
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
7. 7
สมาชิกของคลาส
• คลาส (class) คือนิยามของวัตถุใดวัตถุหนึ่งหรือกลุ่มของวัตถุ ที่มี
คุณลักษณะและพฤติกรรมเหมือนกัน
• ประกอบด้วย คุณลักษณะ (properties) และ พฤติกรรม (behavior/
method)
• สามารถอธิบายด้วยแผนภาพ (Class Diagram)
+ BankAccount Class Name
+ number : long
- balance : double Properties
+ holder : customer
+ BankAccount(Customer)
+ deposit(double) : void
+ withdraw(double) : void
Methods
+ toString() : String
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
8. 8
แปลง Class Diagram ให้เป็น Java Source
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
9. 9
แปลง Class Diagram ให้เป็น Java Source
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
10. 10
การสร้างวัตถุ
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
11. 11
การสร้างวัตถุ
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
12. 12
การสร้างวัตถุ
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
13. 13
Demo
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
14. 14
โครงสร้างของคลาส
01: public class BankAccount { Class Declaration
02:
03: public static long number;
04: private double balance;
05: public Customer holder; Variables
06:
07: public BankAccount(Customer holder) {
08: number++;
09: this.holder = holder;
10: balance = 0; Constructor
11: }
12:
13: public void deposit(double amount) {
14: balance += amount;
15: }
16:
17: public void withdraw(double amount) {
18: if (balance >= amount) { Methods
19: balance -= amount;
20: } else {
21: System.out.println("System cannot process this transaction."); + BankAccount
22: }
23: }
24:
+ number : long
25: @Override - balance : double
26: public String toString() { + holder : customer
27: String info = "";
28: info += "Account Number: " + number + "n"; + BankAccount(Customer)
29: info += "Holder Name: " + holder.getName() + "n";
30: info += "Current Balance: " + balance; + deposit(double) : void
31: return info; + withdraw(double) : void
32: } + toString() : String
33: }
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
15. 15
Component of Class Declaration
Component Purpose
public Class is publicly accessible
abstract Class cannot be instantiated
final Class cannot be subclassed
class Name_of_Class Name of the class
extends Superclass Superclass of the class
implements Interfaces Interface implemented by the class
{
Class_Body Body of the class
}
public abstract final class Name_of_Class extends Superclass
implements Interfaces {
:
Class_Body
:
}
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
16. 16
Declaring Member Variables
Component Purpose
Indicates the access level for this member (public, protected, default,
accessLevel
private)
static Declares a class member
final Indicates that it is a constant
transient This variable is transient
volatile This variable is volatile
type Type of the variable
VariableName Name of the variable
accessLevel static final transient volatile type VariableName;
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
17. 17
การเข้าถึงสมาชิกของคลาส
01: public class Tester { customer @268BE1AH
02:
03: public static void main(String[] args) { Customer
04: BankAccount account;
05: Customer customer; Customer@268BE1AH
06:
07: value “Bob Goodman”
08: customer = new Customer();
String
09:
10: customer.setName("Bob Goodman"); value “2567893”
11: customer.setSsn("2567893"); String
12:
13: account = new BankAccount(customer); BankAccount@268BE1AH
14:
15: account.deposit(1000.00);
number 1
16:
17: System.out.println(account); long
18: balance 1000.00
19: }
20: } double
account @AB1F245H
holder @268BE1AH
BankAccount Customer
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
18. 18
การเข้าถึงสมาชิกของคลาส
01: public class BankAccount {
02: customer @268BE1AH account @AB1F245H
03: public static long number;
04: private double balance; Customer BankAccount
05: public Customer holder;
06:
07: public BankAccount(Customer holder) {
Customer@268BE1AH
08: number++;
09: this.holder = holder;
10: balance = 0; value “Bob Goodman”
11: }
12: String
13: public void deposit(double amount) {
14: balance += amount; value “2567893”
15: }
16: String
17: public void withdraw(double amount) {
18: if (balance >= amount) { BankAccount@268BE1AH
19: balance -= amount;
20: } else {
21: System.out.println("System cannot process this
number 1
transaction.");
22: }
long
23: }
24:
balance 1000.00
25: @Override
26: public String toString() {
double
27: String info = "";
28: info += "Account Number: " + number + "n";
29: info += "Holder Name: " + holder.getName() + "n"; holder @268BE1AH
30: info += "Current Balance: " + balance;
31: return info; Customer
32: }
33: }
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
19. 19
Method
• คือฟังก์ชันที่ถูกประกาศไว้ภายในคลาส
• สามารถเข้าถึงสมาชิกของคลาสตัวอื่นๆได้โดยตรง
• เมธอสจะถูกเรียกใช้ได้ก็ต่อเมื่อคลาสถึกนํามาสร้างเป็นวัตถุ
• ในระหว่าการทํางาน การเปลี่ยนแปลงค่าของสมาชิกที่เป็น
properties จะถูกเปลี่ยนแปลงในระดับวัตถุ
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
20. 20
Defining Methods
Component Purpose
Indicates the access level for this method (public, protected, default,
accessLevel
private)
static Declares a class method
abstract This method is not implemented
final Method cannot be over written
native Method implemented in other language.
synchronized Method requires a method to run.
returnType The return type
methodName The method name
(parameterList) The list of arguments.
throws exceptions The exceptions thrown by this method.
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
21. 21
Naming a Method
• Method name can be any legal identifier and code
conventions restrict method names.
• Method name should be verbs.
• Method name should be mixed case, with first letter in
lowercase and the first letter each internal word in
uppercase.
• Method name should not be the same as the name.
• Method name has a unique name within its class.
• excepted to overriding method, hiding method and
overloading method
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
22. 22
Method Declaration
Type of return value Type of parameter
name of method name of parameter
variable
public static double cubeVolume(double sideLength){
double volume = sideLength*sideLength*sideLength;
return volume;
}
method body
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
23. 23
Method Calls
• Method can directly invoke any other method of same class
public class Cube {
public static double cubeVolume(double sideLength){
double volume = sideLength*sideLength*sideLength;
return volume;
}
invoke /call
return public static void main(String [ ] args ) {
double length =10.0;
double result;
result = cubeVolume(length);
System.out.println(result);
}
}
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
24. 24
Parameter Passing Mechanism
• Passing by Value, copies the value of the underlying
programming element into a local variable in the method
• Passing by Reference, gives the method a direct
reference to the underlying programming element in the
calling code
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11