public void transferTo(Account whereTo, double amount) {
this.add(-amount);
whereTo.add(amount);
}
public void transferTo(Account whereTo) {
whereTo.add(balance);
balance = 0;
}
public void transferTo(Account whereTo, double amount) {
this.add(-amount);
whereTo.add(amount);
}
public void transferTo(Account whereTo) {
whereTo.add(balance);
balance = 0;
}
amount
peněz.balance
) z účtu odesílatele.public void transferTo(Account whereTo, double amount) {
this.add(-amount);
whereTo.add(amount);
}
public void transferTo(Account whereTo) {
transferTo(whereTo, balance);
}
Zkusme naší metodu lépe popsat:
public void transferTo(Account whereTo, double amount) {
this.add(-amount);
whereTo.add(amount);
}
public void transferAllMoneyTo(Account whereTo) {
transferTo(whereTo, balance);
}
this
public Person() {
this("Default name"); // calls second constructor
}
public Person(String name) {
this.name = name;
}
Která metoda se zavolá?
public int getNumber() {
return 5;
}
public short getNumber() { // smaller int
return 6;
}
...
long bigInt = getNumber(); // 5 or 6?
int
na long
, nebo short
na long
?new String("Sss").isEmpty(); // result is omitted
public void getNumber() {
// do nothing
}
public int getNumber() { // smaller int
return 6;
}
...
getNumber(); // which one is called?
this
:public class Account {
private double balance;
public Account(double balance) {
this.balance = balance;
}
public Account transferTo(Account whereTo, double amount) {
add(-amount);
whereTo.add(amount);
return this; // return original object
}
}
Account petrsAccount = new Account(100);
Account ivansAccount = new Account(100);
Account robertsAccount = new Account(1000);
// we can chain methods
petrsAccount
.transferTo(ivansAccount, 50)
.transferTo(robertsAccount, 20);
StringBuilder
metody append
./