method inheretance
-
Hello everyone, I'm building my own library. But I'm kind of stuck and it is really tough to explain, but I will try: I have a class, let's call this X. X has an interface and a base class. X also inherets another class (called Y). Now I call some base functions and give X as a parameter. But I would like to know if it is possible in another baseclass? To sketch it up in code, it looks like this now:
class X : Y, InterfaceX
{
public X()
{
}bool function1(String txt)
{
return base.function1(this, txt);
}
}class Y
{
bool function1(InterfaceX x, String txt)
{
//do something
return true/false;
}
}And what I would like to have is something like this:
class X : Y (& Z), InterfaceX
{
public X()
{
}
}class Y
{
bool function1(InterfaceX x, String txt)
{
//do something
return true/false;
}
}class Z
{bool function1(String txt)
{
return base.function1(this.getParent(), txt);
}
}Is this possible, and if it is, how can I implement this? Thank you very much.
-
Hello everyone, I'm building my own library. But I'm kind of stuck and it is really tough to explain, but I will try: I have a class, let's call this X. X has an interface and a base class. X also inherets another class (called Y). Now I call some base functions and give X as a parameter. But I would like to know if it is possible in another baseclass? To sketch it up in code, it looks like this now:
class X : Y, InterfaceX
{
public X()
{
}bool function1(String txt)
{
return base.function1(this, txt);
}
}class Y
{
bool function1(InterfaceX x, String txt)
{
//do something
return true/false;
}
}And what I would like to have is something like this:
class X : Y (& Z), InterfaceX
{
public X()
{
}
}class Y
{
bool function1(InterfaceX x, String txt)
{
//do something
return true/false;
}
}class Z
{bool function1(String txt)
{
return base.function1(this.getParent(), txt);
}
}Is this possible, and if it is, how can I implement this? Thank you very much.
Class X can't inherit from Y and Z because C# only supports single inheritance (unfortunately). You could have X inherit from Y, and Y inherit from Z. Or you could have X implement multiple interfaces. Also you need the keyword "virtual" on the virtual methods in your base classes, and the keyword "override" on your overridden methods in the derived classes. Another possibility is the Decorator design pattern, which lets you define inheritance (single or multiple) at runtime.
-
Class X can't inherit from Y and Z because C# only supports single inheritance (unfortunately). You could have X inherit from Y, and Y inherit from Z. Or you could have X implement multiple interfaces. Also you need the keyword "virtual" on the virtual methods in your base classes, and the keyword "override" on your overridden methods in the derived classes. Another possibility is the Decorator design pattern, which lets you define inheritance (single or multiple) at runtime.
Thank you. But the problem I have is that if I want to add a function to my Interface class and it is as simple as calling the base function with as parameter itself, shouldn't it be possible to do that in some kind of global class. Something like this:
class Main
{
public Main()
{
X x = new X();
x.function1("myText"); -> false
X2 x2 = new X2();
x2.function1("myText"); -> true
}
}interface InterfaceX
{
int number { get; };
bool function1(String txt);
}class X : Z, InterfaceX
{
public int number
{
get { return 5;}
}
public X(){}
}
class X2 : Z, InterfaceX
{
public int number
{
get { return 2;}
}
public X2() {}
}class Z : Y,
{
public bool function1(String txt)
{
return base.function2(this.getParent(), txt);
}
}class Y
{
public bool function2(InterfaceX x, String txt)
{
if(x.number < 3)
return true;
else
return false;
}
}It's all about the this.getParent(), this doesn't work, but how can I get this working?
-
Thank you. But the problem I have is that if I want to add a function to my Interface class and it is as simple as calling the base function with as parameter itself, shouldn't it be possible to do that in some kind of global class. Something like this:
class Main
{
public Main()
{
X x = new X();
x.function1("myText"); -> false
X2 x2 = new X2();
x2.function1("myText"); -> true
}
}interface InterfaceX
{
int number { get; };
bool function1(String txt);
}class X : Z, InterfaceX
{
public int number
{
get { return 5;}
}
public X(){}
}
class X2 : Z, InterfaceX
{
public int number
{
get { return 2;}
}
public X2() {}
}class Z : Y,
{
public bool function1(String txt)
{
return base.function2(this.getParent(), txt);
}
}class Y
{
public bool function2(InterfaceX x, String txt)
{
if(x.number < 3)
return true;
else
return false;
}
}It's all about the this.getParent(), this doesn't work, but how can I get this working?
I'm not sure what you're trying to do, but this.getParent () will only work if: 1. Class Z (or an ancestor of Z) defines a getParent () method, and 2. This getParent () method returns InterfaceX (or a descendant of InterfaceX).
-
I'm not sure what you're trying to do, but this.getParent () will only work if: 1. Class Z (or an ancestor of Z) defines a getParent () method, and 2. This getParent () method returns InterfaceX (or a descendant of InterfaceX).
With the this.getParent() I would like to get the 'parent' of Z. So if I make an X : Z I would like to get X. And if I make an BlahBlah : Z I would like to get BlahBlah. I'm trying to get some work of my back. In stead of implementing all the functions (of the interface) inside of every class, I would like to make a base class which implements these functions.
-
With the this.getParent() I would like to get the 'parent' of Z. So if I make an X : Z I would like to get X. And if I make an BlahBlah : Z I would like to get BlahBlah. I'm trying to get some work of my back. In stead of implementing all the functions (of the interface) inside of every class, I would like to make a base class which implements these functions.
If you put all the interface functions in a base class, they will automatically be available to all the derived classes. If this doesn't work for you, you can define a class that implements all the interface functions, and define an instance of this in any class that that implements the interface. Then for each interface function, forward the call to this instance. This avoids having to re-implement the whole functions in each class that implements the interface. For example: interface InterfaceX { // Lots of interface function definitions... } class ImplementationOfInterfaceX : InterfaceX { // Implementation of each function... } class X : InterfaceX { ImplementationOfInterfaceX implX; public int func1 (int par) { return implX.func1 (par); // I.e., a forwarded call instead of a 100-line function. } public float func2 (float par) { return implX.func2 (par); } ... } Of course this would be much easier if C# allowed multiple inheritance. I'm hoping they will in a future version. It makes some things very easy. It seems to me they left it out because it allows complexity and confusion for some extreme contrived cases that no one in their right mind would do. Not a good justification for leaving it out (in my biased opinion).
-
Thank you. But the problem I have is that if I want to add a function to my Interface class and it is as simple as calling the base function with as parameter itself, shouldn't it be possible to do that in some kind of global class. Something like this:
class Main
{
public Main()
{
X x = new X();
x.function1("myText"); -> false
X2 x2 = new X2();
x2.function1("myText"); -> true
}
}interface InterfaceX
{
int number { get; };
bool function1(String txt);
}class X : Z, InterfaceX
{
public int number
{
get { return 5;}
}
public X(){}
}
class X2 : Z, InterfaceX
{
public int number
{
get { return 2;}
}
public X2() {}
}class Z : Y,
{
public bool function1(String txt)
{
return base.function2(this.getParent(), txt);
}
}class Y
{
public bool function2(InterfaceX x, String txt)
{
if(x.number < 3)
return true;
else
return false;
}
}It's all about the this.getParent(), this doesn't work, but how can I get this working?
The crux of the problem is that you want class Z to pass an item into a function that accepts an
InterfaceX
item, but classZ
is not necessarily anInterfaceX
. So, the only way to do it as-is is to do this messy kind of check. Ideally, you'd have the call tofunction2
be in a class that is anInterfaceX
. Passing a Z that is not anInterfaceX
won't work, so you'll have figure out how you want to handle that situation. Here's a classZ
that should work:class Z : Y
{
public bool function1(String txt)
{
if(this is InterfaceX)
return base.function2((InterfaceX)this, txt);
else
return false; // maybe throw exception?
}
}Keep It Simple Stupid! (KISS)
-
The crux of the problem is that you want class Z to pass an item into a function that accepts an
InterfaceX
item, but classZ
is not necessarily anInterfaceX
. So, the only way to do it as-is is to do this messy kind of check. Ideally, you'd have the call tofunction2
be in a class that is anInterfaceX
. Passing a Z that is not anInterfaceX
won't work, so you'll have figure out how you want to handle that situation. Here's a classZ
that should work:class Z : Y
{
public bool function1(String txt)
{
if(this is InterfaceX)
return base.function2((InterfaceX)this, txt);
else
return false; // maybe throw exception?
}
}Keep It Simple Stupid! (KISS)