Encapsulation & access modifiers

Java เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

Sani Yusuf

Lead Software Engineering Content Developer

ทำความเข้าใจ Encapsulation

  • Encapsulation อธิบายได้ผ่านอุปกรณ์มือถือ
  • เข้าถึงได้เฉพาะส่วนที่เปิดเผย เช่น หน้าจอ ลำโพง เป็นต้น
  • การทำงานภายในถูกซ่อนจากผู้ใช้ทั้งหมด

โทรศัพท์มือถือ

Java เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

Public properties

  • Properties ที่กำหนดเป็น public สามารถเข้าถึงได้จาก object instance

  class Car {
    // Public property
    public String color;


// Public constructor public Car(String color){ this.color = color; } }

    public class Main {

      // main method
      public static void main(String[] args) {
          Car myCar = new Car("brown");
          // Can access public members 
          // from object instance
          System.out.println(
            myCar.color); // Brown
        }
      }

Java เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

Private properties

  • Properties ที่กำหนดเป็น private ไม่สามารถเข้าถึงได้จาก object instance
  • Properties ที่กำหนดเป็น private ใช้ได้เฉพาะภายใน class เท่านั้น

  // Car class
  class Car {
     public String color;

// Private properties private String model;
public Car(String color){ this.color = color; } }

    // Main class
    public class Main {
      // main method
      public static void main(String[] args) {
          Car myCar = new Car("brown");
          // Calling private properties causes errors        
          System.out.println(
            myCar.model); // Java compilation error
        }
      }


Java เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

Public methods


  // Car class
  class Car {
      public String color;
      private String model;

      public Car(String color){
          this.color = color;
      }

      // Public method
      public String getModel(){
          return this.model;
      }    
  }    

Java เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

Private methods


  class Car {


// Private method, "calculateSpeed" can only be used within "Car" class private calculateSpeed(){ // Trademarked formula code } public int getSpeed(){ // "calculateSpeed" can be used anywhere within "Car" class return this.calculateSpeed(); } }
Java เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

Static methods

  • Properties ที่กำหนดเป็น static เข้าถึงได้โดยไม่ต้องสร้าง object instance
  • มักใช้สำหรับเก็บโค้ดหรือไลบรารีที่ใช้ร่วมกัน

   // Formula class 
   static class Formula {

     // Method for calculating square
     static int getSquare(int number) {
        return number * number;
      }
   }





  // Main class 
  public class Main {

    public static void 
      main(String[] args) {
          // We can use "getSquare" 
          // without an object instance
        System.out.println(
          Formula.getSquare(5)); // 25
      }
  }

Java เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

สรุป

  • Access modifiers ได้แก่ public, private, static
  • Access modifiers ช่วยเพิ่มความปลอดภัยให้แอปพลิเคชัน
  • static ช่วยให้ใช้งาน class ได้โดยไม่ต้องสร้าง instance
Java เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

มาฝึกกันเถอะ!

Java เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

Preparing Video For Download...