简体   繁体   English

静态继承:可能吗? 有更好的解决方案吗?

[英]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 B级扩展了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 C类与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. 并且我知道这是因为displayfoo类引用了A类中的静态foo,因此请忽略此内容。 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. 编辑:我感觉自己像个白痴,我完全忘了说出我想完成的事情,但实际上我想要的只是让B和C在不更改A变量的情况下将自己的静态变量加载到其中,这应该是默认值。

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: 如果您随后需要静态默认值,则可以将其作为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. 看来,首先您想使用loadfoo将值loadfoofoo ,然后使用displayfoo方法显示该foo的值。 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 . 好吧,我认为没有任何方法可以使用静态方法来实现,您可以通过将displayfoo()方法抽象化并在子类BC覆盖相同的方法来实现。

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. 如果调用B.loadfoo()则将调用B类的方法。
  • If you call C.loadfoo() then a method of C class is called. 如果调用C.loadfoo()则将调用C类的方法。
  • 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. 如果要使用多态性,则没有必要使用静态方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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