bloggerads

2013年11月26日 星期二

C# : Class inherits and virtual (override) example


    public class Father
    {
        public Father() { Console.WriteLine("Father Constructor!"); }
        public virtual void WifeAge() { Console.WriteLine("Age is 50, father's wife!"); }
        public void FamilyName() { Console.WriteLine("Family name: Lee"); }
        ~Father() { Console.WriteLine("Father De-Constructor!"); }
        
    }
    public class Son : Father
    {
        public Son() { Console.WriteLine("Son Constructor!"); }
        public virtual void WifeAge() { Console.WriteLine("Age is 30, son's wife!"); }
        ~Son() { Console.WriteLine("Son De-Constructor!"); }
        
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            Father obj = new Father();
            obj.WifeAge();
            Son obj2 = new Son();
            obj2.WifeAge();
            obj.FamilyName();
            obj2.FamilyName();
        }
    }
_/_/_/_/_/_/ _/  OUTPUT  _/_/_/_/_/_/_/_/

Father Constructor!
Age is 50, father's wife!
Father Constructor!
Son Constructor!
Age is 30, son's wife!
Family name: Lee
Family name: Lee
Son De-Constructor!
Father De-Constructor!
Father De-Constructor!

沒有留言:

張貼留言