I'd like to write a
class X (this) which
inherits from A (base) can
execute the methods of B (?) and must
implement the members of C (interface).
Implementing A and C are not a problem. But since X cannot derive from multiple classes it seems impossible to have X inherit the logic of A and B. Note that A is the very important base class and B is almost a interface but contains executable behaviour. The reason why I don't want B to be an interface is because the behaviour is the same for every class that inherits or implements it.
Do I really must declare B as an interface and implement the exact same 10 lines of code for each X that needs the behaviour of B?
2 months later
I am currently learning C++ for using it in UE4 (Unreal Engine 4).
Since C++ is a lot less strict than C# it actually contains a pattern implementation idom term that describes this behaviour: These are called mixins.
You can read a paragraph about the C++ mixin here on page 9 (second paragraph).
This document describes the simplest possible coding style for making classes in C. It will describe constructors, instance variables, instance methods, class variables, class methods, inheritance, polymorphism, namespaces with aliasing and put it all together in an example project.
A class defines a type of object, but it is not an object itself. An object is a concrete entity based on a class, and is sometimes referred to as an instance of a class. Objects can be created by using the new keyword followed by the name of the class that the object will be based on, like this: C# Copy.
Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces.
C# compiler is designed not to support multiple inheritence because it causes ambiguity of methods from different base class. This is Cause by diamond Shape problems of two classes If two classes B and C inherit from A, and class D inherits from both B and C.
Do I really must declare
B
as an interface and implement the exact same 10 lines of code for eachX
that needs the behaviour ofB
?
Yes and no. You do need to make B
an interface. But common method implementations should not be duplicated across all implementations of the interface. Instead, they should go into a class extension for interface B
:
public interface B {
void MethodX();
void MethodY();
}
public static class ExtensionsB {
public static void MethodZ(this B b) {
// Common implementations go here
}
}
Extension methods provide a way to share implementations "horizontally", without having your classes inherit from a second class. Extension methods behave as if they were regular methods of the class:
class X : A, B {
public void MethodX() {...}
public void MethodY() {...}
}
public static void Main(string[] args) {
var x = new X();
x.SomeMethodFromA();
x.MethodX(); // Calls method from X
x.MethodY(); // Calls method from X
x.MethodZ(); // Calls method from ExtensionsB
}
I think your best bet would be to require an instance of B
in the constructor of A
and then expose or call the methods of B
as required:
public class X : A, C
{
private readonly B _b;
public X(B b)
{
_b = b;
}
}
You'll find a lot of information on this sort of approach if you look up Composition over inheritance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With