简体   繁体   English

我如何在超类方法中使用子类的静态字段

[英]How do I use static field of subclass in super class method

I have a code on my super class that want WIDTH and HEGHT from subclass to calculate 我的超类上有一个代码,希望子类的WIDTH和HEGHT可以计算

public SuperClass{
    public Vector2 getCenter(){
        float x = this.position.x + WIDTH/2;
        float y = this.position.y + HEIGHT/2;
        return new Vector2(x, y);
    }

    public void setCenter(Vector2 position){
        float x = position.x - WIDTH/2;
        float y = position.y - HEIGHT/2;
        setPosition(new Vector2(x, y));
    }
}

I have to use static field of WIDTH and HEIGHT but it compilation error cause super class still don't know where is WIDTH and HEIGHT. 我必须使用WIDTH和HEIGHT的静态字段,但是它编译错误导致超类仍然不知道WIDTH和HEIGHT在哪里。 Can I have static field that overrided in subclass? 我可以在子类中覆盖静态字段吗?

You can't do that. 你不能那样做。 The whole point of polymorphism is that super classes know nothing about child classes! 多态的全部意义在于,超级类对子类一无所知

In other words: dependency flows "upwards" only. 换句话说:依赖关系仅“向上”流动。 A child class depends on its superclass, but not vice versa. 子类取决于其父类,但反之则不然。

If at all, you can do something like this: 如果有的话,您可以执行以下操作:

public abstract class SuperClass {
  protected abstract int getWidth();

to then call getWidth() within your base class algorithms; 然后在基类算法中调用getWidth(); and your child classes then have to implement those methods - that is how you actually do proper OO design following the Open/closed principle . 然后您的子类必须实现这些方法-这就是您实际上遵循“ 打开/关闭”原理进行正确的OO设计的方式。

So, the solution here is: you step back and do some more reading on OO design; 因此,这里的解决方案是:您退后一步,对OO设计进行更多阅读; how to do it; 怎么做; and how it actually works with Java. 以及它在Java中的实际工作方式。 And for the record: you really want to stay way from using static - that is an abnormality in good OO design; 记录在案:您真的想远离使用静态方法 -在良好的OO设计中这是一个异常 and should only be used when you have really good reasons to do so! 仅在确实有充分理由时才使用!

暂无
暂无

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

相关问题 Java:如何使用超类的静态方法从其创建子类的实例 - Java: How to Create an instance of a subclass from with a Static method of the super class 如何防止子类继承父类中的静态方法 - How to prevent a subclass inherit a static method in the super class 如何从子类的静态方法获取类? - How do I get the class from a static method of a subclass? 我可以在不重写的情况下使用子类中超类的方法吗? - Can I use a method from a super class in a subclass without overriding it? 如果我在子类中有一个要求超类对象的方法,我该如何区分两者? - If I have a method in a subclass that asks for an object of the super class, how do i distinguish both? 通过子类实例调用时,如何在子类的方法中使用子类的静态字段? - How do I use a subclass' static fields in superclass' method when calling via instance of subclass? 如何覆盖子类中超类的属性? - How do I override an attribute from a super class in a subclass? 如何在子类的超类的超类中调用方法 - How to call a method in super class of super class of a subclass 如何在超类的静态字段上同步访问? - How to synchronize access on a static field of a super class? 如何使用多态性访问未在超 class 中使用超类的引用变量定义的子类中的方法 - How to use polymorphism to access a method inside a subclass that is not defined in the super class using a reference variable of a superclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM