zoukankan      html  css  js  c++  java
  • c# base 和this 继承

    父类的构造函数总是在子类之前执行的。既先初始化静态构造函数,后初始化子类构造函数。

    复制代码
    复制代码
    public class BaseCircle {
            public BaseCircle()
            {
                Console.WriteLine(" no arguments base constructor!!!");
            }
            public BaseCircle(double arg)
            {
                Console.WriteLine("double arg base constructor!!!");
            }
        }
     
        public class SubCircle : BaseCircle {
            public SubCircle():base()
            {
                Console.WriteLine("sub class no argument constructor,actually call base constructor !!!");
            }
     
            public SubCircle(double a):base(a)
            {
                Console.WriteLine("sub class with argument, actually call base double constructor!!!");
            }
     
            public SubCircle(int k):this(1,2)
            {
                Console.WriteLine("sub class with argument int k, actually call sub class constructor int i & j !!!");
            }
     
            public SubCircle(int i,int j)
            {
                Console.WriteLine("sub class with int i&j argument!!!!");
            }
        }
     
    static void Main(string[] args)
            {
                SubCircle s1 = new SubCircle();
                SubCircle s2 = new SubCircle(1.1);
                SubCircle s3 = new SubCircle(1);
    }
    复制代码
    复制代码
    复制代码
    复制代码
    输出结果:
    no arguments base constructor!!!
    sub class no argument constructor,actually call base constructor !!!
     
    double arg base constructor!!!
    sub class with argument, actually call base double constructor!!!
     
     no arguments base constructor!!!
    sub class with int i&j argument!!!!
    sub class with argument int k, actually call sub class constructor int i & j !!!
    复制代码
    复制代码

    用法二:                                                                                                                                                                                                                        

    是不是很模糊这两个关键字那?


    哈,现在我来写份代码,代码可是最有说服力的啦!

    复制代码
    复制代码
     class BaseClass
        {
            private int numA;
            public BaseClass()
            {
                Console.WriteLine("基类的无参数构造函数. value:{0}", numA);
            }
            public BaseClass(int i)
            {
                this.numA = i;
                Console.WriteLine("基类带一个参数的构造函数. value:{0}", numA);
            }
        }
        class ChildClassA : BaseClass
        {
            private int numB;
            public ChildClassA()
            {
                Console.WriteLine("子类无参数构造函数. value:{0}", numB);
            }
            public ChildClassA(int i)
            {
                this.numB = i;
                Console.WriteLine("子类带有一个参数的构造函数. value:{0}", numB);
            }
            public ChildClassA(int i, int j)
                : base(i)
            {
                this.numB = j;
                Console.WriteLine("子类带有两个参数的构造函数. value:{0}", numB);
            }
        }
        class ChildClassB : BaseClass
        {
            private int numB;
            public ChildClassB()
            {
                Console.WriteLine("子类无参数构造函数. value:{0}", numB);
            }
            public ChildClassB(int i)
            {
                this.numB = i;
                Console.WriteLine("子类带有一个参数的构造函数. value:{0}", numB);
            }
            public ChildClassB(int i, int j)
                : this(i)
            {
                this.numB = j;
                Console.WriteLine("子类带有两个参数的构造函数. value:{0}", numB);
            }
        }
        class Demo
        {
            static void Main(string[] args)
            {
                Console.WriteLine("使用base
    ");
                ChildClassA a = new ChildClassA(2, 4);
                Console.WriteLine();
                Console.WriteLine("----------------------------------------
    ");
                Console.WriteLine("使用this
    ");
                ChildClassB b = new ChildClassB(2, 4);
                Console.ReadKey();
            }
        }
    复制代码
    复制代码

    执行的结果如下:

    复制代码
    复制代码
    --------------------------------结果----------------------------------
     
    使用base
     
    基类带一个参数的构造函数. value:2
    子类带有两个参数的构造函数. value:4
     
    ----------------------------------------
     
    使用this
     
    基类的无参数构造函数. value:0
    子类带有一个参数的构造函数. value:2
    子类带有两个参数的构造函数. value:4
     
    --------------------------------结果--------------------------------
    复制代码
    复制代码

    this只是调用本身,但是这样是需要调用一次基类没有参的构造函数,所以会多显示一条“基类的无参数构造函数. value:0”。

    base是调用基类的有参数构造函数。

  • 相关阅读:
    vs2010 + .net3.5 MSCharts使用介绍与例子
    TFS服务连接TF31002 出错
    SharePoint CAML 通过时间查询
    SharePoint2010项目总结汇总
    jquery 获取和设置 select下拉框的值
    How to Create Multilingual Webpart in SharePoint 2010 (C# 方式)
    sharepoint母版页固定宽度与纵向滚动条靠右边(修改版)
    JavaScript进行GET和POST请求
    端口简介大全
    程序员学习能力提升三要素
  • 原文地址:https://www.cnblogs.com/HKKD/p/7015862.html
Copyright © 2011-2022 走看看