Exemple #1
0
        static void Main(string[] args)
        {
            Account         acc  = new Account(1001, "Alex", 0.0);
            BussinesAccount bacc = new BussinesAccount(1002, "Maria", 0.0, 500.00);

            //UPCASTING

            Account acc1 = bacc;
            Account acc2 = new BussinesAccount(1003, "Bob", 0.0, 200.00);
            Account acc3 = new SavingsAccount(1004, "Anna", 0.0, 0.01);

            //DOWNCASTING

            BussinesAccount acc4 = (BussinesAccount)acc2;

            acc4.Loan(100.00);

            if (acc3 is BussinesAccount)
            {
                // [FORMA 1] BussinesAccount acc5 = (BussinesAccount)acc3;
                BussinesAccount acc5 = acc3 as BussinesAccount;
                acc5.Loan(200.00);
                Console.WriteLine("Loan");
            }

            if (acc3 is SavingsAccount)
            {
                SavingsAccount acc5 = (SavingsAccount)acc3;
                acc5.UpdateBalance();
                Console.WriteLine("Updated");
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            BussinesAccount c1 = new BussinesAccount(0001, "Caique Pereira Alve", 10.000, 20.000);
            // UPCASTING

            Account c2 = c1;

            //other ex

            Account c4 = new BussinesAccount(0002, "Carlos Eduardo Pereira Alves", 50.000, 100.00);

            Account         C5 = new SavingsAccount(0003, "Paulo Henrique Pereira Alves", 100.000, 0.01);
            BussinesAccount c7 = new BussinesAccount(10, "Ivani Pereira Alves", 1000.00, 0.02);


            //Downcasting

            // BussinesAccount c6 = c4;
            // o copilador não pode converte explicitamente um Account para BussinesAcount
            // o compilador não sabe que iremos ter um OBJETO BussinesAcount
            // precisamos fazer um "casting" porem um casting de classe que se chama DownCasting
            // Que  ficaria ingual ao exemplo abaixo \/

            BussinesAccount c6 = (BussinesAccount)c4;

            c6.Loan(45.00); // demonstração que o Objeto está acessando os meótods da class BussinesAccount

            c4.Loan(1.00);  // o Objeto não acessa o método por que ela é um tipo Account, mesmo instânciado como BussinesAccount

            c7.Loan(1.00);  // o Objeto ja é um tipo BussinesAcount
        }
Exemple #3
0
        static void Main(string[] args)
        {
            /*  trabalhando com upcasting - converter a superclasse em subclasse
             * como a subclasse é uma superclasse é perfeitamente possível sem nenhum comando adicional */


            Account         acc1  = new Account(1001, "Alex", 0.0);
            BussinesAccount bacc1 = new BussinesAccount(1002, "Maria", 0.0, 500.00);

            // UpCasting

            Account acc2 = bacc1;
            // neste caso a variável criada é do tipo account, mas está instanciada como bussines account
            Account acc3 = new BussinesAccount(1003, "Bob", 0.0, 200.00);
            Account acc4 = new SavingsAccount(1004, "Tiago", 0.0, 0.01);

            // DownCasting

            BussinesAccount bcc2 = (BussinesAccount)acc3;

            bcc2.Loan(200);

            //  BussinesAccount bcc3 = (BussinesAccount)acc4;
            // testa se instanciaçaõ de acc4 é bussines account

            if (acc4 is BussinesAccount)
            {
                // BussinesAccount bcc3 = (BussinesAccount)acc4;

                // outra formar de fazer a conversão.
                BussinesAccount bcc3 = acc4 as BussinesAccount;
                bcc3.Loan(200.0);
                Console.WriteLine("Loan");
            }

            if (acc4 is SavingsAccount)
            {
                // SavingsAccount Sacc1 = (SavingsAccount)acc4;
                SavingsAccount Sacc1 = acc4 as SavingsAccount;
                Sacc1.UpdateBalance();
                Console.WriteLine("Update");
            }


            Console.ReadLine();
        }