Object Oriented Programming Advanced Usage – Arma Reforger
Lou Montana (talk | contribs) m (Some wiki formatting) |
Lou Montana (talk | contribs) m (Some wiki formatting) |
||
(2 intermediate revisions by 2 users not shown) | |||
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> | </enforce> | ||
Line 119: | Line 116: | ||
} | } | ||
} | } | ||
void Test() | void Test() | ||
{ | { | ||
Line 125: | Line 122: | ||
a.Say(); // prints "original Say method" then "something said" | 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"; | m_sPrivateString = "modded said"; | ||
Line 136: | Line 133: | ||
} | } | ||
} | } | ||
void TestModded() | void TestModded() | ||
{ | { |
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.