Object Oriented Programming Advanced Usage – Arma Reforger
Lou Montana (talk | contribs) m (Text replacement - "<syntaxhighlight lang="C#">" to "<enforce>") |
Lou Montana (talk | contribs) m (Some wiki formatting) |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{TOC|side}} | {{TOC|side}} | ||
{{Feature|informative|This guideline requires the understanding of | {{Feature|informative|This guideline requires the understanding of {{Link|Arma Reforger:Object Oriented Programming Basics}}.}} | ||
Line 12: | Line 12: | ||
class Cocker : Dog {} | class Cocker : Dog {} | ||
class Labrador : Dog {} | class Labrador : Dog {} | ||
</ | </enforce> | ||
=== Upcasting === | === Upcasting === | ||
Line 23: | Line 23: | ||
Animal animal = cocker; // OK, as a cocker is an animal | Animal animal = cocker; // OK, as a cocker is an animal | ||
string sentence = cocker; // error: cocker does -not- inherit from string | string sentence = cocker; // error: cocker does -not- inherit from string | ||
} | } | ||
</ | </enforce> | ||
=== Downcasting === | === Downcasting === | ||
Line 35: | Line 35: | ||
Cocker cocker2 = Cocker.Cast(dog); // OK: manual casting tells the code "the developer knows what he is doing" | Cocker cocker2 = Cocker.Cast(dog); // OK: manual casting tells the code "the developer knows what he is doing" | ||
// if 'dog' is not castable as a Cocker, null is returned - the code does not crash | // if 'dog' is not castable as a Cocker, null is returned - the code does not crash | ||
} | } | ||
</ | </enforce> | ||
=== Manual Casting === | === Manual Casting === | ||
Line 50: | Line 47: | ||
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 84: | Line 81: | ||
Print(m_data); | Print(m_data); | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
<enforce> | <enforce> | ||
Line 93: | Line 90: | ||
Item<int> intItem = new Item<int>(72); // template class Item declared with type "int". In Item<int> class, all Ts are substituted with 'int' | Item<int> intItem = new Item<int>(72); // template class Item declared with type "int". In Item<int> class, all Ts are substituted with 'int' | ||
stringItem.PrintData(); // prints "m_data = 'Hello!'" | stringItem.PrintData(); // prints "m_data = 'Hello!'" | ||
intItem.PrintData(); // prints "m_data = 72" | intItem.PrintData(); // prints "m_data = 72" | ||
} | } | ||
</ | </enforce> | ||
Line 102: | Line 99: | ||
A mod can inherit/replace an existing class with the use of the {{hl|modded}} keyword.<br> | A mod can inherit/replace an existing class with the use of the {{hl|modded}} keyword.<br> | ||
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) but also allows '''{{hl|private}}''' methods and functions access and modification. | |||
When a modded class is declared, the modded class will be instanced instead of the original class. | |||
{{Feature|important|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> | <enforce> | ||
Line 108: | Line 108: | ||
class A | class A | ||
{ | { | ||
private string m_sPrivateString = "something said"; | |||
void Say() | void Say() | ||
{ | { | ||
Print("original Say method"); | Print("original Say method"); | ||
Print(m_sPrivateString); | |||
} | } | ||
}; | } | ||
void Test() | |||
{ | |||
A a = new A(); // "class A" is instanced | |||
a.Say(); // prints "original Say method" then "something said" | |||
} | |||
// mod | // mod | ||
modded class A // this class automatically inherits from the original class A | modded class A // this class automatically inherits from the original class A | ||
{ | { | ||
void Say() | override void Say() | ||
{ | { | ||
m_sPrivateString = "modded said"; | |||
Print("modded Say method"); | Print("modded Say method"); | ||
super.Say(); | super.Say(); | ||
} | } | ||
} | } | ||
void | void TestModded() | ||
{ | { | ||
A a = new A(); // "modded class A" is instanced | A a = new A(); // "modded class A" is instanced | ||
a.Say(); // prints | a.Say(); // prints "modded Say method" then "original Say method" and "modded said" | ||
} | } | ||
</ | </enforce> | ||
{{GameCategory|armaR|Modding|Guidelines|Scripting}} | {{GameCategory|armaR|Modding|Guidelines|Scripting}} |
Latest revision as of 23:48, 29 September 2024
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) but also allows private methods and functions access and modification.
When a modded class is declared, the modded class will be instanced instead of the original class.