Is it possible for the a.doStuff() method to print "B did stuff" without editing the A class? If so, how would I do that?
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
a.doStuff();
b.doStuff();
Console.ReadLine();
}
}
class A
{
public void doStuff()
{
Console.WriteLine("A did stuff");
}
}
class B : A
{
public void doStuff()
{
Console.WriteLine("B did stuff");
}
}
I'm modding a steam game, Terraria. And I don't want to decompile and recompile it all because that will screw with steam. My program 'injects' into Terraria via XNA. I can use the update() and draw() methods from XNA to mod some things. But it's pretty limited. I wan't to override base methods to mod more things (worldgen for example).
Yes, if you declare
doStuff
asvirtual
inA
and thenoverride
inB
.The code for class A & B you have posted will anyways generate below compiler warning and will ask to use the
new
keyword on class B, although it will compile: The keyword new is required on 'B.doStuff()' because it hides inherited member 'A.doStuff()'Use
method hiding
along withnew
andvirtual
keyword in class Mapper and class B as follows:Since B is effectively A through inheritance and the method is overloaded.