I’m needing help implementing an interface for my many enemy classes as a way of implementing polymorphism. I read the moock book but I cant seem to make much out of it. Could someone point me to a guide or something and maybe help me decide/tell me when i should take advantage of them?
Cloud_9ine
2231 posts
|
|
Jabor
9656 posts
|
It sounds to me like you don’t necessarily want an interface for your enemy classes here – much better would be a base “Enemy” class and all your other classes inheriting from that. Inheriting a base class is usually the better solution for “is-a” relationships – like a walking squid “is-a” Enemy, a bus “is-a” automobile, and so on; while interfaces are used for behaviour relationships. |
jonathanasdf
1592 posts
|
like, for the bus, it would subclass an automobile, but it might implement wheels (if Automobile doesn’t already implement wheels) |
Jabor
9656 posts
|
No, that’s still not a “behaviour” relationship. A behaviour relationship is something like “carries passengers” – buses, planes, cars and taxis would all implement IPassengerCarrier. |
Cloud_9ine
2231 posts
|
Okay I got you guys. so an interface could be used for something where some enemys havee a feature that some have and some dont and a sub sub class thing wont work. Right? |
bLasTamos
546 posts
|
Kind of, but not exactly. I can give you the example of an interface I made recently. By handling an IMovableObject, the only thing you can expect from it is moving it with a certain vector. |
jonathanasdf
1592 posts
|
basically, what he means is that the interface defines that this object which implements it definitely has a certain function, which in this case is the moveWith function, however depending on the actual object how that function is defined may vary. But, you know that moveWith definitely exists on anything that implements the interface. |
bLasTamos
546 posts
|
No interfaces are not for saying some class has some function. You can interpertate it that way but that’s not what they are. |
jonathanasdf
1592 posts
|
I guess I’m still not very clear about it myself :) thanks for clearing it up. |
Loading