11-11-2018, 05:47 AM
(This post was last modified: 11-11-2018, 06:14 AM by DerVVulfman.)
I wanted you to know about Inheritance and remind you about the Parent and Child Classes. At the same time, I needed you to understand that a Parent Class wears blue tights and a red cape.... er... a UPERCLASS.
Now to talk about how one class 'inherits' and what may happen
As you may have guessed, when you decide to make a class based on another class, crafting a CHILD class, that child class carries over methods from the Parent class. It inherited 'all' the methods from the previous class. And we also say that the Parent class is the Superclass to the new class you're writing.
So let's assume you have an existing class... And ... yeah... there's not much to it.
The initialize method creates a goofy value called @name. And the @name value is holding the name 'pops'. The my_name value just returns the data of the @name value. So calling (or using) my_name just supplies you with the name in there.
If we made a class called Kid_Class like THIS:
And it will actually inherit both the initialize and my_name methods!!!
BUT, what happens if you ... perform a change within the Kid_Class????
Ooooh.... This one does not inherit the initialize method. It destroys it and creates its own initialize method. This new initialize method creates a new initialize method and new @name value storing the name 'Brat' this class does inherit the my_name method just as its parent class, so it will return the value of the @name value... returning 'Brat' instead of 'pops'
And...
This likewise rewrites the initialize method, this time not even creating a @name variable but a @name2 variable instead. Yes, this version has a my_name call, but running it might go and *CRASH* as this class doesn't have a @name variable.
So, now let's introduce something new....
What's that super() statement?????
As stated some time earlier, a Parent class is also called a superclass to the class. The super() allows you to create a new method, but include all the material and statements from the original within. So the above version with the super() statement acts like this!
Everything from the first statement is replicated, pasted, SHOVED into where the super() statement is!!!! So this version with the super() statement creates both a @name and @name2 value.
So now let's try something practical and already existing.......
Let's look at the initialize method of the Game_Event class.
Oh, wow. First, this class is a child class of the Game_Character class. but it also has a super() statement, which means this class not only inherits methods from Game_Character, but this totally rewritten 'initialize' class has some of Game_Character's initialize method thrown in!
Here is the Initialize method from Game_Character
So essentially, what the Game_Event class really does is it replaces the super() statement with all the stuff from Game_Character... like this:
That's sorta the BASICS of the super() statement. However, there's more to it. But I do want you to understand this before I cover something called parameters or arguments.
NO ARGUMENTS FROM THE AUDIENCE, PLEASE!!!!
Now to talk about how one class 'inherits' and what may happen
As you may have guessed, when you decide to make a class based on another class, crafting a CHILD class, that child class carries over methods from the Parent class. It inherited 'all' the methods from the previous class. And we also say that the Parent class is the Superclass to the new class you're writing.
So let's assume you have an existing class... And ... yeah... there's not much to it.
Code:
class Pops_Class
def initialize
@name = 'pops'
end
def my_name
return @name
end
end
If we made a class called Kid_Class like THIS:
Code:
class Kid_Class < Pops_Class
end
BUT, what happens if you ... perform a change within the Kid_Class????
Code:
class Kid_Class < Pops_Class
def initialize
@name = 'Brat'
end
end
And...
Code:
class Kid_Class < Pops_Class
def initialize
@name2 = 'Brat'
end
end
So, now let's introduce something new....
Code:
class Kid_Class < Pops_Class
def initialize
super()
@name2 = 'Brat'
end
end
As stated some time earlier, a Parent class is also called a superclass to the class. The super() allows you to create a new method, but include all the material and statements from the original within. So the above version with the super() statement acts like this!
Code:
class Kid_Class < Pops_Class
def initialize
@name = 'pops'
@name2 = 'Brat'
end
end
Everything from the first statement is replicated, pasted, SHOVED into where the super() statement is!!!! So this version with the super() statement creates both a @name and @name2 value.
So now let's try something practical and already existing.......
Let's look at the initialize method of the Game_Event class.
Code:
def initialize(map_id, event)
super()
@map_id = map_id
@event = event
@id = @event.id
@erased = false
@starting = false
@through = true
# Move to starting position
moveto(@event.x, @event.y)
refresh
end
Here is the Initialize method from Game_Character
Code:
def initialize
@id = 0
@x = 0
@y = 0
@real_x = 0
@real_y = 0
@tile_id = 0
@character_name = ""
@character_hue = 0
@opacity = 255
@blend_type = 0
@direction = 2
@pattern = 0
@move_route_forcing = false
@through = false
@animation_id = 0
@transparent = false
@original_direction = 2
@original_pattern = 0
@move_type = 0
@move_speed = 4
@move_frequency = 6
@move_route = nil
@move_route_index = 0
@original_move_route = nil
@original_move_route_index = 0
@walk_anime = true
@step_anime = false
@direction_fix = false
@always_on_top = false
@anime_count = 0
@stop_count = 0
@jump_count = 0
@jump_peak = 0
@wait_count = 0
@locked = false
@prelock_direction = 0
end
So essentially, what the Game_Event class really does is it replaces the super() statement with all the stuff from Game_Character... like this:
Code:
def initialize
@id = 0
@x = 0
@y = 0
@real_x = 0
@real_y = 0
@tile_id = 0
@character_name = ""
@character_hue = 0
@opacity = 255
@blend_type = 0
@direction = 2
@pattern = 0
@move_route_forcing = false
@through = false
@animation_id = 0
@transparent = false
@original_direction = 2
@original_pattern = 0
@move_type = 0
@move_speed = 4
@move_frequency = 6
@move_route = nil
@move_route_index = 0
@original_move_route = nil
@original_move_route_index = 0
@walk_anime = true
@step_anime = false
@direction_fix = false
@always_on_top = false
@anime_count = 0
@stop_count = 0
@jump_count = 0
@jump_peak = 0
@wait_count = 0
@locked = false
@prelock_direction = 0
@map_id = map_id
@event = event
@id = @event.id
@erased = false
@starting = false
@through = true
# Move to starting position
moveto(@event.x, @event.y)
refresh
end
That's sorta the BASICS of the super() statement. However, there's more to it. But I do want you to understand this before I cover something called parameters or arguments.
NO ARGUMENTS FROM THE AUDIENCE, PLEASE!!!!