static void Main(string[] args) { BussinessAccount account = new BussinessAccount(100, "Diogo Rondao", 100.0, 500.0); Console.WriteLine(account.Balance); //account.Balance = 200.0; }
static void Main(string[] args) { //var acc = new Account(1, "Eduardo Marim", 100.00); Nao pode criar instancias de classes abstratas var accB = new BussinessAccount(2, "Renata Esperandio", 3000000.00, 20000.00); Account savings = new SavingsAccount(10, "Moacir Marim", 100.00, 0.01); //UPCASTING Account ac2 = accB; Account ac3 = new BussinessAccount(); //DOWNCASTING - E necessario o casting explicito. BussinessAccount accB2 = ac3 as BussinessAccount; // usando o as ou... BussinessAccount accB3 = (BussinessAccount)ac3; //outra forma de casting if (accB is SavingsAccount) //Verifica se o tipo e do tipo... //SavingsAccount scv = (SavingsAccount)accB; { System.Console.WriteLine(accB.GetType() + " e uma instancia de Account"); } //acc.Witdraw(10); //System.Console.WriteLine(acc); savings.Witdraw(10); System.Console.WriteLine(savings); List <Account> lstAccount = new List <Account>(); lstAccount.Add(new BussinessAccount(10, "Eduardo Marim", 500.00, 20.00)); lstAccount.Add(new BussinessAccount(11, "Renata Sperandio", 400.00, 25.00)); lstAccount.Add(new SavingsAccount(12, "Luzia Odete", 450.00, 0.02)); double totalBalanceHolders = 0.00; foreach (Account item in lstAccount) { totalBalanceHolders += item.Balance; System.Console.WriteLine($"Account:{item.Number}, Holder:{item.Holder}, Balance:{item.Balance.ToString("F2")}"); } System.Console.WriteLine($"Total Balance Accounts: {totalBalanceHolders.ToString("F2")}"); foreach (Account item in lstAccount) { item.Witdraw(10); } totalBalanceHolders = 0.00; foreach (Account item in lstAccount) { totalBalanceHolders += item.Balance; System.Console.WriteLine($"Account:{item.Number}, Holder:{item.Holder}, Balance:{item.Balance.ToString("F2")}"); } System.Console.WriteLine(value: $"Total Balance Accounts: {totalBalanceHolders.ToString("F2")}"); }
static void Main(string[] args) { Account acc = new Account(1001, "Alex", 0.0); BussinessAccount bacc = new BussinessAccount(1002, "Maria", 0.0, 500.0); //UPCASTING Account acc1 = bacc; Account acc2 = new BussinessAccount(1003, "Bob", 0.0, 200.0); Account acc3 = new SavingsAccount(1004, "Ana", 0.0, 0.01); //DOWNCASTING BussinessAccount ACC4 = (BussinessAccount)acc2; ACC4.Loan(100.0); }
static void Main(string[] args) { Account acc = new Account(1001, "Alex", 0.0); BussinessAccount bacc = new BussinessAccount(1002, "Maria", 0.0, 500.0); //UPCASTING Account acc1 = bacc; // essa conversão é perfeitamente legal, o complilador aceita. Account acc2 = new BussinessAccount(1003, "Bob", 0.0, 200.0); // posso fazer isso aqui também Account acc3 = new SavingsAccount(104, "Anna", 0.0, 0.01); // outra forma possível //DOWNCASTING BussinessAccount acc4 = (BussinessAccount)acc2; // fez a conversão Casting acc4.Loan(100.0); // agora conseguimos ter acesso a operacao LOAN // BussinessAccount acc5 = (BussinessAccount)acc3; // os dois tipos são incompativeis mesmo o compilador nao ter dado o erro. // são subclasses //Test if (acc3 is BussinessAccount) { //BussinessAccount acc5 = (BussinessAccount)acc3; //modo1 BussinessAccount acc5 = acc3 as BussinessAccount; // modo2 acc5.Loan(200.0); Console.WriteLine("loan!"); } if (acc3 is SavingsAccount) { //SavingsAccount acc5 = (SavingsAccount)acc3; //modo 1 SavingsAccount acc5 = acc3 as SavingsAccount; //modo 2 acc5.UpdateBalance(); Console.WriteLine("Update!"); } }