如何在 Java 类方法中访问私有变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14087002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access private variables in a Java class method
提问by ANemati
I am trying to access a private variable (x
) in my method distanceFromPoint
but it seems it doesn't work. How can I access it? My method is returning 0.0
all the time regardless of other values.
我试图x
在我的方法中访问一个私有变量 ( )distanceFromPoint
但它似乎不起作用。我怎样才能访问它?0.0
无论其他值如何,我的方法一直在返回。
Code
代码
public class Pointdeclare {
private static int x;
private static int y;
Pointdeclare (int x_ , int y_ ){
this.x = x_;
this.y = y_;
}
int getX(){
return x;
}
int getY(){
return y;
}
static double distanceFromZero (){
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
double distanceFromPoint(Pointdeclare point){
int distX = point.getX()- this.x;
int distY = point.getY()- this.y;
return (double) Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2));
}
}
Main Class
主班
public class main {
static Pointdeclare p1 = new Pointdeclare(6, 7);
static Pointdeclare p2 = new Pointdeclare(3, 7);
public static void main (String[] args){
System.out.println(p2.distanceFromZero());
System.out.print(p1.distanceFromPoint(p2));
}
}
回答by duffymo
This will work better for you:
这对你来说会更好:
package cruft;
/**
* Point2 description here
* @author Michael
* @link http://stackoverflow.com/questions/14087002/how-to-access-private-variables-in-a-java-class-method
* @since 12/29/12 6:52 PM
*/
public class Point2 {
private int x;
private int y;
Point2(int x_, int y_) {
this.x = x_;
this.y = y_;
}
int getX() {
return x;
}
int getY() {
return y;
}
double distanceFromZero() {
return distanceFromPoint(new Point2(0, 0));
}
double distanceFromPoint(Point2 point) {
int distX = point.getX()-this.x;
int distY = point.getY()-this.y;
return (double) Math.sqrt(Math.pow(distX, 2)+Math.pow(distY, 2));
}
public static void main(String[] args) {
Point2 p1 = new Point2(6, 7);
Point2 p2 = new Point2(3, 7);
System.out.println(p2.distanceFromZero());
System.out.print(p1.distanceFromPoint(p2));
}
}
回答by Kevin Bigler
I hope you find this relevant, it doesn't answer your question directly but I was recently informed my understanding of static
was incorrect so having just researched the topic I thought maybe I could help. Static
variables belong to a class and not its objects, objects of a class may access a static variable but no matter how many objects of the class there are there will only be one copy of the static variable. So I think what the people before me were saying is instead of p1 and p2 having their own copies of x and y both objects share the same x and y field therefore your value returned is 0. In other worlds your trying to find the distance between one location, it will always be zero. Hopefully that helps :-). I'm sorry I missed the first line of the main method. p2 should return a value as long as it isn't zero but p1 will not.
我希望你觉得这很相关,它没有直接回答你的问题,但我最近被告知我的理解static
是不正确的,所以刚刚研究了这个话题,我想也许我可以提供帮助。Static
变量属于一个类而不是它的对象,一个类的对象可以访问一个静态变量,但无论该类有多少个对象,都只会有一个静态变量的副本。所以我认为我之前的人所说的是,而不是 p1 和 p2 拥有自己的 x 和 y 副本,这两个对象共享相同的 x 和 y 字段,因此您返回的值是 0。在其他世界中,您试图找到之间的距离一个位置,它将永远为零。希望这有帮助:-)。很抱歉我错过了 main 方法的第一行。只要它不为零,p2 就应该返回一个值,但 p1 不会。
回答by Pointy
You declared "x" and "y" static
, which means that they're class variables, not instance variables.
您声明了 "x" 和 "y" static
,这意味着它们是类变量,而不是实例变量。
Because of that, every new call to the constructor will overwrite the old values. Thus distanceFromPoint
always returns zero because there's only one x
and one y
.
因此,每次对构造函数的新调用都会覆盖旧值。因此distanceFromPoint
总是返回零,因为只有一x
和一y
。
回答by moonwave99
You shouldn't declare your class fields as static
, just leave them private
.
您不应该将您的类字段声明为static
,只需将它们保留即可private
。
Btw consider using Point
or Point2D
native classes from java.awt.geom
package.
顺便说一句,考虑使用包中的Point
或Point2D
本机类java.awt.geom
。
回答by TieDad
The problem is not due to private, instead, it's because of static. x is static. In your method, you should use x as:
问题不是由于私有,而是由于静态。x 是静态的。在您的方法中,您应该将 x 用作:
int distX = point.getX()- Pointdeclare.x; // You could use this.x because x is static.