-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from vbuxbaum/cap-introducao
Cap introducao - Decisões e continuação de implementações
- Loading branch information
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Implementado no arquivo 8_heranca_poliformismo.py | ||
""" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
Nesta implementação em Java, o método imprime() lança Strings na saída padrão. | ||
Para facilitar a implementação de testes, o comportamento desse método foi | ||
alterado para que apenas retorne as respectivas Strings. | ||
""" | ||
|
||
|
||
class ContaBancaria: | ||
def __init__(self, saldo_inicial): | ||
self.saldo = saldo_inicial | ||
|
||
def depositar(self, valor): | ||
self.saldo += valor | ||
|
||
def sacar(self, valor): | ||
self.saldo -= valor | ||
|
||
def verifica_saldo(self): | ||
return "Saldo = $" + str(self.saldo) | ||
|
||
|
||
conta_teste = ContaBancaria(200.00) | ||
|
||
assert conta_teste.verifica_saldo() == "Saldo = $200.0" | ||
|
||
conta_teste.depositar(50.00) | ||
conta_teste.sacar(70.00) | ||
|
||
assert conta_teste.verifica_saldo() == "Saldo = $180.0" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
Nesta implementação em Java, o livro utiliza o artificio 'static' da linguagem. | ||
Entretanto não há uma forma, em Python, para simular o comportamento de | ||
atributos estáticos EXATAMENTE como ocorre em Java. | ||
Para simular o comportamento em Python, o atributo estático deve ser acessado | ||
diretamente por sua classe, e não pela instância (como é feito no livro). | ||
""" | ||
|
||
|
||
class A: | ||
|
||
STATIC = int() | ||
|
||
def __init__(self): | ||
self.media = int() | ||
|
||
|
||
a = A() | ||
A.STATIC, a.STATIC, a.media = 5, 5, 5 | ||
|
||
b = A() | ||
A.STATIC, b.STATIC, b.media = 7, 7, 7 | ||
|
||
|
||
assert (A.STATIC, a.STATIC, a.media, b.STATIC, b.media) == (7, 5, 5, 7, 7) |
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