Content deleted Content added
Reverting edit(s) by 86.121.176.122 (talk) to rev. 1289177642 by Explicit: Vandalism (UV 0.1.6) |
Filled in 6 bare reference(s) with reFill 2 |
||
Line 62:
* [[Java (programming language)|Java]], [[C++]], [[C Sharp (programming language)|C#]], [[ActionScript]], {{nowrap|[[PHP]] 4}}, and [[MATLAB]] have a naming convention in which constructors have the same name as the class with which they are associated.
* In PHP 5, a recommended name for a constructor is <code>__construct</code>. For backwards compatibility, a method with the same name as the class will be called if <code>__construct</code> method can not be found. Since PHP 5.3.3, this works only for non-namespaced classes.<ref name="php5cpnstructor">[http://www.php.net/manual/en/language.oop5.decon.php Constructors and Destructors], from PHP online documentation</ref>
* In PHP 7, you should always name the constructor as <code>__construct</code>. Methods with the same name as the class will trigger an E_DEPRECATED level error.<ref name="php5cpnstructor"
* In [[Perl]], constructors are, by convention, named "new" and have to do a fair amount of object creation.
* In [[Moose perl|Moose object system]] for Perl, constructors (named ''new'') are automatically created and are extended by specifying a ''BUILD'' method.
Line 145:
==== C# static constructor ====
In [[C Sharp (programming language)|C#]], a ''static constructor'' is a static data initializer.<ref name="Albahari">{{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |publisher= O'Reilly |isbn= 978-1-098-12195-2}}</ref>{{rp|111-112}} Static constructors are also called ''class constructors''. Since the actual method generated has the name ''.cctor'' they are often also called "cctors".<ref>{{cite web|url=http://ericlippert.com/2013/02/06/static-constructors-part-one/ |title=Fabulous Adventures in Coding |publisher=Eric Lippert |date=2013-02-06|access-date=2014-04-05}}</ref><ref>{{cite book|url=https://books.google.com/books?id=oAcCRKd6EZgC&pg=PA222 |title=Expert .NET 2.0 IL Assembler |publisher=APress |date=2006-01-01|isbn=9781430202233 |access-date=2014-04-05}}</ref>
Static constructors allow complex static variable initialization.<ref>
Static constructors are called implicitly when the class is first accessed. Any call to a class (static or constructor call), triggers the static constructor execution.
Static constructors are [[thread safe]] and implement a [[singleton pattern]]. When used in a [[generic programming]] class, static constructors are called at every new generic instantiation one per type.<ref name=Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}</ref>{{rp|38}}<ref name="Albahari
<syntaxhighlight lang="csharp">
Line 225:
* Creation procedures must leave the newly initialized object in a state that satisfies the class invariant.{{Efn|Because the inherited class invariant must be satisfied, there is no mandatory call to the parents' constructors.}}
Although object creation involves some subtleties,<ref name="eiffel standard">
* Create a new direct instance of type <code lang="eiffel">T</code>.{{Efn|The Eiffel standard requires fields to be initialized on first access, so it is not necessary to perform default field initialization during object creation.}}
Line 322:
Java permit users to call one constructor in another constructor using <code>this()</code> keyword.
But <code>this()</code> must be first statement. <ref>
<syntaxhighlight lang="java">
Line 428:
=== OCaml ===
In [[OCaml]], there is one constructor. Parameters are defined right after the class name. They can be used to initialize instance variables and are accessible throughout the class. An anonymous hidden method called <code>initializer</code> allows to evaluate an expression immediately after the object has been built.<ref>
<syntaxhighlight lang="ocaml">
Line 536:
=== Python ===
In [[Python (programming language)|Python]], constructors are defined by one or both of <code>__new__</code> and <code>__init__</code> methods. A new instance is created by calling the class as if it were a function, which calls the <code>__new__</code> and <code>__init__</code> methods. If a constructor method is not defined in the class, the next one found in the class's Method Resolution Order will be called.<ref name="auto">
In the typical case, only the <code>__init__</code> method need be defined. (The most common exception is for immutable objects.)
Line 557:
</syntaxhighlight>
Classes normally act as [[Factory (object-oriented programming)|factories]] for new instances of themselves, that is, a class is a callable object (like a function), with the call being the constructor, and calling the class returns an instance of that class. However the <code>__new__</code> method is permitted to return something other than an instance of the class for specialised purposes. In that case, the <code>__init__</code> is not invoked.<ref
=== Raku ===
|