Extending Types * 2
BlitzMax Forums/BlitzMax Beginners Area/Extending Types * 2
| ||
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? |
| ||
e.g. Type Player Extends ActiveObject, Movement, Animation End Type |
| ||
. |
| ||
Cannot. Sorry. |
| ||
Type A End Type Type B Extends A End Type Type C Extends B End Type |
| ||
No multiple inheritance in BlitzMax. |
| ||
Workaround:Type A Field myB:B = New B Field myC:C = New C End type Local blah:A = New A A.myB.something() |
| ||
@Czar: That has nothing to do with inheritance. Attempting to emulate multiple inheritance like that is quite nasty. |
| ||
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? |
| ||
But isnt that slow? Wouldn't multiple inheritance also be 'slow'? |
| ||
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. |
| ||
Your fussing over non-issues, the real slow-down is collision detection, mass mathematical calculations and graphics rendering. |
| ||
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. |
| ||
I agree very munch the implementation of Interface (like java) for support the pseudo-multiinherintance. The really multi-inheritance is error prone. Thanks, paposo |
| ||
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 |
| ||
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) |
| ||
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 |
| ||
Using polymophinwhatitsface...Function ShowAge:Int(studentData:TStudent) Return studentData.GetAge() End Function |