static void Main(string[] args) { BusinessAccount bAcc = new BusinessAccount(5621, "Some Name Business", 10000.00, 50000.00); Account acc = new Account(8031, "Some Name 02", 0.0); //Upcasting sample Account acc1 = bAcc; Account acc2 = new BusinessAccount(3231, "Some Name 03", 321.3, 20000); Account acc3 = new SavingsAccount(1854, "Some name 05", 0.0, 0.02); //Donwcasting sample BusinessAccount acc4 = (BusinessAccount)acc2; acc4.Loan(340); // BusinessAccount acc5 = (BusinessAccount)acc3; if (acc3 is BusinessAccount) //Must do this to validate right object { BusinessAccount acc5 = (BusinessAccount)acc3; } if (acc2 is BusinessAccount) { BusinessAccount acc6 = acc2 as BusinessAccount; } }
static void Main(string[] args) { Account acc = new Account(1001, "Alex", 0.0); BusinessAccount bacc = new BusinessAccount(1002, "Maria", 0.0, 500.0); // UPCASTING Account acc1 = bacc; Account acc2 = new BusinessAccount(1003, "Bob", 0.0, 200.0); Account acc3 = new SavingsAccount(1004, "Anna", 0.0, 0.01); // DOWNCASTING BusinessAccount acc4 = (BusinessAccount)acc2; acc4.Loan(100.0); // BusinessAccount acc5 = (BusinessAccount)acc3; if (acc3 is BusinessAccount) { //BusinessAccount acc5 = (BusinessAccount)acc3; BusinessAccount acc5 = acc3 as BusinessAccount; acc5.Loan(200.0); Console.WriteLine("Loan!"); } if (acc3 is SavingsAccount) { //SavingsAccount acc5 = (SavingsAccount)acc3; SavingsAccount acc5 = acc3 as SavingsAccount; acc5.UpdateBalance(); Console.WriteLine("Update!"); } }
static void Main(string[] args) { Account acc = new Account(1001, "Alex", 0.0); BusinessAccount bacc = new BusinessAccount(1002, "Maria", 0.0, 500.0); //upcasting Account acc1 = bacc; Account acc2 = new Account(1003, "Bob", 500.0); Account acc3 = new SavingsAccount(1004, "Mike", 500.0, 0.08); acc2.Withdraw(5.00); acc3.Withdraw(5.00); Console.WriteLine(acc2.Balance); Console.WriteLine(acc3.Balance); }
static void Main(string[] args) { BusinessAccount businessAccount = new BusinessAccount(1234, "Thiago M.", 1500.00, 500.00); Console.WriteLine(businessAccount.Balance); }