SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Downloaden Sie, um offline zu lesen
Computer Programming
                                                         Chapter 7 : Objects and Classes




                                           Atit Patumvan
                           Faculty of Management and Information Sciences
                                         Naresuna University




Tuesday, August 23, 11
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



                                                                            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



                                                   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



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


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

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

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

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

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

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


Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
6



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


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

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

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

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

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

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

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

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

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
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



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




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
9



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




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
10



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




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
11



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




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
12



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




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
13



                                                                                     Demo




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
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



                                               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



                                                       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



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


     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



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

   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



                                                                                     Method


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

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

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

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




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Tuesday, August 23, 11
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



                                                                          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



                                                                       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



                                                                                     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



                                                  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

Weitere ähnliche Inhalte

Mehr von Atit Patumvan

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agricultureAtit Patumvan
 
An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)Atit Patumvan
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตAtit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics toolsAtit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics toolsAtit Patumvan
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556Atit Patumvan
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationAtit Patumvan
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6Atit Patumvan
 
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsComputer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsAtit Patumvan
 
Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Atit Patumvan
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2Atit Patumvan
 
Computer Programming: Chapter 1
Computer Programming: Chapter 1Computer Programming: Chapter 1
Computer Programming: Chapter 1Atit Patumvan
 

Mehr von Atit Patumvan (20)

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agriculture
 
An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computation
 
Media literacy
Media literacyMedia literacy
Media literacy
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6
 
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsComputer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : Methods
 
Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2
 
Computer Programming: Chapter 1
Computer Programming: Chapter 1Computer Programming: Chapter 1
Computer Programming: Chapter 1
 

Computer Programming Chapter 7: Object and Class

  • 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
  • 6. 6 การสร้างกรอบคิด • สร้างคําอธิบายกิจกรรมต่างๆ ที่เกิดขึ้นในกรอบที่สนใจ (domain) • หาสิ่งต่างๆ (objects) ที่มีปฏิสัมพันธ์ระหว่างกัน • จับต้องได้ (tangible) เช่น ลูกค้า สินค้า • จับต้องไม่ได้ (intangible) เช่น การขาย ธุรกิจ • หาความเชื่อมโยงระหว่างสิ่งต่างๆ พิจารณาจากการส่งสารเพื่อ • ร้องขอให้ปฏิบัติ เช่น ธุรกิจขายสินค้าให้กับลูกค้า • ร้องขอข้อมูลหรือสารสนเทศ เช่น ลูกค้าสอบถามราคาสินค้า • ในการประมวลสาร ต้องการข้อมูลหรือสารสนเทศใดบ้าง 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