java - static inheritance: is it possible? Are there better solutions? -


consider example (warning-very bad code):

public abstract class {      static float foo;      public static void loadfoo(float incomingfoo) {         foo = incomingfoo;     }     public static void displayfoo() {         system.out.println("your foo is" +foo);     }  } 

class b extends class

public class b extends {      static float foo;      //@override (overide not allowed static methods.  dis problem...)     public static void loadfoo(float incomingfoo){         foo = incomingfoo;     } } 

class c pretty same b

public class c extends {     static float foo;      //@override      public static void loadfoo(float incomingfoo) {         //i  different static variable loaded class using method         foo = incomingfoo;     } } 

finally main class runs thing

public class main {      public static void main(string whatever[]){         b.loadfoo(5);         c.loadfoo(8);         b.displayfoo();         c.displayfoo();     } } 

so output of :

your foo is0.0 foo is0.0 

and aware because displayfoo class reference static foo in class a, please disregard this. assume have been specific enough describing problem , goal. solutions anyone?

edit: feel idiot forgot state wanted accomplish, want b , c have there own static variables loaded them without altering a's variable, should default.

it looks need static access 2 stateful objects same structure. in case, enum might solution:

public enum {   b, c;    private float foo;   // getter , (optional) setter foo here    public void displayfoo() { system.out.println("this foo  " + foo); } } 

this way can still access object statically, don't need duplicate else:

a.b.setfoo(5); a.c.setfoo(8); a.b.displayfoo(); // 5 a.c.displayfoo(); // 8 

if need static default, make method on a:

enum {   getdefault() { return a.b; } }  a.getdefault().displayfoo(); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -