Extending Types * 2

BlitzMax Forums/BlitzMax Beginners Area/Extending Types * 2

Trader3564(Posted 2008) [#1]
Ok so

Type A Extends B, C

doesnt work

Type A Extends B AND C

doesnt work

Type A Extends B Extends C

doesnt work either

Question: How do i extend 2 types?


Trader3564(Posted 2008) [#2]
e.g.

Type Player Extends ActiveObject, Movement, Animation

End Type


Trader3564(Posted 2008) [#3]
.


Otus(Posted 2008) [#4]
Cannot. Sorry.


TaskMaster(Posted 2008) [#5]
Type A
End Type

Type B Extends A
End Type

Type C Extends B
End Type


Brucey(Posted 2008) [#6]
No multiple inheritance in BlitzMax.


Czar Flavius(Posted 2008) [#7]
Workaround:
Type A
Field myB:B = New B
Field myC:C = New C
End type

Local blah:A = New A
A.myB.something()



plash(Posted 2008) [#8]
@Czar: That has nothing to do with inheritance.

Attempting to emulate multiple inheritance like that is quite nasty.


Trader3564(Posted 2008) [#9]
O_O it cant be done at all? o.o

hmm.. i assume then there is no other way than to create multiple combinations of types and work with duplicate code :S

Or you get (indeed) something like

Type A
End Type

Type B Extends A
End Type

Type C Extends B
End Type

But isnt that slow?


plash(Posted 2008) [#10]
But isnt that slow?
Wouldn't multiple inheritance also be 'slow'?


Trader3564(Posted 2008) [#11]
i have no idea.. i assume it wouldve been faster as it would ve been designed for it but i guess you have a point.
Question is tough, IS IT slow.. because if theres any barely any slowdown, then theres no problem with doing it that way. Otherwise im better off making individual Types anyway.


plash(Posted 2008) [#12]
Your fussing over non-issues, the real slow-down is collision detection, mass mathematical calculations and graphics rendering.


Philip7(Posted 2008) [#13]
Building the ideal Type tree is one of the fun things in programming.
If you need multiple inheritance you're building a broken tree.
The whole idea of inheritance is to give you a more centralized control over your objects.

As much trouble as i have with timingcode and tweening, so easy comes OOP. Thanks for the timingcode you posted a few days ago, i'm using it myself now.


Paposo(Posted 2008) [#14]
I agree very munch the implementation of Interface (like java) for support the pseudo-multiinherintance.
The really multi-inheritance is error prone.

Thanks,
paposo


Czar Flavius(Posted 2008) [#15]
TStudentMusician inherits form both TStudent and TWorker

The ideal way
Type TStudentMusician Extends TStudent And TWorker


The work-around way
Type TStudentMusician
Field s:TStudent = New TStudent
Field w:TWorker = New TWorker
Method getAge:Int() Return s.age End Method
Method getHoursWorked:Int() Return w.hoursworked End Method
End Type



Kistjes(Posted 2009) [#16]
It can help by thinking about the 'is a' and 'has a' relation.
TStudentMusician .. is-a .. TStudent
TStudentMusician .. has-a .. TJob

So..
Type TStudent
	Field age%

	Method GetAge%()
		Return age
	End Method
End Type


Type TStudentMusician Extends TStudent
	Field job:TJob = new TJob

	Method GetHoursWorked%()
		Return job.GetHoursWorked()
	End Method
End Type


It helps you to solve the lack of multi-inheritance (which is not always the preferable OOP structure, after all)


Stu_ovine(Posted 2009) [#17]
Sorry to hijack this thread but Im wondering using the above code - how do you reference the Extended type (tstudent) to be used with an function outside the type.

Type TStudent
	Field age%

	Method GetAge%()
		Return age
	End Method
End Type

Type TStudentMusician Extends TStudent
End Type

Type ITStudent Extends TStudent
End Type

'
'How can you reference the age field using a function ?
'
Local student1:TStudentMusician = New  TStudentMusician
Local student2:ITStudent = New  ITStudent

student1.age = 23
student1.age = 28

DebugLog( ShowAge(student1))
DebugLog( ShowAge(student2))

Function ShowAge:Int( studentdata:Object )  ' <<< how to pass 

    Return Studentdata.age

End Function


NO sooner I post then I find the answer


    Return Tstudent(Studentdata).age




Yan(Posted 2009) [#18]
Using polymophinwhatitsface...
Function ShowAge:Int(studentData:TStudent)

    Return studentData.GetAge()

End Function