Наследование 2

Содержание

Слайд 2

27) public Nevermore60Customer(string Name) : this(Name, " ") 28) Таблица 4.1. Модификаторы видимости

27)
public Nevermore60Customer(string Name)
: this(Name, "")
28)

Таблица 4.1. Модификаторы видимости

Слайд 3

29) public class MyClass { // и т.д. 30) public class

29)
public class MyClass
{
// и т.д.
30)
public class OuterClass
{
protected

class InnerClass
{
// и т.д.
}
// и т.д.
}
Слайд 4

Таблица 4.2. Другие модификаторы

Таблица 4.2. Другие модификаторы

Слайд 5

32) public interface IDisposable { void Dispose() ; } 33) class

32)
public interface IDisposable
{
void Dispose() ;
}
33)
class SomeClass: IDisposable
{
// Этот класс.ДОЛЖЕН

содержать реализацию
// метода IDisposable.Dispose (), иначе
// возникнет ошибка компиляции.
public void Dispose ()
{
// реализация метода Dispose()
}
// остальная часть класса
}
Слайд 6

34) namespace Wrox.ProCSharp { public interface IBankAccount { void PayIn(decimal amount);

34)
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;

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( "Сберегательный Банк Венеры: Баланс

public override string ToString()
{
return String.Format(
"Сберегательный Банк

Венеры: Баланс = (0,6:С}", balance);
}
}
}
36) public class SaverAccount: IBankAccount
37)
namespace Wrox.ProCSharp.JupiterBank
{
public class GoldAccount: IBankAccount
{
// и т.д.
}
}
Слайд 9

38) using System; using Wrox.ProCSharp; using Wrox.ProCSharp.VenusBank; using Wrox.ProCSharp.JupiterBank; 39) namespace

38)
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 Сберегательный Банк

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: НЕВЕРНО!!
Слайд 11

43) Cannot implicitly convert type 'Wrox.ProCSharp.SomeOtherClass' to 'Wrox. ProCSharp.IBankAccount' Неявное преобразование

43)
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);
}
}
Слайд 12

45) public class CurrentAccount: ITransferBankAccount { private decimal balance; public void

45)
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;
}
Слайд 13

public decimal Balance { get { return balance; } } public

public 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( "Текущий счет в Банке

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() ) ;
}