computer programming chapter 7: object and class

24
Computer Programming Chapter 7 : Objects and Classes Atit Patumvan Faculty of Management and Information Sciences Naresuna University Tuesday, August 23, 11

Upload: atit-patumvan

Post on 29-May-2015

887 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Computer Programming Chapter 7: Object and Class

Computer ProgrammingChapter 7 : Objects and Classes

Atit PatumvanFaculty of Management and Information Sciences

Naresuna University

Tuesday, August 23, 11

Page 2: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

Structural Programming

2

Main Block

Procedure

Function

Input Values

Input Values

Return Values

01: void main(){02: printf(“%d”, module(4, -2));03: } :15: int module(int n1, int n2){16: int sum;17: sum = n1 + n2;18: return sum;19: }

Tuesday, August 23, 11

Page 3: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

Data Abstraction

3

x : doubley : int

z : double

x : doubley : int

z : double

myData

x : doubley : int

z : double

myData

a

x : doubley : int

z : double

myData

b

x : doubley : int

z : double

myData

c

Tuesday, August 23, 11

Page 4: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

Object-Oriented Programming

4

Main Program

Sub-program1

Sub-program2

Procedure Programming

Data

Method

Method

Method

Method

Data

Method

Method

Method

Method

Object-Oriented Programming

Object1

Object2

Tuesday, August 23, 11

Page 5: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

แนวคิดพื้นฐานของการเขียนโปรแกรมเชิงวัตถุ

• ทุกสิ่งทุกอย่างคือวัตถุ (object)

• ไม่ได้จํากัดขอบเขตอยู่ที่สิ่งของที่จับต้องได ้หรือมองเห็น

• เป็นกรอบคิด (conceptualize) เพื่อใช้อธิบายสิ่งที่กําลังพิจารณา

• วัตถุมีคุณลักษณะ (property) และพฤติกรรม (method)

• กรอบคิดที่แตกต่างย่อมมีคุณลักษณะและพฤติกรรมไม่เหมือนกัน

• เป็นการทํางานระหว่างวัตถุ โดยผ่านการส่งสารถึงกัน

5

Tuesday, August 23, 11

Page 6: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

การสร้างกรอบคิด

6

• สร้างคําอธิบายกิจกรรมต่างๆ ที่เกิดขึ้นในกรอบที่สนใจ (domain)

• หาสิ่งต่างๆ (objects) ที่มีปฏิสัมพันธ์ระหว่างกัน

• จับต้องได ้(tangible) เช่น ลูกค้า สินค้า

• จับต้องไม่ได ้(intangible) เช่น การขาย ธุรกิจ

• หาความเชื่อมโยงระหว่างสิ่งต่างๆ พิจารณาจากการส่งสารเพื่อ

• ร้องขอให้ปฏิบัต ิเช่น ธุรกิจขายสินค้าให้กับลูกค้า

• ร้องขอข้อมูลหรือสารสนเทศ เช่น ลูกค้าสอบถามราคาสินค้า

• ในการประมวลสาร ต้องการข้อมูลหรือสารสนเทศใดบ้าง

Tuesday, August 23, 11

Page 7: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

สมาชิกของคลาส

• คลาส (class) คือนิยามของวัตถุใดวัตถุหนึ่งหรือกลุ่มของวัตถุ ที่มีคุณลักษณะและพฤติกรรมเหมือนกัน

• ประกอบด้วย คุณลักษณะ (properties) และ พฤติกรรม (behavior/method)

• สามารถอธิบายด้วยแผนภาพ (Class Diagram)

7

+ BankAccount

+ number : long- balance : double+ holder : customer

+ BankAccount(Customer)+ deposit(double) : void+ withdraw(double) : void+ toString() : String

Class Name

Properties

Methods

Tuesday, August 23, 11

Page 8: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

แปลง Class Diagram ให้เป็น Java Source

8

Tuesday, August 23, 11

Page 9: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

แปลง Class Diagram ให้เป็น Java Source

9

Tuesday, August 23, 11

Page 10: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

การสร้างวัตถุ

10

Tuesday, August 23, 11

Page 11: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

การสร้างวัตถุ

11

Tuesday, August 23, 11

Page 12: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

การสร้างวัตถุ

12

Tuesday, August 23, 11

Page 13: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

Demo

13

Tuesday, August 23, 11

Page 14: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

01: public class BankAccount {02: 03: public static long number;04: private double balance;05: public Customer holder;06: 07: public BankAccount(Customer holder) {08: number++;09: this.holder = holder;10: balance = 0;11: }12: 13: public void deposit(double amount) {14: balance += amount;15: }16: 17: public void withdraw(double amount) {18: if (balance >= amount) {19: balance -= amount;20: } else {21: System.out.println("System cannot process this transaction.");22: }23: }24: 25: @Override26: public String toString() {27: String info = "";28: info += "Account Number: " + number + "\n";29: info += "Holder Name: " + holder.getName() + "\n";30: info += "Current Balance: " + balance;31: return info;32: }33: }

โครงสร้างของคลาส

14

+ BankAccount

+ number : long- balance : double+ holder : customer

+ BankAccount(Customer)+ deposit(double) : void+ withdraw(double) : void+ toString() : String

Class Declaration

Variables

Constructor

Methods

Tuesday, August 23, 11

Page 15: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

Component of Class Declaration

15

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 :}

Tuesday, August 23, 11

Page 16: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

Declaring Member Variables

16

Component Purpose accessLevel Indicates the access level for this member (public, protected, default,

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;

Tuesday, August 23, 11

Page 17: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

การเข้าถึงสมาชิกของคลาส

17

01: public class Tester {02: 03: public static void main(String[] args) {04: BankAccount account;05: Customer customer;06: 07: 08: customer = new Customer();09: 10: customer.setName("Bob Goodman");11: customer.setSsn("2567893");12: 13: account = new BankAccount(customer);14: 15: account.deposit(1000.00);16: 17: System.out.println(account);18: 19: }20: }

account @AB1F245HBankAccount

Customer@268BE1AH

customer @268BE1AHCustomer

value “Bob Goodman”String

value “2567893”String

BankAccount@268BE1AH

number 1long

balance 1000.00double

holder @268BE1AHCustomer

Tuesday, August 23, 11

Page 18: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

การเข้าถึงสมาชิกของคลาส

18

account @AB1F245HBankAccount

Customer@268BE1AH

customer @268BE1AHCustomer

value “Bob Goodman”String

value “2567893”String

BankAccount@268BE1AH

number 1long

balance 1000.00double

holder @268BE1AHCustomer

01: public class BankAccount {02: 03: public static long number;04: private double balance;05: public Customer holder;06: 07: public BankAccount(Customer holder) {08: number++;09: this.holder = holder;10: balance = 0;11: }12: 13: public void deposit(double amount) {14: balance += amount;15: }16: 17: public void withdraw(double amount) {18: if (balance >= amount) {19: balance -= amount;20: } else {21: System.out.println("System cannot process this transaction.");22: }23: }24: 25: @Override26: public String toString() {27: String info = "";28: info += "Account Number: " + number + "\n";29: info += "Holder Name: " + holder.getName() + "\n";30: info += "Current Balance: " + balance;31: return info;32: }33: }

Tuesday, August 23, 11

Page 19: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

Method

• คือฟังก์ชันที่ถูกประกาศไว้ภายในคลาส

• สามารถเข้าถึงสมาชิกของคลาสตัวอื่นๆได้โดยตรง

• เมธอสจะถูกเรียกใช้ได้ก็ต่อเมื่อคลาสถึกนํามาสร้างเป็นวัตถุ

• ในระหว่าการทํางาน การเปลี่ยนแปลงค่าของสมาชิกที่เป็น properties จะถูกเปลี่ยนแปลงในระดับวัตถุ

19

Tuesday, August 23, 11

Page 20: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

Defining Methods

20

Component Purpose accessLevel Indicates the access level for this method (public, protected, default,

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.

Tuesday, August 23, 11

Page 21: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

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

21

Tuesday, August 23, 11

Page 22: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

Method Declaration

public static double cubeVolume(double sideLength){ double volume = sideLength*sideLength*sideLength; return volume; }

Type of return value

name of method

Type of parameter

name of parameter variable

method body

22

Tuesday, August 23, 11

Page 23: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

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; }

public static void main(String [ ] args ) { double length =10.0; double result; result = cubeVolume(length); System.out.println(result); }}

invoke /callreturn

23

Tuesday, August 23, 11

Page 24: Computer Programming Chapter 7: Object and Class

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

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

24

Tuesday, August 23, 11