26.08.2014 Views

C# e Orientação a Objetos

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

INTERFACES 114<br />

2 Defina uma interface para padronizar as assinaturas dos métodos das contas do banco.<br />

1 // prefixo I para seguir a convenção<br />

2 interface IConta<br />

3 {<br />

4 void Deposita ( double valor );<br />

5 void Saca ( double valor );<br />

6 double Saldo { get ; set ; }<br />

7 }<br />

Código <strong>C#</strong> 11.10: IConta.cs<br />

3 Agora, crie algumas classes para modelar tipos diferentes de conta.<br />

1 class ContaCorrente : IConta<br />

2 {<br />

3 public double Saldo { get ; set ; }<br />

4 private double taxaPorOperacao = 0.45;<br />

5<br />

6 public void Deposita ( double valor )<br />

7 {<br />

8 this . Saldo += valor - this . taxaPorOperacao ;<br />

9 }<br />

10<br />

11 public void Saca ( double valor )<br />

12 {<br />

13 this . Saldo -= valor + this . taxaPorOperacao ;<br />

14 }<br />

15 }<br />

Código <strong>C#</strong> 11.11: ContaCorrente.cs<br />

1 class ContaPoupanca : IConta<br />

2 {<br />

3 public double Saldo { get ; set ; }<br />

4<br />

5 public void Deposita ( double valor )<br />

6 {<br />

7 this . Saldo += valor ;<br />

8 }<br />

9<br />

10 public void Saca ( double valor )<br />

11 {<br />

12 this . Saldo -= valor ;<br />

13 }<br />

14 }<br />

Código <strong>C#</strong> 11.12: ContaPoupanca.cs<br />

4 Faça um teste simples com as classes criadas anteriormente.<br />

1 class TestaContas<br />

2 {<br />

3 static void Main ()<br />

4 {<br />

5 ContaCorrente c1 = new ContaCorrente ();<br />

6 ContaPoupanca c2 = new ContaPoupanca ();<br />

7<br />

8 c1. Deposita (500) ;<br />

9 c2. Deposita (500) ;<br />

10<br />

11 c1. Saca (100) ;<br />

12 c2. Saca (100) ;<br />

114 www.k19.com.br

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!