欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

asp.net 构造函数 base 基类构造函数 父类构造函数 有大用 有大大用 有大大大用

C#构造函数里的base和this

父类的构造函数总是在子类之前执行的。 (如果没有base默认是调用父类的无参数的构造函数)


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 !!!


来自  https://www.cnblogs.com/lein-wang/archive/2013/05/23/4333575.html



C# 用Base方法调用基类构造函数

原文链接:http://blog.csdn.net/riyuedangkong1/article/details/52410695

public class BaseClass        //基类  
{  
    int Number;  
    public BaseClass()        //构造函数  
    {  
        Console.WriteLine("子类调用基类的第一个构造函数");  
    }  
    public BaseClass(int i)  
    {  
        Number = i;  
        Console.WriteLine("子类调用基类的第二个构造函数");  
    }  
    public int GetNumber()  
    {  
        return Number;  
    }  
}  
public class DerivedClass : BaseClass            //子类  
{  
    //这个构造函数调用第一个基类的构造函数  
    public DerivedClass()  
        : base()            //子类的构造函数  
    {  
    }  
    //这个构造函数调用第二个基类的构造函数  
    public DerivedClass(int i)  
        : base(i)           //子类的第二个构造函数  
    {  
    }  
    static void Main()  
    {  
        DerivedClass Bs1 = new DerivedClass();  
        DerivedClass Bs2 = new DerivedClass(1);  
    }  
}  

原文链接:http://www.bubuko.com/infodetail-43635.html

 public class Person
    {
        protected string name;
        protected string ID;
        public Person()
        {
            name = "zhouzhou";
            ID = "130552199301152555";
        }

        public virtual void GetInfo()
        {
            Console.WriteLine("姓名:{0}", name);
            Console.WriteLine("身份证号:{0}", ID);
        }
    }

    public class Student : Person     //声明student是person的子类
    {
        private string StudentNo = "123456";
        public Student()
            : base() //用base关键字调用基类的构造函数
        {
            Console.WriteLine("我的名字叫周周。");
        }
        public override void GetInfo()
        {
            base.GetInfo();     //用base关键字调用父类的方法
            Console.WriteLine("学号:{0}", StudentNo);
        }

        static void Main(string[] args)
        {
            Student zhouzhou = new Student();
            zhouzhou.GetInfo();
        }
    }

原文链接:https://zhidao.baidu.com/question/35782297.html
base()的意思是调用基类的构造函数……
public DerivedClass() : base() 的意思就是先调用基类的构造函数,然后执行DerivedClass() ‘
如果你改成
public DerivedClass() : base(1) 就看出区别了

来自  https://blog.csdn.net/zhruifei/article/details/77987687?utm_source=blogkpcl0


.Net C#语法 构造函数中this和base

  1. public class TestClassA  

  2. {  

  3.     public TestClassA()  

  4.     {  

  5.         Console.WriteLine("我是在TestClassA中的TestClassA()构造函数中");  

  6.     }  

  7.     public TestClassA(string a, string b)  

  8.     {  

  9.         Console.WriteLine("我是在TestClassA中的TestClassA(string a, string b)构造函数中");  

  10.     }  

  11.     public TestClassA(string a)  

  12.         : this(a, "b")  

  13.     {  

  14.         Console.WriteLine("我是在TestClassA中的TestClassA(string a): this(a, \"b\")构造函数中");  

  15.     }  

  16. }  

  17.   

  18. public class TestClassB : TestClassA  

  19. {  

  20.     public TestClassB()  

  21.     {  

  22.         Console.WriteLine("我是在TestClassB中的TestClassB()构造函数中");  

  23.     }  

  24.     public TestClassB(string a, string b)  

  25.         : base(a, b)  

  26.     {  

  27.         Console.WriteLine("我是在TestClassB中的TestClassB(string a, string b): base(a, b)构造函数中");  

  28.     }  

  29.   

  30.     public TestClassB(string a)  

  31.         : this(a, "b")  

  32.     {  

  33.         Console.WriteLine("我是在TestClassB中的TestClassB(string a, string b): this(a, \"b\")构造函数中");  

  34.     }  

  35. }  




  1. Console.WriteLine("TestClassA Father1 = new TestClassA();运行结果:");   

  2. TestClassA Father1 = new TestClassA();   

  3. Console.WriteLine();   

  4. Console.WriteLine("TestClassA Father2 = new TestClassA(\"f2\", \"f2\");运行结果:");   

  5. TestClassA Father2 = new TestClassA("f2""f2");   

  6. Console.WriteLine();   

  7. Console.WriteLine("TestClassA Father3 = new TestClassA(\"f3\");运行结果:");   

  8. TestClassA Father3 = new TestClassA("f3");   

  9. Console.WriteLine();  

  10. Console.WriteLine("TestClassB Son1 = new TestClassB();运行结果:");  

  11. TestClassB Son1 = new TestClassB();               

  12. Console.WriteLine();  

  13. Console.WriteLine("TestClassB Son2 = new TestClassB(\"s2\", \"s2\");运行结果:");  

  14. TestClassB Son2 = new TestClassB("s2""s2");  

  15. Console.WriteLine();               

  16. Console.WriteLine("TestClassB Son3 = new TestClassB(\"s3\");运行结果:");  

  17. TestClassB Son3 = new TestClassB("s3");  

  18. Console.WriteLine();  




分析:

this:调用的是本身,不能调用父类和子类的

base:调用父类的,不能调用本身的,但别人继承,可以调用

从中也可以得出另外个结果构造函数的运行过程 先从基类开始构造再到类本身

来自 https://www.cnblogs.com/dudumao/archive/2012/09/27/3339462.html


类的构造函数后使用base()是什么意思?

ispovo2005-11-14 03:56:24
例如:
public Car(int w, float g, int p):base(w, g)
{
wheels = w;
weight = g;
passengers = p
}

这里的base(w, g)表示什么意思?
w, g2个参数又有什么用?
1938点赞打赏收藏举报
 写回复
8 条回复
 切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wangfupeng1988 2010-06-05
先执行父类的对应的构造函数,再执行当前的构造函数。
zr1982930 2005-11-14
只是路过啊!
jujuxys 2005-11-14
补充一下,子类的父类可能有多个构造的重载,那么用base(param1,param2)可以显示的(显式的)实现父类相匹配的构造,在MSDN中有比较详细的说明,好像就叫显示(显式)构造
cjzlxy 2005-11-14
都说完了,我就不多嘴了,楼上说的对.
Overriding 2005-11-14
调用当前类的基类的构造函数。
jxufewbt 2005-11-14
楼上正解
mapserver 2005-11-14
用构造car的参数w, g去构造car的基类。
ispovo 2005-11-14
base(w, g)
标示引用基类中地构造函数中的参数?

来自  https://bbs.csdn.net/topics/80173088


普通分类: