Inheritance on static variables?
- From: shooter@xxxxxxxxx
- Date: 25 Oct 2005 01:18:06 -0700
How does one make a function in a base class slect the appopriate
version of an overriden static variable.
Code-splination follows:
public class letter
{
public static int PostCodeDigits = 4;
public void grow()
{PostCodeDigits++;}
public virtual void shrink()
{PostCodeDigits--;}
public virtual string desc()
{return PostCodeDigits.ToString();}
}
public class parcil : letter
{
public static new int PostCodeDigits = 8;
public override void shrink()
{PostCodeDigits--;}
public override string desc()
{return PostCodeDigits.ToString() + "!";}
}
Examble call:
letter a, b;
a = new letter();
b = new parcil();
Console.WriteLine("a = {0}, b = {1}", a.desc(), b.desc());
a.grow();
b.grow();
Console.WriteLine("a = {0}, b = {1}", a.desc(), b.desc());
a.shrink();
b.shrink();
Console.WriteLine("a = {0}, b = {1}", a.desc(), b.desc());
output:
a = 4, b = 8!
a = 6, b = 8!
a = 5, b = 7!
So shrink works, but grow only effects the base class.
It quickly becomes impractical overide all functions that asses the
overriden static feild.
Is there a way in c# to make the "grow function" work as per the shrink
function" without overriding it in all inherited classes.
.
- Follow-Ups:
- Re: Inheritance on static variables?
- From: Ollie Riches
- Re: Inheritance on static variables?
- From: shooter
- Re: Inheritance on static variables?
- Prev by Date: Re: Reading a keystroke in a Console app
- Next by Date: Re: Inheritance on static variables?
- Previous by thread: Re: using a frame of an animated gif pic.
- Next by thread: Re: Inheritance on static variables?
- Index(es):
Relevant Pages
|