virtual\interface\abstract Class

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace ConsoleApplication2
  6{
  7    class Program
  8    {
  9        static void Main(string[] args)
 10        {
 11            //can't create a instant of abstract class event it didn't contain any abstract method
 12            //interface is so circumstance !
 13            //absClass a = new absClass();
 14            
 15            child c = new child();
 16            //不可能new parent001()因为parent001是抽象类
 17            parent001 p = new child();
 18
 19            
 20            //p actualy is an object of child(),the later one implemented 
 21            //the virtual method Vir() inherited from its base class --one abstract class
 22            p.Vir();
 23            
 24            //although the p is an object of child ,it is asigned as parent001.
 25            //it is for child:Parent001
 26            //SO c inherits parent001's method.
 27            //p.instan() can work properly
 28            p.instan();
 29            
 30            
 31            //c implents Iface's method method1()
 32            //so c.method1() can work properly
 33            c.method1();
 34            
 35            Console.WriteLine("************");
 36            //c.Vir();
 37            Console.ReadLine();
 38        }

 39    }

 40    public abstract class absClass
 41    {
 42        public string str = "";
 43    }

 44    /// <summary>
 45    /// <para>abstract Class can't be Instantiate</para>
 46    /// </summary>

 47    public abstract class parent001
 48    {
 49        //只能用public才能被子类的实例对象继承得到
 50        public string str = "str";
 51
 52        private string _str2 = "";
 53        public string str2
 54        {
 55            get {
 56                return this._str2;
 57            }

 58            set {
 59                this._str2 = value;
 60            }

 61        }

 62        public virtual void Vir()
 63        {
 64            Console.WriteLine("me,parent virtual");
 65        }

 66        public void instan()
 67        {
 68            Console.WriteLine("me instant");
 69        }

 70        public void comman()
 71        {
 72            Console.WriteLine("I'm a acomman method");
 73        }

 74
 75        //abstract method can't have Body,even { }
 76        //Console.WriteLine("I'm a abstract method");
 77        public abstract void absMethod();
 78        
 79
 80    }

 81    /// <summary>
 82    /// <para>For test interface been implement</para>
 83    /// </summary>

 84    public interface Iface
 85    {
 86        void method1();
 87        void  method2();
 88    }

 89    /// <summary>
 90    /// <para>only to test the parent001 been cut off abstract</para>
 91    /// </summary>

 92    //public  class parent001
 93    //{
 94    //    protected string str = "str";
 95
 96    //    public virtual void Vir()
 97    //    {
 98    //        Console.WriteLine("me,parent virtual");
 99    //    }
100    //    public void instan()
101    //    {
102    //        Console.WriteLine("me instant");
103    //    }
104    //}
105
106    public class child : parent001,Iface
107    {
108        
109        public override void Vir()
110        {
111            base.Vir();
112            Console.WriteLine("1");
113            Console.WriteLine("me child");
114        }

115        /// <summary>
116        /// <para>
117        /// 重写时,自动产生base.Vir(),可能系统认为
118        /// “应该”调用“基类”的虚方法。 
119        /// </para>
120        /// </summary>

121        //public override void Vir()
122        //{
123        //    base.Vir();
124        //}
125        
126        /// <summary>
127        /// <para>method1()是实现的接口中的方法</para>
128        /// </summary>

129        public   void method1()
130        {
131            Console.WriteLine("me,implement the interface's first method!");
132        }

133        /// <summary>
134        /// <para>
135        /// method2() is the second method of Iface,each method should be implemented
136        /// </para>
137        /// </summary>

138        public void method2()
139        {
140            Console.WriteLine("I implement the interface's second method!");
141        }

142        /// <summary>
143        /// <para>
144        /// abstract method (inherited) must be implented also, 
145        /// and must be "override" this keyword must shown before
146        /// 
147        /// ms call override as "member modifier"
148        /// </para>
149        /// </summary>

150        public   override void absMethod()
151        {
152            Console.WriteLine("I implement the (abstract) (inherit) class parent001's abstract method!");
153        }

154    }

155
156
157}

158

posted on 2007-07-15 11:07  simhare  阅读(288)  评论(0编辑  收藏  举报

导航