I have a doubt in Inheritance
1,I have a common class named - common
It has 3 child classes child1,child2,child3
2, I have a common class named - state
It has 3 child classes state1, state2, state3
I have a function named xyz() in all the 3 childclasses of common, which I want to move to the parent class common.
The function xyz( ) in child1 uses state1 class inside the function, creates object for state1 class.
The function xyz( ) in child2 uses state2 class and creates object for state2 class.
The function xyz( ) in child3 uses state3 class and creates object for state3 class.
But the procedure is same in all the function xyz()of the 3 childclasses, the only difference is usage of classes state1, state2, state3.
In this case how can I put that xyz()function in parent class.so that all the 3 child classes can access the function from the parent class...
Is it necessary that I have to use Generic Inheritance....
could anybody throw some light on it?
keerthiPosted Dec 9, 2007, 2:29 AM
AlanPosted Dec 8, 2007, 5:25 PM
The only way of doing this that I can think of is as follows:
using System;
class Test
{
static void Main()
{
child1 c1 = new child1();
state1 s1 = (state1)c1.xyz();
child2 c2 = new child2();
state2 s2 = (state2)c2.xyz();
child3 c3 = new child3();
state3 s3 = (state3)c3.xyz();
}
}
class common
{
public state xyz()
{
state s = null;
if (this is child1)
s = new state1();
else if (this is child2)
s = new state2();
else if (this is child3)
s = new state3();
return s;
}
}
class child1: common {}
class child2: common {}
class child3: common {}
class state
{
}
class state1 : state
{
public state1()
{
Console.WriteLine("state1");
}
}
class state2 : state
{
public state2()
{
Console.WriteLine("state2");
}
}
class state3 : state
{
public state3()
{
Console.WriteLine("state3");
}
}
/* the output is:
state1
state2
state3
*/
/* the output is:
state1
state2
state3
*/
However, it's hardly good OO practice for a base class to assume knowledge of its child classes so I can't really recommend it unless you must do it this way for some reason.
Mike GoldPosted Dec 8, 2007, 5:25 PM
I have a doubt in Inheritance
1,I have a common class named - common
It has 3 child classes child1,child2,child3
2, I have a common class named - state
It has 3 child classes state1, state2, state3
I have a function named xyz() in all the 3 childclasses of common, which I want to move to the parent class common.
The function xyz( ) in child1 uses state1 class inside the function, creates object for state1 class.
The function xyz( ) in child2 uses state2 class and creates object for state2 class.
The function xyz( ) in child3 uses state3 class and creates object for state3 class.
But the procedure is same in all the function xyz()of the 3 childclasses, the only difference is usage of classes state1, state2, state3.
In this case how can I put that xyz()function in parent class.so that all the 3 child classes can access the function from the parent class...
Is it necessary that I have to use Generic Inheritance....
could anybody throw some light on it?
just create a virtual function in state called DoStateSpecific. Overridde it in all three state classes.
Move xyz to the child base class and declare a member in the child base class
state _state;
or you can pass the state into xyz
xyz(state myState) // myState can be of type state1, state2, state3
do the state specific stuff inside of the DoStateSpecific override function.