A()(构造函数)和A():base()之间的区别 [英] Difference between A() (constructor ) and A():base()

查看:187
本文介绍了A()(构造函数)和A():base()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查...
以下代码之间有什么区别...

1.

Please check it...
what is the difference between the following codes...

1.

class A
   {
       public A()
       {
           Console.WriteLine("inside the  constructor A");
       }
   }
 class B:A
    {
        public B()
        {
            Console.WriteLine("inside the  constructor B");
        }
    }

main()
{
B objb=new B();
}



2.



2.

class A
   {
       public A()
       {
           Console.WriteLine("inside the  constructor A");
       }
   }
 class B:A
    {
        public B():base()    //here is the change, it is calling the constructor !!
        {
            Console.WriteLine("inside the  constructor B");
        }
    }

main()
{
B objb=new B();
}



两者都在调用构造函数.构造函数的执行也与首先执行A()然后执行B()相同. 那么B():base()的额外功能是什么?



Both are calling the constructor.And the execution of constructor also same like first A() and then B() ..
then what is the extra functionality of B():base() ??

推荐答案

在您的示例中,没有区别.
但是,可以使用base关键字来确定要调用的构造函数.
In your example, there is no difference.
However the base keyword can be used to decide which constructor to call.
class A
   {
       public A()
       {
           Console.WriteLine("inside the  constructor A");
       }

       public A(int i)
       {
           Console.WriteLine("inside constructor(2) A");
       }

   }
 class B:A
    {
        public B():base(1)
        {
            Console.WriteLine("inside the  constructor B");
        }
    }



在这里,您指定要调用类A的第二个构造函数(带有参数的那个).

您可以在此处阅读并找到有关此关键字的更多示例- http://msdn .microsoft.com/en-us/library/hfw7t1ce(v = vs.100).aspx [



Here you are specifying that the second constructor of class A (the one with the parameter) be called.

You can read and find more examples about this keyword here - http://msdn.microsoft.com/en-us/library/hfw7t1ce(v=vs.100).aspx[^].


使用A():base()的想法是给基类提供一些参数,该参数由基类的构造函数初始化.在此示例中,无需调用没有任何参数的基类构造函数cuz :)
例如,您可以
The idea of using A():base() is to give some parameter to the base class to be initialized by the constructor of the base class. In this example there is no need of calling the base class constructor cuz you don''t have any parameters:)
For example you can
public B(int a,int b):base(b)
{
 //some initialization here
}


并致电


and to call

B obj=new B(c,d);
//where c and d are int


在这里,您的参数d将由基类(A)的构造函数初始化. 顺便说一句,要注意如何继承类.在通常情况下,您是这样继承的:


here your parameter d will be initialized by the constructor of the base class(A)
And btw be careful how you inherit classes . In the common case you inherit like this

class B:public A


它可以是私有的也可以受保护,它将改变基类中成员变量的访问级别.


It can be private and protected too and it will change the access levels for member variables from the base class


解决方案1、2和3确实非常好,并且可以很好地解释这种情况.
当基类中没有parameterless 构造函数时,我想添加base indispensable.
案例1:
Solution 1, 2 and 3 are really very good and explain the case very well.
I want to add that base is indispensable, when there is no parameterless constructor in the base class.
Case 1:
void Main()
{
    B objb=new B(1);
}
class A
{
   public A(int id)
   {
        //Do something with id
       Console.WriteLine("inside the  constructor A");
   }
}
class B:A
{
    public B(int id)
    {
        Console.WriteLine("inside the  constructor B");
    }
}


这将无法编译.并抛出错误,提示 Class A不包含带有0个参数的构造函数.

情况2:


This will not compile. And throws error that Class A does not contain a constructor that takes 0 arguments.

Case 2:

void Main()
{
    B objb=new B(1);
}
class A
{
   public A(int id)
   {
        //Do something with id
       Console.WriteLine("inside the  constructor A");
   }
}
class B:A
{
    public B(int id):base(id)
    {
        Console.WriteLine("inside the  constructor B");
    }
}


这会编译


This compiles


这篇关于A()(构造函数)和A():base()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆