- Home /
Inheritance structure
Hello all,
I have some difficulties managing inheritance in C#. I'm writing a game in which the player builds all sorts of machinery; this can be anything, from relatively simple objects (such as simple containers/boxes) all the way to something more elaborate; let's use a coal extractor as an example. I want to inherit certain behaviours from my simple objects in the more complex objects. To give a concrete example:
The coal extractor needs two basic behaviours:
The behaviour of a container (to store the extracted coal, and also so that the player can interact with it in the same way as it does with a box and move the coal from the extractor's internal storage into the inventory)
The behaviour of an electrically powered device (to be able to interact with the electric network, to know whether there is sufficient power in the electric grid etc.)
I hae two classes, Container and ElectricSink, which would do just the job, but C# won't let me inherit from two classes. Moreover, I not only need the behaviour (implementation of methods) from the those two classes; I need my Extractor to actually pass is-a type tests. This is because when the player interacts with an object, the type of interaction gets chosen by a piece of code that has different behaviours based on type. So, when I want to take the coal out of the extractor, I need it to go through a is-a Container test.
This can't be achieved with the interface trick, where you store instances of objects that implement different interfaces, because the underlaying class holding those objects still cannot itself pass the type testing.
Any ideas as to how I can solve this issue? Thanks!,
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
unity doesn't recognize the derived class 3 Answers
Inheritance structure 2 Answers
Abstract class question 1 Answer