inheritance - C# access children's static member in parent -
i have class foo, base class lot other classes such bar , baz, , want calculation within foo using static members in bar , baz, shown below:
public class foo{ public result1 { get{ return field1; } } } public class bar : foo{ public const int field1 = 5; } public class baz : foo{ public const int field1 = 10; }
the solution can think of wrap fields in container, add identifier each object, , use function return fields, so
bar : foo{ public readonly int id = 0; public static wrapper wrapper; } public wrapper getwrapper(int id){ switch(id){ case 0: return bar.wrapper; } }
however, can see, need maintain 1 additional class , function, , i'd rather not fragment code. there alternative?
edit
what asking for, i.e. accessing static
or const
value in subclass base class technically possible, doing violate principals of solid oo design. also, since need instance of specific subclass in order able 'reason over' type of subclass in order obtain appropriate field1
, there's little point approaching problem statically.
instead, common, cleaner, approach here use subtype polymorphicism allow calling method in base class, or method in external class altogether, access appropriate value 'field1' based on subclass. allows control on value returned remain inside appropriate subclasses (i.e. per words, code won't become "fragmented").
alternative solution using subclass polymorphicism (recommended)
a subclass polymorphic approach (i.e. virtual/abstract
, override
keywords) allow encapsulate retrieval of value (or object) customizable each subclass. here, abstraction remains conceptually @ "give me integer value", , sub-class-specific implementations of 'how' return value can abstracted (hidden) caller. also, marking base property abstract
, force subclasses implement property, requirement provide value isn't forgotten about.
i.e. recommend polymorphic approach this:
public abstract class foo { public abstract int result { get; } } public class bar : foo { // implementation specific. hide it. private const int field1 = 5; public override int result { { return field1; } } } public class baz : foo { public override int result { // no need implementation constant ... { return theresultofareallycomplexcalculationhere(); } } }
if there no other reusable concrete methods on base class foo
, model abstraction interface, same effect:
public interface ifoo { int result { get; } }
approaching problem without polymorphicism (not recommended)
any compile-time attempt access static fields on subclasses typically require code somewhere switch (or map) on type of subclass instance, e.g.:
public class foo { public int result1 { { switch(this.gettype().name) { case "bar": return bar.field1; case "baz": return baz.field1; default: return 0; } } } public void methodrequiringvaluefromsubclass() { console.writeline(result1); } } public class bar : foo { public const int field1 = 5; } public class baz : foo { public const int field1 = 10; }
the problem here open , closed principal violated, each time new sub class added, result1
method need changed accomodate new class.
Comments
Post a Comment