简体   繁体   中英

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

Consider this example (warning-very bad code):

public abstract class A {

    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 A

public class B extends A {

    static float foo;

    //@Override (overide is not allowed for static methods.  dis is a problem...)
    public static void loadfoo(float incomingfoo){
        foo = incomingfoo;
    }
}

Class C is pretty much the same as B

public class C extends A {
    static float foo;

    //@Override 
    public static void loadfoo(float incomingfoo) {
        //I would like  a different static variable loaded into this class using this method
        foo = incomingfoo;
    }
}

finally the main Class runs the thing

public class Main {

    public static void main(String whatever[]){
        B.loadfoo(5);
        C.loadfoo(8);
        B.displayfoo();
        C.displayfoo();
    }
}

so the output of this is :

your foo is0.0
your foo is0.0

and I am aware this is because the displayfoo class reference the static foo in Class A, so please disregard this. I assume I have now been specific enough about describing my problem and goal. solutions anyone?

Edit: I feel like an idiot I completely forgot to actually state what I wanted to accomplish, but really all I want is for B and C to have there own static variables loaded into them without altering A's variable, which should be the default.

It looks like you need static access to two stateful objects with the same structure. In this case, an enum might be a solution:

public enum A {
  B, C;

  private float foo;
  // getter and (optional) setter for foo here

  public void displayFoo() { System.out.println("This foo  is " + foo); }
}

This way you can still access your object statically, but don't need to duplicate anything else:

A.B.setFoo(5);
A.C.setFoo(8);
A.B.displayFoo(); // 5
A.C.displayFoo(); // 8

If you then need a static default, I would make it a method on A:

enum A {
  A getDefault() { return A.B; }
}

A.getDefault().displayFoo();

It seems that first you want to load the values using loadfoo to foo and then display the value of that foo using the displayfoo method. Well, I don't think there is anyway to do it using static methods.You can do this by making displayfoo() method abstract and overriding the same in the subclasses B and C .

Here is the code:

abstract class A {
     float foo;
     public void loadfoo(float incomingfoo){
        foo = incomingfoo;
     }
     public abstract void displayfoo();
}

class B extends A{
    @Override 
    public void loadfoo(float incomingfoo){
        foo = incomingfoo;
    }
    @Override
    public void displayfoo(){
        System.out.println("foo is " + foo);
    }
}

class C extends A{
    @Override 
    public void loadfoo(float incomingfoo){
        this.foo = incomingfoo;
    }
    @Override
    public void displayfoo(){
        System.out.println("foo is " + foo);
    }
}

public class Main {
    public static void main(String whatever[]){
         B b = new B();
         C c = new C();
         b.loadfoo(5);
         c.loadfoo(5);
         b.displayfoo();
         c.displayfoo();
    }
}

You can also check the same kind of question here .

Static methods should be used by static method access and not by object instance. It's not supposed to be virtual because it's not belong to the object.

  • If you call B.loadfoo() then a method of B class is called.
  • If you call C.loadfoo() then a method of C class is called.
  • You cannot call a static method if it doesn't exist in the class.

There's no point to use static methods if you want to use polimorphism.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM