[英]How do I use a specific variable of an object that is in another class?
I'm trying to modify the toString method.我正在尝试修改 toString 方法。 I have an object called 'p' which has 2 doubles as attributes, in this case, 5.0 and 6.0 which are 'x' and 'y' values, respectively.
我有一个名为 'p' 的对象,它有 2 个双打属性,在这种情况下,5.0 和 6.0 分别是 'x' 和 'y' 值。
The brackets, inside the String converter "< Point >", should be printing the "x" of p, the "y" of p, while in the circle it should print the radius.字符串转换器“< Point >”内的括号应该打印 p 的“x”,p 的“y”,而在圆圈中它应该打印半径。 Sure enough printing the radius works, but I'm unsure as to how I'm supposed to specify "x" of p and "y" of p.
果然打印半径有效,但我不确定我应该如何指定 p 的“x”和 p 的“y”。
Class Circle:班级圈:
package packageName;
public class Circle {
public Point center;
public double radius;
public Circle(Point a, double b) {
this.center = a;
this.radius = b;
}
public String toString() {
String converter = "<Circle(<Point(" + (x of p) + ", " + (y of p) + ")>, " + this.radius + ")>";
return converter;
}
public static void main(String args []) {
Point p = new Point(5.0, 6.0);
Circle c = new Circle(p, 4.0);
c.toString();
}
}
Class Point:类点:
package packageName;
public class Point{
public double x;
public double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public String toString() {
String input = "<Point(" + this.x + ", " + this.y + ")>";
return input;
}
}
You are saying that you want to print "x" of "p" and "y" of "p" in the toString
method of Cirlce
, but toString
does not know anything about p
because p
is declared locally in the main
method.您是说要在
Cirlce
的toString
方法中打印“p”的“x”和“p”的“y”,但是toString
对p
Cirlce
,因为p
在main
方法中本地声明。
In the main
method, you created p
and passed it to the first parameter of Circle
, which then gets assigned to center
.在
main
方法中,您创建了p
并将其传递给Circle
的第一个参数,然后将其分配给center
。 So center
stores the same thing as p
.所以
center
存储与p
相同的东西。 You should use center.x
and center.y
:你应该使用
center.x
和center.y
:
String converter = "<Circle(<Point(" + center,x + ", " + center.y + ")>, " + this.radius + ")>";
return converter;
Alternatively, you could call center.toString()
directly:或者,您可以直接调用
center.toString()
:
String converter = "<Circle(" + c.toString() + ", " + this.radius + ")>";
return converter;
Notice how I used the syntax foo.bar
to mean "bar of foo".请注意我如何使用语法
foo.bar
来表示“bar of foo”。 This is dot notation, and it seems like you are unfamiliar with this.这是点符号,您似乎对此不熟悉。
p
is a local variable to the main
method, so the variable p
itself cannot be used at the place where you want use it. p
是main
方法的局部变量,因此变量p
本身不能在您想要使用它的地方使用。
But I have good news – you're passing in a Point
instance as argument to the Circle
constructor, and you're storing it in the center
field.但我有一个好消息——您将
Point
实例作为参数传递给Circle
构造函数,并将其存储在center
字段中。
You can reference it as this.center
or just center
.您可以将其引用为
this.center
或仅使用center
。 To reference x
of the specified Point
instance, use要引用指定
Point
实例的x
,请使用
this.center.x
You can use center.x and center.y as follows:您可以使用 center.x 和 center.y 如下:
String converter = "<Circle(<Point(" + this.center.x + ", " + this.center.y + ")>, " + this.radius + ")>";
Or you just make x and y variable of Point class as private and use getter method as follows:或者您只需将 Point 类的 x 和 y 变量设为私有并使用 getter 方法,如下所示:
private double x;
private double y;
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
And use并使用
String converter = "<Circle(<Point(" + this.center.getX() + ", " + this.center.getY() + ")>, " + this.radius + ")>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.