Slide 82
Slide 82 text
Answer: Create a "case" or "choice" class that encapsulates all
three payment methods in one class, in one place.
void PrintPayment(payment) =
switch (payment)
{
case Cash : // print cash
case Check(checkNo) : // print check info
case Card(cardType,cardNo) // print card info
}
A case statement is then used to match the
subclass that was created, and at the same
time extract the relevant data.
class PaymentMethod =
| Cash
| Check(int checkNo)
| Card(string cardType, string cardNo)
PaymentMethod cash = Cash()
PaymentMethod check = Check(123);
PaymentMethod card = Card("Visa", "4012888888881881");
There are three different constructors, each
with different data.
The compiler keeps track of which constructor
was used to create the instance.