Object Oriented Programming Basics – Arma Reforger
Lou Montana (talk | contribs) m (Text replacement - "overriden" to "overridden") |
Lou Montana (talk | contribs) (Fix examples) |
||
(One intermediate revision by the same user not shown) | |||
Line 19: | Line 19: | ||
class MyClass | class MyClass | ||
{ | { | ||
protected int m_iValue = | protected int m_iValue = 100; | ||
void MyClass(int value) | void MyClass(int value) | ||
Line 54: | Line 54: | ||
{ | { | ||
Print("MyMethod with 1 int argument"); | Print("MyMethod with 1 int argument"); | ||
} | |||
void MyMethod(int a, int b = 0) // error: "identical" to the above method due to the optional parameter | |||
{ // both can be called MyMethod(42) | |||
Print("MyMethod with 1 or 2 int argument(s)"); | |||
} | } | ||
Line 92: | Line 97: | ||
class MyClass | class MyClass | ||
{ | { | ||
int Health = 100; // bad - public access | |||
} | } | ||
class MyClass | class MyClass | ||
{ | { | ||
protected int m_iHealth; | protected int m_iHealth; // good - getter and setter allow for evolution | ||
int GetHealth() // health getter | int GetHealth() // health getter | ||
Line 112: | Line 112: | ||
{ | { | ||
m_iHealth = health; | m_iHealth = health; | ||
} | |||
void MyClass() // constructor method | |||
{ | |||
m_iHealth = 100; | |||
} | } | ||
} | } | ||
Line 186: | Line 191: | ||
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(); // | MyClass instance = new MyClass(); // error: argument is missing | ||
</enforce> | </enforce> | ||
=== Destructor === | === 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 method is a specific one: it is a method that is automatically called on object destruction, and exists only without arguments.<br> | ||
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 {{hl|~}}. | A destructor is declared as a method having the same name as its class, starting with a tilde {{hl|~}}. | ||
Line 349: | Line 355: | ||
child.TheMethod(); // outputs "Child Method" | child.TheMethod(); // outputs "Child Method" | ||
</enforce> | </enforce> | ||
{{Feature|important| | |||
An overridden method must have the '''exact same signature''' as the overridden method - up to the parameter names. | |||
<enforce> | |||
int MyMethod(int value1, int value2); | |||
// overrides | |||
override int MyMethod(int value1, int value2); // works | |||
override int MyMethod(int value1, string value2); // does not work - parameter types mismatch | |||
override int MyMethod(int valueA, int valueB); // does not work - parameter names mismatch | |||
</enforce> | |||
}} | |||
=== super === | === super === | ||
Line 410: | Line 428: | ||
{{GameCategory|armaR|Modding|Guidelines | {{GameCategory|armaR|Modding|Scripting|Guidelines}} |
Latest revision as of 00:09, 16 June 2025
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: