Слайд 124)
class Nevermore60Customer: GenericCustomer
{
public Nevermore60Customer(string name, string referrerName)
: base(name)
{
this.referrerName =
referrerName;
}
private string referrerName;
private uint highCostMinutesUsed;
25)
public Nevermore60Customer(string name) : this(name, "")
{
}
26)
GenericCustomer customer = new Nevermore60Customer("Arabel Jones");
Слайд 227)
public Nevermore60Customer(string Name)
: this(Name, "")
28)
Таблица 4.1. Модификаторы
видимости
Слайд 329)
public class MyClass
{
// и т.д.
30)
public class
OuterClass
{
protected class InnerClass
{
// и т.д.
}
// и т.д.
}
Слайд 4Таблица 4.2. Другие модификаторы
Слайд 532)
public interface IDisposable
{
void Dispose() ;
}
33)
class SomeClass: IDisposable
{
// Этот класс.ДОЛЖЕН содержать реализацию
// метода IDisposable.Dispose
(), иначе
// возникнет ошибка компиляции.
public void Dispose ()
{
// реализация метода Dispose()
}
// остальная часть класса
}
Слайд 634)
namespace Wrox.ProCSharp
{
public interface IBankAccount
{
void
PayIn(decimal amount);
bool Withdraw(decimal amount);
decimal Balance
{
get;
}
}
}
35)
namespace Wrox.ProCSharp.VenusBank
{
public class SaverAccount: IBankAccount
{
Слайд 7 private decimal balance;
public void Payln(decimal
amount)
{
balance +=
amount;
}
public bool Withdraw(decimal amount)
{
if (balance >= amount)
{
balance -= amount;
return true;
}
Console.WriteLine("Попытка перевода денег не удалась.");
return false;
}
public decimal Balance
{
get
{
return balance;
}
}
Слайд 8 public override string ToString()
{
return String.Format(
"Сберегательный Банк Венеры: Баланс = (0,6:С}", balance);
}
}
}
36) public class SaverAccount: IBankAccount
37)
namespace Wrox.ProCSharp.JupiterBank
{
public class GoldAccount: IBankAccount
{
// и т.д.
}
}
Слайд 938)
using System;
using Wrox.ProCSharp;
using Wrox.ProCSharp.VenusBank;
using Wrox.ProCSharp.JupiterBank;
39)
namespace Wrox.ProCSharp
{
class MainEntryPoint
{
static void Main()
{
IBankAccount venusAccount = new SaverAccount();
IBankAccount jupiterAccount = new GoldAccount();
venusAccount.Payln(200);
venusAccount.Withdraw(100);
Console.WriteLine(venusAccount.ToString());
jupiterAccount.PayIn(500);
Слайд 10 jupiterAccount.Withdraw(600);
jupiterAccount.Withdraw(100);
Console.WriteLine(jupiterAccount.ToString());
}
}
}
40)
С:> BankAccounts
Сберегательный Банк Венеры: Баланс
= £100.00
Попытка перевода денег не удалась.
Планетарный Банк Юпитера: Баланс = £400.00
41)
IBankAccount!] accounts = new IBankAccount[2];
accounts[0] = new SaverAccount();
accounts [1] =» new GoldAccount();
42)
accounts[1] = new SomeOtherClass(); //SomeOtherClass не реализует
// IBankAccount: НЕВЕРНО!!
Слайд 1143)
Cannot implicitly convert type 'Wrox.ProCSharp.SomeOtherClass' to 'Wrox.
ProCSharp.IBankAccount'
Неявное преобразование типа
'Wrox.ProCSharp.SomeOtherClass' в 'Wrox.ProCSharp.
IBankAccount' невозможно
44)
namespace Wrox.ProCSharp
{
public interface ITransferBankAccount: IBankAccount
{
bool TransferTo(IBankAccount destination, decimal amount);
}
}
Слайд 1245)
public class CurrentAccount: ITransferBankAccount
{
private decimal balance; public void Payln(decimal
amount)
{
balance += amount;
}
public bool
Withdraw(decimal amount)
{
if (balance >= amount)
{
balance -= amount;
return true;
}
Console.WriteLine("Попытка перевода денег не удалась.");
return false;
}
Слайд 13public decimal Balance
{
get
{
return balance;
}
}
public bool TransferTo(IBankAccount destination, decimal amount)
{
bool result;
result = Withdraw (amount) ;
if (result)
{
destination.Payln(amount);
}
return result;
}
Слайд 14 public override string ToString()
{
return
String.Format(
"Текущий счет в Банке Юпитера:
Баланс = {0,6:С}", balance);
}
}
46)
static void Main()
{
IBankAccount venusAccount = new SaverAccount ();
ITransferBankAccount jupiterAccount = new CurrentAccount();
venusAccount.Payln(200); jupiterAccount.Payln(500);
jupiterAccount.TransferTo(venusAccount, 100);
Console.WriteLine(venusAccount.ToString0) ;
Console.WriteLine(jupiterAccount.ToString() ) ;
}
Слайд 1547)
С:> CurrentAccount
Сберегательный Банк Венеры: Баланс = £300.00
Текущий счет в Банке
Юпитера: Баланс = £400.00