Wave's~ BlitzMax Tutorial for NG | ~ November, 2015 ~ Version 11
Beginners guide to BlitzMax |
Override Methods
If you have a type Car which is extended from a Vehicle then you'll inherit all methods in Vehicle to Car. You can still make a Method in Car with the same name as this Method in vehicle. If you call that method from your car you'll use the car's method. If you call it from vehicle you'll use the vehicle's method. When you have two methods of the same name in the same type it is called overriding methods. The Cars method overrides the vehicles method (car extends vehicle). This is also known as polymorphism. It can be useful because one command can do a lot of different things depending on who is calling it (what object-type). See the example below.
Self
Self can only be used in methods. If you use Self it'll refer to the instance of that type that called the method. Like: Local C:Car = New Car; C.Run(). In the Run method Self would refer to the instance of Car known as C. So all fields in method can be accessed by Self.FieldName instead of just FieldName this use of Self would be quite pointless though. You use Self is if you in a method have a function/method that requires an instance of your type to be passed to it. Let's say that you inside your Method Run() in your Car-Type, need to call a function which accept Cars, to refer to your current instance of your car, use Self, StealTires( Self, Number ). Also you can use self when your function parameters have the same name as fields in your type. Example: Method SetPosition( X, Y ) Self.X = X self.Y = Y If you haven't used Self here then your method must call it's parameters X1, Y1 or something.
Here is the real example:
Type Vehicle Global Creator$ = " FX-Factory" Field Name$="Vehicle" Field Broken=False Method Test() If Not Broken print name+" works!" Else Print name+" is broken!" End Method 'Self refers to the Type calling 'Example:'Bus.Collide( Train ) '--> Bus is refered to as Self and Train as V (in the method below) 'Example2:'Train.Collide( Bus ) '--> Train is refered to as Self and Bus as V (in this method Collide) Method Collide(V:Vehicle) If Car(Self) And Car(V) Print "Car Collide with Car" If Car(Self) And Bus(V) Print "Car Collide with Bus" If Bus(Self) And Car(V) Print "Bus Collide with Car" If Bus(Self) And Bus(V) Print "Bus Collide with Bus" Broken=True 'Note that Self. is not needed here V.Broken=True End Method 'If this Method feels strange, checkout "casting" below End Type Type Car Extends Vehicle Method Test() If Not Broken Then Print "Car works!" Else Print "Car is broken!" End If End Method End Type Type Bus Extends Vehicle Method Test() If Not Broken Then Print "Bus works!" Else Print "Bus is broken!" End If End Method Method SuperTest() Super.Test() 'Calls Vehicle's Test() End Method End Type 'Just To test it all in an easy way Local C:Car = New Car Local B:Bus = New Bus Local B.Collide( C ) Local Car2:Car = New Car Local Bus2:Bus = New Bus Bus2.Collide( B ) Car2.Collide( Bus2 ) Car2.Collide( C ) Car2.Test C.Test B.Test Bus2.Test Bus2.SuperTest()
This example shows method overriding, self, super, and casting.
Super
If you have an overridden function or method Somefunction() in an extended type, let's take "Car" again. If you are somewhere in the car type, its functions or methods, and want to call Vehicles Somefunction() (With the same name!) you have to refer to it as Super.Somefunction(). Read Super as: Use parent's
Casting
In the above example I use casting. Casting is a way to check if an object is of a certain type (if it is a car not a bus). Objects are the instances of your types. If you create a new car, C:Car then C is your object, there will be times where you either want to check if C is a Car, Bus or perhaps a Vehicle. Well the answer here should be obvious, C is a Car and a Vehicle but not a bus. Casting is done like this: TypetoCheck( InstanceObject ). If you look in the code example above at the method Collide() you'll see that I use casting to check whether the object that calls the method is a Car or a Bus. Like this Car(Self) and Bus(Self). Car(Self) will check if Self is a Car-Type. If Self is a Car-Type or any Type extending Car this will return a Car-Type which is Self in this case. Confused? If Self is not a Car-Type it returns Null or False. If a Car-Type is returned it is not Null, therefore it's True. This is why we get True if Self is a Car. Casting can be used on any Object-Type, actually BlitzMax casts things for you sometimes, like when you assign an Integer to a String. This is specified in the Language Reference. Still confused? Then I recommend you take a closer look at the example above and the example below, try to change the code. One example of casting could be if a function returns an object. All types are of the object type. An array of objects can for example contain any type. The same goes with lists which only uses objects. These objects could be strings, types or arrays.
'------------------------------------ Type Tank Field name$ = "Object Tank" End Type Type Bird End Type Function ReturnsAnObject:Object() Local T:Tank = New Tank Return T 'But it is actually a Tank End Function TestObject:Object = ReturnsAnObject() ' Let's say we want to know if this TestObject is a Tank then we need ' to cast it like this: If Tank( TestObject ) ' This will evaluate True only If TestObject is of the Tank-Type. ' Ok, we know it's of the TankType but how dowe do if we want to change ' the name of this tank. We cannot do TestObject.Name$, We know it's a ' tank but BlitzMax doesn't, so first we need to put the tank into a ' variable of it's own: Local myTank:Tank = Tank( TestObject ) Print myTank.Name 'You can also cast And access an Object's field or method in one line: Tank( TestObject ).Name$ = "Cool Tank" End If ' To show it all I made a Function which takes objects And If it is a ' tank it prints the name. Function GetTank( TestObject:Object ) If Tank( TestObject ) Local myTank:Tank = Tank( TestObject ) If myTank Then Print myTank.Name End If Else Print "Not a Tank" End If End Function 'See what happens when we give the Function some stuff. GetTank( TestObject ) GetTank(New Tank) GetTank(New Bird) '----------------------------
To Index | Next Page | page 14