简体   繁体   English

如何使用另一个类中的对象的特定变量?

[英]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.您是说要在CirlcetoString方法中打印“p”的“x”和“p”的“y”,但是toStringp Cirlce ,因为pmain方法中本地声明。

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.xcenter.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. pmain方法的局部变量,因此变量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.

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