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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab