Aggiungere proprietà

Introduzione alla programmazione orientata agli oggetti in Java

Sani Yusuf

Lead Software Engineering Content Developer

Esempio: auto

  • Le auto in strada possono avere forme diverse, con caratteristiche come colore e modello che le distinguono.

Auto

Introduzione alla programmazione orientata agli oggetti in Java

Distinguere le auto

  • Anche quando le auto sono uguali, differiscono per colore, targa, libretto.

Auto identiche

Introduzione alla programmazione orientata agli oggetti in Java

Aggiungere proprietà alla classe

  • Le proprietà descrivono una classe
  • Le proprietà possono avere tipi di dato diversi

  // 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 }
Introduzione alla programmazione orientata agli oggetti in Java

Costruttori in Java

  • Il costruttore è un metodo, chiamato sempre quando si crea un'istanza della classe
  • Il costruttore deve avere lo stesso nome della classe

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

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


Introduzione alla programmazione orientata agli oggetti in Java

Impostare proprietà dentro i costruttori


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


Introduzione alla programmazione orientata agli oggetti in Java

Costruttori con parametri

  • I costruttori possono avere parametri come qualsiasi altro metodo

  class Passport {
    String firstName;
    String lastName;

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

    }
  }


Introduzione alla programmazione orientata agli oggetti in Java

Impostare proprietà con i parametri del costruttore

  • La parola chiave this si riferisce all'oggetto Passport
  • È comune dare ai parametri del costruttore lo stesso nome delle proprietà

  class Passport {
    String firstName;
    String lastName;

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


Introduzione alla programmazione orientata agli oggetti in Java

Creare istanze con parametri

  • Gli oggetti si creano con la parola chiave new
  • I parametri del costruttore si passano durante la creazione

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


Introduzione alla programmazione orientata agli oggetti in Java

Esercitiamoci!

Introduzione alla programmazione orientata agli oggetti in Java

Preparing Video For Download...