Constructor (object-oriented programming): Difference between revisions

Content deleted Content added
Reverting edit(s) by 77.137.77.211 (talk) to rev. 1157671443 by Cngzz1: (RW 16.1)
m Small WP:COPYEDITs WP:EoS: clarify, WP:TERSE. MOS:FIRSTABBReviation clarify, define before WP:ABBR in parentheses. WP:LINKs: update-standardizes, needless WP:PIPEs > WP:NOPIPEs.
Line 84:
 
== Language details ==<!-- see also Category:Programming language comparisons -->
Constructors are implemented in different [[programming language]]s in various ways, including:
 
=== C++ ===
Line 186 ⟶ 187:
</syntaxhighlight>
 
=== ColdFusion Markup Language (CFML) ===
 
[[CFMLColdFusion Markup Language]] (CFML) uses a method named '<code>init</code>' as a constructor method.
 
'''Cheese.cfc'''
Line 443 ⟶ 444:
=== OCaml ===
 
In [[OCaml (programming language)|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>[http://caml.inria.fr/pub/docs/manual-ocaml/index.html OCaml manual]</ref>
 
Line 487 ⟶ 488:
=== Perl 5 ===
 
In [[Perl|Perl programming language]] version 5, by default, constructors are [[factory method]]s, that is, methods that create and return the object, concretely meaning create and return a blessed reference. A typical object is a reference to a hash, though rarely references to other types are used too. By convention the only constructor is named ''new'', though it is allowed to name it otherwise, or to have multiple constructors. For example, a Person class may have a constructor named ''new'', as well asand a constructor ''new_from_file'' which reads a file for Person attributes, and ''new_from_person'' which uses another Person object as a template.
 
<syntaxhighlight lang="perl">
Line 522 ⟶ 523:
==== Perl 5 with Moose ====
 
WithIn the [[Moose perl|Moose object system]] for Perl, most of this boilerplate can be left outomitted, a default ''new'' is created, attributes can be specified, as well asand whether they can be set, reset, or are required. In addition, any extra constructor functionality can be included in a ''BUILD'' method which the Moose generated constructor will call, after it has checked the arguments. A ''BUILDARGS'' method can be specified to handle constructor arguments not in hashref / key => value form.
 
<syntaxhighlight lang="perl">
Line 581 ⟶ 582:
=== Raku ===
 
WithIn [[Raku (programming language)|Raku]], even more boilerplate can be left outomitted, given that a default ''new'' method is inherited, attributes can be specified, as well asand whether they can be set, reset, or are required. In addition, any extra constructor functionality can be included in a ''BUILD'' method which will get called to allow for custom initialization. A ''TWEAK'' method can be specified to post-process any attributes already (implicitly) initialized.
 
<syntaxhighlight lang="perl6">