Object Oriented Programming Advanced Usage – Arma Reforger
m (Fixed errors in examples) |
Lou Montana (talk | contribs) m (Text replacement - "</syntaxhighlight>" to "</enforce>") |
||
(One intermediate revision by the same user not shown) | |||
Line 7: | Line 7: | ||
Casting is the act of "presenting" a value as another type. For example, if a class hierarchy is {{hl|Animal > Dog > Cocker}}, a dog is an animal, a cocker is a dog (that is an animal), but a dog is not especially a cocker. | Casting is the act of "presenting" a value as another type. For example, if a class hierarchy is {{hl|Animal > Dog > Cocker}}, a dog is an animal, a cocker is a dog (that is an animal), but a dog is not especially a cocker. | ||
< | <enforce> | ||
class Animal {} | class Animal {} | ||
class Dog : Animal {} | class Dog : Animal {} | ||
class Cocker : Dog {} | class Cocker : Dog {} | ||
class Labrador : Dog {} | class Labrador : Dog {} | ||
</ | </enforce> | ||
=== Upcasting === | === Upcasting === | ||
Upcasting means seeing the class as one of its parents: | Upcasting means seeing the class as one of its parents: | ||
< | <enforce> | ||
void Method() | void Method() | ||
{ | { | ||
Line 24: | Line 24: | ||
string sentence = cocker; // error: cocker does -not- inherit from string | string sentence = cocker; // error: cocker does -not- inherit from string | ||
}; | }; | ||
</ | </enforce> | ||
=== Downcasting === | === Downcasting === | ||
Downcasting means seeing a parent class as a specific child - this must be done by manually casting: | Downcasting means seeing a parent class as a specific child - this must be done by manually casting: | ||
< | <enforce> | ||
void Method(Dog dog) | void Method(Dog dog) | ||
{ | { | ||
Line 39: | Line 39: | ||
cocker3.Cast(dog); // alternative method | cocker3.Cast(dog); // alternative method | ||
} | } | ||
</ | </enforce> | ||
=== Manual Casting === | === Manual Casting === | ||
< | <enforce> | ||
void Method() | void Method() | ||
{ | { | ||
Line 50: | Line 50: | ||
string value3 = "result = " + (bool)value2; // "result = true", as a non-zero integer is true when casted to bool | string value3 = "result = " + (bool)value2; // "result = true", as a non-zero integer is true when casted to bool | ||
} | } | ||
</ | </enforce> | ||
Line 59: | Line 59: | ||
{{Feature|informative|The generic type is by convention declared by the {{hl|T}} letter.}} | {{Feature|informative|The generic type is by convention declared by the {{hl|T}} letter.}} | ||
< | <enforce> | ||
// template class Item with generic type T | // template class Item with generic type T | ||
class Item<Class T> | class Item<Class T> | ||
Line 85: | Line 85: | ||
} | } | ||
}; | }; | ||
</ | </enforce> | ||
< | <enforce> | ||
void Method() | void Method() | ||
{ | { | ||
Line 96: | Line 96: | ||
intItem.PrintData(); // prints "m_data = 72" | intItem.PrintData(); // prints "m_data = 72" | ||
} | } | ||
</ | </enforce> | ||
Line 104: | Line 104: | ||
It is used to inject inherited class into class hierarchy without modifying other scripts (especially suitable in modding). A modded class behaves like a class inherited from the original class (one can use {{hl|[[Arma Reforger:Object Oriented Programming Basics#super|super]]}} to access the original class). When a modded class is declared, the modded class will be instanced instead of the original class. Only classes within the same module can be modded (to mod a class in e.g {{hl|GameLib}} module, the modded class has to be placed in the {{hl|GameLib}} module). | It is used to inject inherited class into class hierarchy without modifying other scripts (especially suitable in modding). A modded class behaves like a class inherited from the original class (one can use {{hl|[[Arma Reforger:Object Oriented Programming Basics#super|super]]}} to access the original class). When a modded class is declared, the modded class will be instanced instead of the original class. Only classes within the same module can be modded (to mod a class in e.g {{hl|GameLib}} module, the modded class has to be placed in the {{hl|GameLib}} module). | ||
< | <enforce> | ||
// game | // game | ||
class A | class A | ||
Line 129: | Line 129: | ||
a.Say(); // prints 'modded Say method' then 'original Say method' | a.Say(); // prints 'modded Say method' then 'original Say method' | ||
} | } | ||
</ | </enforce> | ||
{{GameCategory|armaR|Modding|Guidelines|Scripting}} | {{GameCategory|armaR|Modding|Guidelines|Scripting}} |
Revision as of 20:18, 30 July 2022
Casting
Casting is the act of "presenting" a value as another type. For example, if a class hierarchy is Animal > Dog > Cocker, a dog is an animal, a cocker is a dog (that is an animal), but a dog is not especially a cocker.
Upcasting
Upcasting means seeing the class as one of its parents:
Downcasting
Downcasting means seeing a parent class as a specific child - this must be done by manually casting:
Manual Casting
Template
A template is a class that allows a generic management for multiple types. Its methods cannot assume anything about the type.
Modding
A mod can inherit/replace an existing class with the use of the modded keyword.
It is used to inject inherited class into class hierarchy without modifying other scripts (especially suitable in modding). A modded class behaves like a class inherited from the original class (one can use super to access the original class). When a modded class is declared, the modded class will be instanced instead of the original class. Only classes within the same module can be modded (to mod a class in e.g GameLib module, the modded class has to be placed in the GameLib module).