Call overloaded base class' function inside static function?
public class A
{
public int F ()
{
// do something
}
}
public class B : A
{
public static B F ()
{
B b = new B ();
// do something more with b
b.base.F (); // this doesn't work
b.A.F (); // this doesn't work either
return b;
}
开发者_如何学Go}
What is the proper syntax for calling A.F()? Please regard that B.F() is a static function.
Edit regarding the "change the name comments":
Please don't make the mistake to conclude from this stripped down to the essential example. Actually the functions are meant to read XML, and the function signature is ReadXML (XmlNode parent, int id)
across all affected classes.
the following will work (tested now so a proper answer) to call the F method of the A class.
public static B F()
{
B b = new B();
// do something more with b
((A)b).F(); // this works
return b;
}
You might like to think about changing the names. I find it hard to believe that a descriptive method name would be the same for something that returns an int and something that returns a class B. Perhaps the latter should have a Create in there somewhere since it seems to be some sort of factory method?
I think it is impossible to use any keyword which needs to work with instance/inheritance, like base
or this
in static methods. As calling F()
in class B
there is no instance of class B
so you can't refer to any base
.
As an option you can create object of class A
in B.F()
and call F()
of class A
like a.F()
, or you need to make F()
in A
as static
and call A.F()
from B.F()
.
精彩评论