如何覆盖类的静态变量
我正在使用一个库,它有一个公共静态浮点变量我想知道如何覆盖它 我想这不会发生吧?
//Settings.java
package org.jbox2d.common;
public class Settings {
public static float velocityThreashold = 1.0f;
}
//MyClass.class
package org.jbox2d.common;
import com.otherlibrary
public class MyClass {
}
谢谢
I'm using a library it has a public static float variable I want to know how to override it
I guess that's not going to happen?
//Settings.java
package org.jbox2d.common;
public class Settings {
public static float velocityThreashold = 1.0f;
}
//MyClass.class
package org.jbox2d.common;
import com.otherlibrary
public class MyClass {
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能覆盖 Java 中的成员变量。您可以使用称为字段隐藏的东西来代替。看看这个 。
但是,在您的示例中,velocityThreashold 不是最终的,因此您可以更改它的值。
You can not override member variables in Java. You can use something called field hiding instead. Have a look at this.
However in your example
velocityThreashold
isn't final so you can change it's value.示例中的velocityThreashold变量不是最终变量,也不是实例变量,因此从技术上讲不能被覆盖。
您可以做的是将velocityThreashold 的值设置为您想要的任何值,因为它是公共的。
我认为您想要做的事情如下:
The velocityThreashold variable in your example is not final, nor is it an instance variable and therefore cannot technically be overridden.
What you can do is set the value of velocityThreashold to any value you want since it's public.
I think what you're going to want to do is something like the following:
覆盖变量?如果您指的是方法,那么:
您不能覆盖静态方法,因为静态元素不是继承的
Override variable? If you mean method then:
You can't override static method because static elements isn't inherited
你不能,这就是他们最终的目的。
至于您的代码,您没有最终变量,因此您只需更改该值即可。
You can't, that's the point of them being final.
As for you code you don't have a final variable so you just have to change the value and that's it.