Přidávání vlastností

Introduction to Object-Oriented Programming in Java

Sani Yusuf

Lead Software Engineering Content Developer

Příklad s autem

  • Auta na silnici se mohou lišit vlastnostmi, jako je barva nebo model.

Auta

Introduction to Object-Oriented Programming in Java

Rozlišení aut

  • I stejná auta se liší barvou, SPZ a registrací vozidla.

Stejná auta

Introduction to Object-Oriented Programming in Java

Přidávání vlastností do třídy

  • Vlastnosti popisují třídu
  • Vlastnosti mohou mít různé datové typy

  // Car class
  class Car {
    String model; // Property for model of car

int topSpeed; // Property for car's top speed boolean isInsured; // Property for current insurance state }
Introduction to Object-Oriented Programming in Java

Konstruktory v Javě

  • Konstruktor je metoda, která se vždy volá při vytvoření instance třídy
  • Konstruktor musí mít stejný název jako třída, ve které se nachází

  class Passport {
    String firstName; // Passport holder's first name
    String lastName; // Passport holder's last name

    Passport() {
      // Constructor of Passport class
    }
  }


Introduction to Object-Oriented Programming in Java

Nastavení vlastností uvnitř konstruktoru


  class Passport {
    String firstName; // Passport holder's first name
    String lastName; // Passport holder's last name

    Passport() {
      this.firstName = "David";  // Set property inside constructor
      this.lastName = "Beckham";
    }
  }


Introduction to Object-Oriented Programming in Java

Konstruktory s parametry

  • Konstruktory mohou mít parametry stejně jako jiné metody

  class Passport {
    String firstName;
    String lastName;

    // Constructor with parameters
    Passport(String firstName, String lastName) {  

    }
  }


Introduction to Object-Oriented Programming in Java

Nastavení vlastností pomocí parametrů konstruktoru

  • Klíčové slovo this odkazuje na objekt Passport
  • Parametry konstruktoru obvykle mají stejný název jako vlastnosti třídy

  class Passport {
    String firstName;
    String lastName;

    Passport(String firstName, String lastName) {
          this.firstName = firstName; // Setting class properties
          this.lastName = lastName;   //  with constructor parameters
    }
  }


Introduction to Object-Oriented Programming in Java

Vytváření instancí objektů s parametry

  • Objekty se vytvářejí pomocí klíčového slova new
  • Parametry konstruktoru se předávají při vytvoření objektu

  // Passport Class with constructor
  class Passport {
    String firstName;
    String lastName;

    // Constructor method
    Passport(String firstName, 
             String lastName){
          this.firstName = firstName;
          this.lastName = lastName;
    }
  }





  // Main Class    
  public class Main {  
    // main method (program entry point)
    public static void main(
      String[] args) {

      // Passing parameters to constructor 
      // Passport(firstName, lastName)
      Passport myPassport = 
        new Passport("Michael","Jackson"); 
      System.out.println(
        myPassport.firstName); // Michael
    }
  }


Introduction to Object-Oriented Programming in Java

Pojďme si procvičit!

Introduction to Object-Oriented Programming in Java

Preparing Video For Download...