12
1 Ereditarietà Prof. Francesco Scarcello D.E.I.S., Università della Calabria Corso di Informatica 2 Esempio: il conto bancario public class BankAccount { public BankAccount() { balance = 0;} public BankAccount(double initialBalance) { balance = initialBalance;} public void deposit(double amount) { balance = balance + amount;} public void withdraw(double amount) { balance = balance - amount; } public double getBalance(){return balance;} public void transfer(BankAccount other, double amount) { withdraw(amount); other.deposit(amount);} private double balance; }

ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

1

Ereditarietà

Prof. Francesco ScarcelloD.E.I.S., Università della Calabria

Corso di Informatica 2

Esempio: il conto bancariopublic class BankAccount

{ public BankAccount() { balance = 0;}

public BankAccount(double initialBalance)

{ balance = initialBalance;}

public void deposit(double amount)

{ balance = balance + amount;}

public void withdraw(double amount)

{ balance = balance - amount; }public double getBalance(){return balance;}public void transfer(BankAccount other,

double amount){ withdraw(amount);

other.deposit(amount);}private double balance;

}

Page 2: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

2

Estendere il codice

z Supponiamo di voler implementare altri tipi di conto bancario.

z Per esempio, consideriamo un conto bancario che dia interessi ad un certo tasso fisso interestRate.

z Dobbiamo riscrivere tutto il codice?z Possiamo sfruttare il codice esistente

estendendolo opportunamente?

La classe SavingsAccount

public class SavingsAccount extends BankAccount{

public SavingsAccount(double rate){ interestRate = rate;}

public void addInterest(){ double interest = getBalance() * interestRate / 100;

deposit(interest); }

private double interestRate;}

Page 3: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

3

Oggetti della sottoclasse SavingsAccount

…SavingsAccount s = new SavingsAccount(10);s.deposit(1000);…

Page 4: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

4

Quando si estende una classe...

z Si ereditano tutti i metodi e gli elementi di dati della superclasse

z Si possono definire nuovi metodi

z Si possono definire nuovi elementi di dati

z Si possono ridefinire metodi per specializzarne le funzionalità

Le regole del gioco

Una classey può estendere al più una classey può essere estesa da un numero arbitrario di

classi

Visibilitày una sottoclasse può accedere ad elementi di

dati ed a metodi che siano public o protectednella sua superclasse

Page 5: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

5

Riferimenti ed assegnamentoSavingsAccount collegeFund = new SavingsAccount(10);BankAccount anAccount = collegeFund;Object anObject = anAccount;SavingsAccount myFund = anAccount;SavingsAccount yourFund = new BankAccount();

Una variabile di una classe Cpuò essere usata per riferirsi agli oggetti di una qualunque sottoclasse di C

Altri conti bancariz Vogliamo un nuovo tipo di libretto di risparmio

che preveda interessi più alti, ma vincoli il cliente a non prelevare denaro prima di un certo tempo. Eventuali prelievi anticipati vengono penalizzati.

z Inoltre vogliamo un conto corrente che garantisca un certo numero di operazioni gratuite. Le operazioni che eccedono tale soglia sono soggette ad un costo.

Page 6: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

6

public class TimeDepositAccount extends SavingsAccount

{ public TimeDepositAccount(double rate, int maturity)

{ super(rate);

periodsToMaturity = maturity; }

public void addInterest()

{ periodsToMaturity--;

super.addInterest();}

public void withdraw(double amount)

{ if (periodsToMaturity > 0)

super.withdraw(EARLY_WITHDRAWAL_PENALTY);

super.withdraw(amount); }

private int periodsToMaturity;

private static double EARLY_WITHDRAWAL_PENALTY = 20; }

super si usa peraccedere a dati e funzioni

della superclasse

public class CheckingAccount extends BankAccount{

public CheckingAccount(int initialBalance){ super(initialBalance);

transactionCount = 0; }public void deposit(double amount){ transactionCount++;

super.deposit(amount); }public void withdraw(double amount) { transactionCount++;

super.withdraw(amount); }public void deductFees(){ if (transactionCount > FREE_TRANSACTIONS)

{ double fees = TRANSACTION_FEE *(transactionCount - FREE_TRANSACTIONS);

super.withdraw(fees);}transactionCount = 0;}

private int transactionCount;private static final int FREE_TRANSACTIONS = 3;private static final double TRANSACTION_FEE = 2.0;}

Page 7: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

7

La gerarchia dei conti correnti

La gerarchia dei rettili antichi

Page 8: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

8

Gerarchia di componenti per la grafica

Polimorfismoz Consideriamo la funzione transfer:

public void transfer(BankAccount other,double amount)

{ withdraw(amount);other.deposit(amount);}

z Ogni classe nella gerarchia eredita transfer

z Con quest’unica funzione possiamoeffettuare un trasferimento verso qualunque tipo di conto corrente

Page 9: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

9

BankAccount collegeFund = new BankAccount (10000);CheckingAccount harrysChecking = … ;…collegeFund.transfer (harrysChecking, 1000);

Nota che otherè di tipo

BankAccount

public class AccountTest{public static void endOfMonth(SavingsAccount savings){ savings.addInterest();}

public static void endOfMonth(CheckingAccount checking){ checking.deductFees();}

public static void printBalance(String name,BankAccount account)

{ System.out.println(“Il saldo del conto ” + name+ ” è di $” + account.getBalance());

}

Programma AccountTest.java

Page 10: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

10

Programma AccountTest.java (continua)

public static void main(String[] args) { SavingsAccount momsSavings = new SavingsAccount(0.5);TimeDepositAccount collegeFund=new TimeDepositAccount(1,3);CheckingAccount harrysChecking = new CheckingAccount(0);

momsSavings.deposit(10000);collegeFund.deposit(10000);momsSavings.transfer(harrysChecking, 2000);collegeFund.transfer(harrysChecking, 980);harrysChecking.withdraw(500);harrysChecking.withdraw(80);endOfMonth(momsSavings);endOfMonth(collegeFund);endOfMonth(harrysChecking);

printBalance(“Risparmi della mamma", momsSavings);printBalance(“Fondo per il college", collegeFund);printBalance(“Conto di Harry", harrysChecking);

} // Fine della funzione main } // Fine della classe AccountTest

La superclasse universaleLa classe Object costituisce la superclasse di ciascuna classe Java

Page 11: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

11

La classe Object

Alcune importanti funzioni definite in Object (e quindi ereditate da ogni classe):

z public String toString();z public boolean equals (Object ob);z protected Object clone();

Clonazione di oggettiBankAccount clonedAccount = anAccount.clone();

Page 12: ereditarietà - frank/POO/Slides/eredit.pdf · Object anObject = anAccount ; SavingsAccount myFund = anAccount ; SavingsAccount yourFund = new BankAccount (); Una variabile di una

12

Il metodo Object.clone()z Crea una copia superficialez Non può essere effettivamente invocato a meno

che non si implementi l’interfaccia Cloneable