Object Oriented Programming Advanced Usage – Arma Reforger

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Fixed errors in examples)
m (Text replacement - "<syntaxhighlight lang="C#">" to "<enforce>")
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.


<syntaxhighlight lang="C#">
<enforce>
class Animal {}
class Animal {}
class Dog : Animal {}
class Dog : Animal {}
Line 17: Line 17:


Upcasting means seeing the class as one of its parents:
Upcasting means seeing the class as one of its parents:
<syntaxhighlight lang="C#">
<enforce>
void Method()
void Method()
{
{
Line 29: Line 29:


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:
<syntaxhighlight lang="C#">
<enforce>
void Method(Dog dog)
void Method(Dog dog)
{
{
Line 43: Line 43:
=== Manual Casting ===
=== Manual Casting ===


<syntaxhighlight lang="C#">
<enforce>
void Method()
void Method()
{
{
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.}}


<syntaxhighlight lang="C#">
<enforce>
// template class Item with generic type T
// template class Item with generic type T
class Item<Class T>
class Item<Class T>
Line 87: Line 87:
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="C#">
<enforce>
void Method()
void Method()
{
{
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).


<syntaxhighlight lang="C#">
<enforce>
// game
// game
class A
class A

Revision as of 19:17, 30 July 2022

This guideline requires the understanding of Object Oriented Programming Basics.


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.

<enforce> class Animal {} class Dog : Animal {} class Cocker : Dog {} class Labrador : Dog {} </syntaxhighlight>

Upcasting

Upcasting means seeing the class as one of its parents: <enforce> void Method() { Cocker cocker = new Cocker(); Animal animal = cocker; // OK, as a cocker is an animal string sentence = cocker; // error: cocker does -not- inherit from string }; </syntaxhighlight>

Downcasting

Downcasting means seeing a parent class as a specific child - this must be done by manually casting: <enforce> void Method(Dog dog) { Cocker cocker1 = dog; // error: "Dog" is too generic to be casted as Cocker - it could be e.g a Labrador 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

Cocker cocker3; cocker3.Cast(dog); // alternative method } </syntaxhighlight>

Manual Casting

<enforce> void Method() { float value1 = 4.9; int value2 = value1; // value2 = 4 as integer casting -truncates- the value, not rounds it string value3 = "result = " + (bool)value2; // "result = true", as a non-zero integer is true when casted to bool } </syntaxhighlight>


Template

A template is a class that allows a generic management for multiple types. Its methods cannot assume anything about the type.

The generic type is by convention declared by the T letter.

<enforce> // template class Item with generic type T class Item<Class T> { protected T m_data;

void Item(T data) { m_data = data; }

void SetData(T data) { m_data = data; }

T GetData() { return m_data; }

void PrintData() { Print(m_data); } }; </syntaxhighlight>

<enforce> void Method() { Item<string> stringItem = new Item<string>("Hello!"); // template class Item declared with type "string". In Item<string> class, all Ts are substituted with 'string' 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!'" intItem.PrintData(); // prints "m_data = 72" } </syntaxhighlight>


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).

<enforce> // game class A { void Say() { Print("original Say method"); } };

// mod modded class A // this class automatically inherits from the original class A { void Say() { Print("modded Say method"); super.Say(); } };

void Test() { A a = new A(); // "modded class A" is instanced a.Say(); // prints 'modded Say method' then 'original Say method' } </syntaxhighlight>