• 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
  • 相关阅读:
    hdu 5365 Run(BC 50 B题)(求四边形的个数)
    浏览器网页推断手机是否安装IOS/Androidclient程序
    外包项目总结
    HDOJ 1534 Schedule Problem 差分约束
    JavaScript入门:003—JS中的变量
    关于产品的一些思考——(三十九)瀑布流载入
    从头认识java-15.7 Map(4)-介绍HashMap的工作原理-hash碰撞(常常作为面试题)
    CodeForce 424C Magic Formulas
    HDU 3400 Line belt (三分再三分)
    oracle跨数据库跨用户訪问注意事项
  • 原文地址:https://www.cnblogs.com/simhare/p/818583.html
Copyright © 2020-2023  润新知