Last active
December 20, 2015 20:28
-
-
Save marciojrtorres/6190208 to your computer and use it in GitHub Desktop.
Sobrecarga de construtores para definir valores padrão e centralizar salva-guardas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dinheiro { | |
private String simbolo; | |
private String nome; | |
private int inteiro; | |
private int decimal; | |
public Dinheiro(String simbolo, String nome, int inteiro, int decimal) { | |
if (inteiro < 0) { | |
throw new IllegalArgumentException("inteiro negativo: " + inteiro); | |
} | |
if (decimal < 0) { | |
throw new IllegalArgumentException("decimal negativo: " + decimal); | |
} | |
this.simbolo = simbolo; | |
this.nome = nome; | |
this.inteiro = inteiro; | |
this.decimal = decimal; | |
} | |
public Dinheiro(int inteiro, int decimal) { | |
this("R$", "Real", inteiro, decimal); | |
} | |
public Dinheiro(int inteiro) { | |
this("R$", "Real", inteiro, 0); | |
} | |
public String getSimbolo() { return simbolo; } | |
public int getInteiro() { return inteiro; } | |
public int getDecimal() { return decimal; } | |
@Override | |
public String toString() { | |
return simbolo + " " + inteiro + "," + decimal; | |
} | |
} | |
// situação de uso: | |
Dinheiro valor1 = new Dinheiro(45, 12); // R$ 45,12 | |
Dinheiro valor2 = new Dinheiro(890); // R$ 890,00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dinheiro { | |
private String simbolo; | |
private int inteiro; | |
private int decimal; | |
public Dinheiro(String simbolo, int inteiro, int decimal) { | |
this.simbolo = simbolo; | |
this.inteiro = inteiro; | |
this. decimal = decimal; | |
} | |
public String getSimbolo() { return simbolo; } | |
public int getInteiro() { return inteiro; } | |
public int getDecimal() { return decimal; } | |
@Override | |
public String toString() { | |
return simbolo + " " + inteiro + "," + decimal; | |
} | |
} | |
// situação de uso: | |
Dinheiro valor1 = new Dinheiro("R$", "Real", 45, 12); | |
Dinheiro valor2 = new Dinheiro("£", "Libra", 34, 12); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dinheiro { | |
private String simbolo; | |
private String nome; | |
private int inteiro; | |
private int decimal; | |
public Dinheiro(String simbolo, String nome, int inteiro, int decimal) { | |
this.simbolo = simbolo; | |
this.nome = nome; | |
this.inteiro = inteiro; | |
this. decimal = decimal; | |
} | |
public Dinheiro(int inteiro, int decimal) { | |
this("R$", "Real", inteiro, decimal); | |
} | |
public Dinheiro(int inteiro) { | |
this("R$", "Real", inteiro, 0); | |
} | |
public String getSimbolo() { return simbolo; } | |
public int getInteiro() { return inteiro; } | |
public int getDecimal() { return decimal; } | |
@Override | |
public String toString() { | |
return simbolo + " " + inteiro + "," + decimal; | |
} | |
} | |
// situação de uso: | |
Dinheiro valor1 = new Dinheiro(45, 12); // R$ 45,12 | |
Dinheiro valor2 = new Dinheiro(890); // R$ 890,00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment