Neok of http://forums.rpgmakerweb.com recently asked the question:
[quote]
[/quote]
To which my answer was…
[quote]
If the normal code (ie, class Window_ChoiceList < Window_Command) is still there, it will not ‘affect’ anything. If you have removed the original code, there would be no inheritance when the code is initialized. For example :
class Game_Actor def example_method p "Just showing text" end end
Will simply add a new method into the game actor class, providing the Game_Actor class has been previously defined, it does not remove Game_Actors inheritance from Game_Battler.
[/quote]
DRS of http://forums.rpgmakerweb.com then stated:
[quote]
That’s interesting! I thought it would be over written since it wasn’t making the original inheritance call.
[/quote]
This prompted me to write a short explanation of inheritance and how to include new inheritance within your classes. So here it is…
As long the inheritance is being applied at ‘some point’ along the line of code it will work. For example…
I have defined this code within my projects script editor…
class Person def intialize @name = "Newborn" @age = 1 end def name @name end def age @age end end class Dekita < Person def intialize @name = "Dekita" @age = 24 end end
This creates and defines the ‘Dekita’ class. Which inherits from ‘Person’ class. Now, I want to add a new method into ‘Dekita’ class called ‘gender’.
class Dekita alias :init_alias :initialize def initialize init_alias @gender = "??" end def gender @gender end end
As you can see, I have removed the class inheritance from my new methods code, but because its already defined prior to my new modification, it does not affect any inheritance already included. This can be tested by calling the code below (when the code from above is also there);
dekita = Dekita.new p dekita.name p dekita.age p dekita.gender
Also – lets say we wanted to include a new ‘inheritance’. We can do this by creating a new module and including it in any class we choose, for this example, I am placing it within the ‘Person’ class.. This will mean even descendants of ‘Person’ will be entitled to access the methods.
module Person_New def new_stuff return "New Stuff" end end class Person include Person_New end
Can be tested by calling.
p dekita.new_stuff
For convenience, I have included all the code from above in one handy script – ready to put in your script editor and mess around with to your hearts content. If can be found HERE 🙂