Zapouzdření a modifikátory přístupu

Introduction to Object-Oriented Programming in Java

Sani Yusuf

Lead Software Engineering Content Developer

Porozumění zapouzdření

  • Zapouzdření lze přiblížit na příkladu mobilního telefonu
  • Uživatel má přístup pouze k vyhrazeným prvkům: obrazovce, reproduktoru apod.
  • Vnitřní funkce jsou před uživatelem zcela skryty

Mobilní telefon

Introduction to Object-Oriented Programming in Java

Veřejné vlastnosti

  • Vlastnosti označené public jsou přístupné instancím objektu

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

Introduction to Object-Oriented Programming in Java

Privátní vlastnosti

  • Vlastnosti označené private nejsou přístupné instancím objektu
  • Vlastnosti označené private lze použít pouze uvnitř třídy

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


Introduction to Object-Oriented Programming in Java

Veřejné metody


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

Introduction to Object-Oriented Programming in Java

Privátní metody


  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(); } }
Introduction to Object-Oriented Programming in Java

Statické metody

  • Vlastnosti označené static jsou přístupné bez instance objektu
  • Obvykle slouží k uchování sdíleného kódu/knihoven

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

Introduction to Object-Oriented Programming in Java

Rekapitulace

  • Modifikátory přístupu: public, private, static
  • Modifikátory přístupu pomáhají zabezpečit aplikace
  • static umožňuje použití tříd bez vytváření instancí
Introduction to Object-Oriented Programming in Java

Pojďme si procvičit!

Introduction to Object-Oriented Programming in Java

Preparing Video For Download...