• 构造函数中base与this的区别


    base是对父类的引用,而this是对类本身的引用。

    namespace ConsoleApplication1
    {
        public class BaseClass
        {
            private string name;
            private int age;
            private string addr;
            public BaseClass()
            {
                Console.WriteLine("No Info");
            }
            public BaseClass(string name)
            {
                this.name = name;
                Console.WriteLine("name=" + this.name);
            }
            public BaseClass(string name, int age)
            {
                this.name = name;
                this.age = age;
                Console.WriteLine(string.Format("name={0},age={1}" ,this.name,this.age));
            }
    
            public BaseClass(string name, int age,string addr)
            {
                this.name = name;
                this.age = age;
                this.addr = addr;
                Console.WriteLine(string.Format("name={0},age={1},addr={2}", this.name, this.age, this.addr));
            }
        }
    
        public class ItemClass : BaseClass
        {
            private string name;
            private int age;
    
            public ItemClass()
                : this("demo")
            {
                //Console.WriteLine("No Info");
            }
            public ItemClass(string name)
                : this("jimmy", 20)
            {
                this.name = name;
                Console.WriteLine("name=" + this.name);
            }
            public ItemClass(string name, int age)
            {
                this.name = name;
                this.age = age;
                Console.WriteLine(string.Format("name={0},age={1}", this.name, this.age));
            }
            public ItemClass(string name,int age,string addr)
                : base("jeason", 20,"shanghai")
            {
                Console.WriteLine("name=" + name);
            }
    
            public static void Main()
            {
                ItemClass ic1 = new ItemClass("mike");
                ItemClass ic2 = new ItemClass();
                ItemClass ic3 = new ItemClass("mike",21,"chengdu");
            }
        }
    } 

    子类与基类的构造函数执行顺序:

    先调用父类的构造函数进行初始化,再调用继承类的构造函数,如果没有在继承类中指明父类的构造函数,则默认调用父类中无参构造函数,然后调用继承类的构造函数。 

  • 相关阅读:
    win32/mfc/qt 异常处理与总结
    tornado使用长连接并运行在nginx反向代理后面
    tornado中计算etag的一个BUG
    关于HTTP长连接的保持和断开
    系统时间不对导致cookie设置不成功
    tornado中模板的变量命令冲突
    unix时间戳转换
    Twisted echo server
    tornado长连接断开的处理机制
    rp_filter及Linux下多网卡接收多播的问题
  • 原文地址:https://www.cnblogs.com/chenh/p/7895592.html
Copyright © 2020-2023  润新知