Object Oriented Programming Basics – Arma Reforger
Lou Montana (talk | contribs) m (1 revision imported) |
Lou Montana (talk | contribs) m (Text replacement - "overriden" to "overridden") |
||
(9 intermediate revisions by 2 users not shown) | |||
Line 12: | Line 12: | ||
A member variable is a variable scoped to that object instance. | A member variable is a variable scoped to that object instance. | ||
It is usually {{hl|protected}} but can be {{hl|private}} (see {{ | It is usually {{hl|protected}} but can be {{hl|private}} (see {{Link|#Visibility}}); a public member is usually a bad practice - it is best to use {{Link|#Getter and Setter|Getters and Setters}}. | ||
See [[Arma Reforger:Scripting: Values]] for naming prefixes. | See [[Arma Reforger:Scripting: Values]] for naming prefixes. | ||
< | <enforce> | ||
class MyClass | class MyClass | ||
{ | { | ||
protected int m_iValue = -1; // -1 is undefined value | |||
void MyClass(int value) | void MyClass(int value) | ||
Line 26: | Line 26: | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
Line 33: | Line 33: | ||
A method is an object's function or a class' (static) function - it can be seen as a "member function". | A method is an object's function or a class' (static) function - it can be seen as a "member function". | ||
A method has a '''signature''', i.e a return type, a name and a parameters list - e.g: < | A method has a '''signature''', i.e a return type, a name and a parameters list - e.g: <enforce>int GetNumber(bool canBeNegative)</enforce> | ||
Two methods can be named identically as long as they differ by their parameters. | Two methods can be named identically as long as they differ by their parameters. | ||
< | <enforce> | ||
class MyClass | class MyClass | ||
{ | { | ||
Line 80: | Line 81: | ||
int staticResult = MyClass.MyStaticMethod(); // without instance usage, staticResult is 33 | int staticResult = MyClass.MyStaticMethod(); // without instance usage, staticResult is 33 | ||
</ | </enforce> | ||
=== Getter and Setter === | === Getter and Setter === | ||
Line 88: | Line 89: | ||
A Setter describes a method that '''sets''' it. | A Setter describes a method that '''sets''' it. | ||
< | <enforce> | ||
class MyClass | class MyClass | ||
{ | { | ||
Line 96: | Line 97: | ||
class MyClass | class MyClass | ||
{ | { | ||
protected int m_iHealth; | |||
void MyClass() // constructor method | void MyClass() // constructor method | ||
Line 118: | Line 119: | ||
class MyClass | class MyClass | ||
{ | { | ||
protected int m_iHeadHealth; | |||
protected int m_iBodyHealth; | |||
void MyClass() // constructor method | void MyClass() // constructor method | ||
Line 143: | Line 144: | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
=== Constructor === | === Constructor === | ||
Line 153: | Line 154: | ||
{{Feature|informative| | {{Feature|informative| | ||
The parent class constructor (see the {{Link|#Inheritance}} chapter below) is automatically called. | |||
An inheriting class can only '''add''' new arguments to those accepted by the base class constructor. | |||
}} | }} | ||
< | <enforce> | ||
class MyClass | class MyClass | ||
{ | { | ||
Line 166: | Line 168: | ||
m_iValue = 50; | m_iValue = 50; | ||
} | } | ||
} | } | ||
MyClass instance = new MyClass(); // instance.m_iValue = 50 | MyClass instance = new MyClass(); // instance.m_iValue = 50 | ||
</ | </enforce> | ||
< | <enforce> | ||
class MyClass | class MyClass | ||
{ | { | ||
Line 178: | Line 180: | ||
void MyClass(int value) | void MyClass(int value) | ||
{ | { | ||
PrintFormat("Instance created with value %1", m_iValue); | |||
m_iValue = value; | m_iValue = value; | ||
} | } | ||
} | } | ||
MyClass instance = new MyClass(42); // prints "Instance created with value 42" | MyClass instance = new MyClass(42); // prints "Instance created with value 42" | ||
MyClass instance = new MyClass(); // errors: argument is missing | MyClass instance = new MyClass(); // errors: argument is missing | ||
</ | </enforce> | ||
=== Destructor === | === Destructor === | ||
Line 191: | Line 193: | ||
A destructor method is a specific one: it is a method that is automatically called on object destruction, and exists only without arguments. There can be zero to one destructor. | A destructor method is a specific one: it is a method that is automatically called on object destruction, and exists only without arguments. There can be zero to one destructor. | ||
A destructor is declared as a method having the same name as its class, starting with a | A destructor is declared as a method having the same name as its class, starting with a tilde {{hl|~}}. | ||
< | <enforce> | ||
class MyClass | class MyClass | ||
{ | { | ||
Line 204: | Line 206: | ||
MyClass instance = new MyClass(); | MyClass instance = new MyClass(); | ||
delete instance; // prints "I am being destroyed!"; | delete instance; // prints "I am being destroyed!"; | ||
</ | </enforce> | ||
Line 215: | Line 217: | ||
This is the default visibility - the member is accessible from inside as well as outside the object. As the default visibility, it does not need a keyword. | This is the default visibility - the member is accessible from inside as well as outside the object. As the default visibility, it does not need a keyword. | ||
< | <enforce noguess> | ||
class ParentClass | class ParentClass | ||
{ | { | ||
Line 234: | Line 236: | ||
ChildClass child = new ChildClass(); | ChildClass child = new ChildClass(); | ||
child.Health = 10; // OK | child.Health = 10; // OK | ||
</ | </enforce> | ||
=== protected === | === protected === | ||
Line 243: | Line 245: | ||
{{Feature|important|It is important to keep visibility to {{hl|protected}} for modding purpose - an inherited class can then expand or reuse these methods.}} | {{Feature|important|It is important to keep visibility to {{hl|protected}} for modding purpose - an inherited class can then expand or reuse these methods.}} | ||
< | <enforce> | ||
class ParentClass | class ParentClass | ||
{ | { | ||
Line 264: | Line 266: | ||
child.m_iHealth = 10; // error: cannot access from the outside | child.m_iHealth = 10; // error: cannot access from the outside | ||
child.SetHealth(10); // OK - the child can internally edit the inherited member | child.SetHealth(10); // OK - the child can internally edit the inherited member | ||
</ | </enforce> | ||
=== private === | === private === | ||
Line 272: | Line 274: | ||
It uses the {{hl|private}} keyword. | It uses the {{hl|private}} keyword. | ||
< | <enforce> | ||
class ParentClass | class ParentClass | ||
{ | { | ||
Line 289: | Line 291: | ||
parent.m_iHealth = 10; // error: cannot access from the outside | parent.m_iHealth = 10; // error: cannot access from the outside | ||
parent.SetHealth(10); // error: the SetHealth method is a member of ChildClass, not ParentClass | parent.SetHealth(10); // error: the SetHealth method is a member of ChildClass, not ParentClass | ||
</ | </enforce> | ||
Line 296: | Line 298: | ||
Inheritance is the transmission of parent properties to a child class. Class inheritance is written with {{hl|:}}. A class can only inherit from '''one''' class. | Inheritance is the transmission of parent properties to a child class. Class inheritance is written with {{hl|:}}. A class can only inherit from '''one''' class. | ||
< | <enforce> | ||
class ParentClass | class ParentClass | ||
{ | { | ||
Line 319: | Line 321: | ||
ChildClass child = new ChildClass(); | ChildClass child = new ChildClass(); | ||
child.ParentMethod(); // outputs "Parent Method" | child.ParentMethod(); // outputs "Parent Method" | ||
child.ChildMethod(); // outputs "Child Method"</ | child.ChildMethod(); // outputs "Child Method"</enforce> | ||
=== override === | === override === | ||
A '''non-private''' (protected or public) inherited method can be overridden thanks to the {{hl|override}} keyword: | A '''non-private''' (protected or public) inherited method can be overridden thanks to the {{hl|override}} keyword: | ||
< | <enforce> | ||
class ParentClass | class ParentClass | ||
{ | { | ||
Line 346: | Line 348: | ||
ChildClass child = new ChildClass(); | ChildClass child = new ChildClass(); | ||
child.TheMethod(); // outputs "Child Method" | child.TheMethod(); // outputs "Child Method" | ||
</ | </enforce> | ||
=== super === | === super === | ||
An inherited object can call its parent's '''non-private''' (protected or public) method: | An inherited object can call its parent's '''non-private''' (protected or public) method: | ||
< | <enforce> | ||
class ParentClass | class ParentClass | ||
{ | { | ||
Line 374: | Line 376: | ||
ChildClass child = new ChildClass(); | ChildClass child = new ChildClass(); | ||
child.TheMethod(); // outputs "ParentMethodA" then "Child MethodB" | child.TheMethod(); // outputs "ParentMethodA" then "Child MethodB" | ||
</ | </enforce> | ||
It can even call an | It can even call an overridden one: | ||
< | <enforce> | ||
class ParentClass | class ParentClass | ||
{ | { | ||
Line 400: | Line 402: | ||
ChildClass child = new ChildClass(); | ChildClass child = new ChildClass(); | ||
child.TheMethod(); // outputs "Parent Method" then "Child Method" | child.TheMethod(); // outputs "Parent Method" then "Child Method" | ||
</ | </enforce> | ||
== | == See Also == | ||
* [[Arma Reforger:Object Oriented Programming Advanced Usage|Object Oriented Programming Advanced Usage]] | |||
{{GameCategory|armaR|Modding| | {{GameCategory|armaR|Modding|Guidelines|Scripting}} |
Latest revision as of 14:36, 9 November 2023
Class vs Object
A class is a definition of an object; it can be seen as the blueprint for an object's creation. An object is an instance of a class, as in an entity created following the class' specifics.
An object can hold values, known as member variables, and functions, known as methods.
A class can have variables and methods too, known as static variables and methods.
Member Variable
A member variable is a variable scoped to that object instance. It is usually protected but can be private (see Visibility); a public member is usually a bad practice - it is best to use Getters and Setters.
See Arma Reforger:Scripting: Values for naming prefixes.
Method
A method is an object's function or a class' (static) function - it can be seen as a "member function".
A method has a signature, i.e a return type, a name and a parameters list - e.g:
Two methods can be named identically as long as they differ by their parameters.
Getter and Setter
A Getter describes a method that gets the value of a property;
A Setter describes a method that sets it.
Constructor
A constructor method is a specific one: it is a method that is automatically called on object instanciation, and can be with or without arguments.
There can be zero to one constructor.
A constructor is declared as a method having the same name as its class.
Destructor
A destructor method is a specific one: it is a method that is automatically called on object destruction, and exists only without arguments. There can be zero to one destructor.
A destructor is declared as a method having the same name as its class, starting with a tilde ~.
Visibility
Visibility is the accessibility of an object's or class' method/variable from the "outside" of that object/class.
public
This is the default visibility - the member is accessible from inside as well as outside the object. As the default visibility, it does not need a keyword.
protected
This is a "hierarchy" visibility - the member is accessible from the object and objects of inheriting classes. It uses the protected keyword.
private
This is the strictest visibility - only the object can access this member. This is useful to e.g cut code in smaller methods and not clutter the list of available methods on this object from the outside. It uses the private keyword.
Inheritance
Inheritance is the transmission of parent properties to a child class. Class inheritance is written with :. A class can only inherit from one class.
override
A non-private (protected or public) inherited method can be overridden thanks to the override keyword:
super
An inherited object can call its parent's non-private (protected or public) method:
It can even call an overridden one: