dqfkd82886 2017-07-04 15:47
浏览 69
已采纳

究竟建设者究竟做了什么? [关闭]

Yesterday I was reading PHP classes and suddenly something got weird to me. And that was about constructors in C# and PHP.

My question is what is constructor? Isn't it a special methods, used when instantiating a class that can does something like a method and take necessary resources for class like Memory and etc? And if it is, for example when we have a parent class named A which we derive another class named B and create an instance of B and call the B constructor and send some arguments to its parent (A) use :base(args.) (in C#) and use parent::__construct(args) (in PHP) , so is it goes and call the parent constructor , we don't want to exactly take resources for the parent but we just call the parent constructor which do it's for us. What happen exactly here?

I guess in this case simply just called it like a function! :\ i don't know i made my self clear or not ... Ask me if there is any Ambiguity, and Thanks ;)

UPDATED Thank by giving me the negative vote by the way ... look at this link : Microsoft Docs as far i read here it's something more than a simple function which hold a block of codes.(but with differences which you said , no return value and etc.) if it is just a function which called when an instance get created so why if we make it private then we can not make any instance of it anymore? why when we wanted to create a class add a () at the end of the class name?and when we want to pass any args to constructor use those () to send ? [Person new_person = new Person();] if you think also this a question which doesn't show any effort , vote negative again :D

  • 写回答

2条回答 默认 最新

  • douzhulan1815 2017-07-04 16:02
    关注

    A constructor is basically just a function. The difference between a normal function and a constructor is, that a Constructor

    1. Doesn't have a return value (not even void)
    2. Is named like the class itself (or has a predefined special name)
    3. Is classified as a mandatory function that has to be executed before the object is in an usable state
    4. Can not be called like other functions

    The constructor will be called automatically when creating a new instance of the object.

    Update:

    To understand why you can't create a new instance of something with a private constructor (from outside) look at it as a normal function for a moment.
    If you want to call new ClassWithPrivateConstructor() from outside of said class it will not work because the you do not have access to the constructor.

    You may now think that a private constructor is useless. But that's not the case. There are valid cases. Eg. singleton Objects.

    And for the () think of a constructor as a normal function again. You want to call the constructor. Maybe even with arguments. That's why you need the () when creating a new instance of an object.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大