Thứ Sáu, 16 tháng 4, 2010

// // 1 comment

Sự kế thừa trong C#

    Có 2 kiểu kế thừa trong lập trình hướng đối tượng là đơn kế thừa (kế thừa từ nhiều lớp) và đa kế thừa (kế thừa từ nhiều lớp). C# chỉ cung cấp mô hình đơn kế thừa. Điều này có nghĩa là bạn chỉ được phép kế thừa từ một lớp cơ sở (lớp cha). Nhưng thay vào đó thì một lớp có con có thể được kế thừa từ nhiều giao diện Interface.
    Ví dụ về kế thừa trong C#.

    /* Ví dụ về thừa kế trong lậ trình C# */
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace __OOP_Inheritance
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dog objDog = new Dog(4);
                objDog.displayProperties();
                Chicken objChicken = new Chicken(2);
                objChicken.displayProperties();
                Console.Read();
            }
        }

        class Animal
        {
            protected int ifoots;
            protected string sName;

            protected void setFoot(int ival)
            {
                ifoots = ival;
            }

            protected void setName(string sVal)
            {
                sName = sVal;
            }

            public void displayProperties()
            {
                Console.WriteLine(sName + " have " + ifoots.ToString()+ " foots");
            }
        }

        class Dog : Animal
        {
            public Dog(int ival)
            {
                setName("Dog");
                ifoots = ival;
            }
        }

        class Chicken : Animal
        {
            public Chicken(int ival)
            {
                setName("Chicken");
                setFoot(ival);
            }
        }
    }
    


    Ở ví dụ trên, Dog và Chicken là hai lớp kế thừa từ lớp Animal, do đó các thuộc tính như số chân, ifoots và tên sName đương nhiên xuất hiện trong hai lớp này và cho phép sử dụng. Tương tự, các hàm như setName(), setFoot(), displayProperties()  tại lớp Animal cũng được kế thừa xuống hai lớp Dog và Chicken. Do đó ta có thể gọi những hàm này, và kết quả hiển thị khi gọi hàm displayProperties()  theo đối tượng objDog và objChicken khác nhau như hình trên.

    Một lưu ý trong thừa kế đấy là Overriding method. Nếu một hàm được định nghĩa trong lớp con có cùng tên, kiểu với hàm trong lớp cha, khi ấy hàm trong lớp con sẽ overrides (làm ẩn) hàm trong lớp cha. Đó được gọi là overriding.

    Ví dụ về Overriding:

    /* Ví dụ về thừa kế,overrding trong lập trình C# */
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace __OOP_Inheritance
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dog objDog = new Dog(4);
                objDog.displayProperties();
                Chicken objChicken = new Chicken(2);
                objChicken.displayProperties();
                Tiger objTiger = new Tiger(4);
                objTiger.displayProperties();
                Console.Read();
            }
        }

        class Animal
        {
            protected int ifoots;
            protected string sName;

            protected void setFoot(int ival)
            {
                ifoots = ival;
            }

            protected void setName(string sVal)
            {
                sName = sVal;
            }

            public virtual void displayProperties() // chú ý hàm này
            {
                Console.WriteLine(sName + " has " + ifoots.ToString()+ " foots");
            }
        }

        class Dog : Animal
        {
            public Dog(int ival)
            {
                setName("Dog");
                ifoots = ival;
            }
        }

        class Chicken : Animal
        {
            public Chicken(int ival)
            {
                setName("Chicken");
                setFoot(ival);
            }

            public void displayProperties()
            {
                base.displayProperties();
                Console.WriteLine(sName + " have " + ifoots.ToString() + " foots (from Chicken class)");
            }
        }

        class Tiger : Animal
        {
            public Tiger(int ival)
            {
                setFoot(ival);
            }

            public override void displayProperties() // chú ý hàm này
            {
                Console.WriteLine("Tiger has " + ifoots.ToString()+ " foots");
            }
        }
    }
    

    Hàm displayProperties() trong lớp Tiger overrides hàm displayProperties() trong lớp Animal.

1 comments:

Nặc danh nói...

Bạn ơi Interface là chức năng không phải giao diện. Đơn kế thừa là kế thừa từ 1 lớp chứ không phải nhiều lớp.