Properties and Encapsulation
Encapsulated State
Methods can change private state while the class controls what is allowed.
encapsulation
Encapsulation keeps state changes inside the object that owns the state.
Encapsulated State
EncapsulatedState.cs
Replay: real traced execution (multi-file project)
using System;
class Wallet
{
private int cents;
public int Cents
{
get { return cents; }
}
public void Deposit(int amount)
{
if (amount > 0)
{
cents += amount;
}
}
public bool Spend(int amount)
{
if (amount <= cents)
{
cents -= amount;
return true;
}
return false;
}
}
class Program
{
static void Main()
{
int spend = 30;
Wallet wallet = new Wallet();
wallet.Deposit(50);
bool paid = wallet.Spend(spend);
Console.WriteLine($"spend={spend}");
Console.WriteLine($"paid={paid}");
Console.WriteLine($"remaining={wallet.Cents}");
}
}
using System;
class Wallet
{
private int cents;
public int Cents
{
get { return cents; }
}
public void Deposit(int amount)
{
if (amount > 0)
{
cents += amount;
}
}
public bool Spend(int amount)
{
if (amount <= cents)
{
cents -= amount;
return true;
}
return false;
}
}
class Program
{
static void Main()
{
int spend = 20;
Wallet wallet = new Wallet();
wallet.Deposit(50);
bool paid = wallet.Spend(spend);
Console.WriteLine($"spend={spend}");
Console.WriteLine($"paid={paid}");
Console.WriteLine($"remaining={wallet.Cents}");
}
}
using System;
class Wallet
{
private int cents;
public int Cents
{
get { return cents; }
}
public void Deposit(int amount)
{
if (amount > 0)
{
cents += amount;
}
}
public bool Spend(int amount)
{
if (amount <= cents)
{
cents -= amount;
return true;
}
return false;
}
}
class Program
{
static void Main()
{
int spend = 80;
Wallet wallet = new Wallet();
wallet.Deposit(50);
bool paid = wallet.Spend(spend);
Console.WriteLine($"spend={spend}");
Console.WriteLine($"paid={paid}");
Console.WriteLine($"remaining={wallet.Cents}");
}
}
spend ← 30, wallet ← Wallet
33{34 static void Main()35 {36 int spend→ 30 = 30; //@spend=20, 8037 Wallet wallet→ Wallet = new Wallet();38 walletWallet.Deposit(50);39 bool paid = wallet.Spend(spend);public void Deposit(int amount)
12public void Deposit(int amount50)13{14 if (amount > 0)cents ← 50
13{14 if (amount50 > 0)15 {16 cents→ 50 += amount50;17 }wallet.Deposit(50);
37Wallet wallet = new Wallet();38walletWallet.Deposit(50);39bool paid = walletWallet.Spend(spend30);public bool Spend(int amount)
20public bool Spend(int amount30)21{22 if (amount <= cents)cents ← 20
21{22 if (amount30 <= cents50)23 {24 cents→ 20 -= amount30;25 return true;26 }paid ← True
38wallet.Deposit(50);39bool paid→ True = walletWallet.Spend(spend30);4041Console.WriteLine($"spend={spend30}");42Console.WriteLine($"paid={paidTrue}");43Console.WriteLine($"remaining={wallet.Cents}");outputspend=30 paid=Trueget
pass 1 of 58{9 get { return cents20; }10}Console.WriteLine($"remaining={wallet.Cents}");
42 Console.WriteLine($"paid={paid}");43 Console.WriteLine($"remaining={wallet.Cents20}");44}Console.WriteLine($"remaining={wallet.Cents}");
42 Console.WriteLine($"paid={paid}");43 Console.WriteLine($"remaining={wallet.Cents20}");44}outputremaining=20
spend ← 20, wallet ← Wallet
33{34 static void Main()35 {36 int spend→ 20 = 20;37 Wallet wallet→ Wallet = new Wallet();38 walletWallet.Deposit(50);39 bool paid = wallet.Spend(spend);public void Deposit(int amount)
12public void Deposit(int amount50)13{14 if (amount > 0)cents ← 50
13{14 if (amount50 > 0)15 {16 cents→ 50 += amount50;17 }wallet.Deposit(50);
37Wallet wallet = new Wallet();38walletWallet.Deposit(50);39bool paid = walletWallet.Spend(spend20);public bool Spend(int amount)
20public bool Spend(int amount20)21{22 if (amount <= cents)cents ← 30
21{22 if (amount20 <= cents50)23 {24 cents→ 30 -= amount20;25 return true;26 }paid ← True
38wallet.Deposit(50);39bool paid→ True = walletWallet.Spend(spend20);4041Console.WriteLine($"spend={spend20}");42Console.WriteLine($"paid={paidTrue}");43Console.WriteLine($"remaining={wallet.Cents}");outputspend=20 paid=Trueget
pass 1 of 58{9 get { return cents30; }10}Console.WriteLine($"remaining={wallet.Cents}");
42 Console.WriteLine($"paid={paid}");43 Console.WriteLine($"remaining={wallet.Cents30}");44}Console.WriteLine($"remaining={wallet.Cents}");
42 Console.WriteLine($"paid={paid}");43 Console.WriteLine($"remaining={wallet.Cents30}");44}outputremaining=30
spend ← 80, wallet ← Wallet
33{34 static void Main()35 {36 int spend→ 80 = 80;37 Wallet wallet→ Wallet = new Wallet();38 walletWallet.Deposit(50);39 bool paid = wallet.Spend(spend);public void Deposit(int amount)
12public void Deposit(int amount50)13{14 if (amount > 0)cents ← 50
13{14 if (amount50 > 0)15 {16 cents→ 50 += amount50;17 }wallet.Deposit(50);
37Wallet wallet = new Wallet();38walletWallet.Deposit(50);39bool paid = walletWallet.Spend(spend80);public bool Spend(int amount)
20public bool Spend(int amount80)21{22 if (amount <= cents)23 {24 cents -= amount;25 return true;26 }2728 return false;29}paid ← False
38wallet.Deposit(50);39bool paid→ False = walletWallet.Spend(spend80);4041Console.WriteLine($"spend={spend80}");42Console.WriteLine($"paid={paidFalse}");43Console.WriteLine($"remaining={wallet.Cents}");outputspend=80 paid=Falseget
pass 1 of 58{9 get { return cents50; }10}Console.WriteLine($"remaining={wallet.Cents}");
42 Console.WriteLine($"paid={paid}");43 Console.WriteLine($"remaining={wallet.Cents50}");44}Console.WriteLine($"remaining={wallet.Cents}");
42 Console.WriteLine($"paid={paid}");43 Console.WriteLine($"remaining={wallet.Cents50}");44}outputremaining=50