简体   繁体   中英

How do I use a specific variable of an object that is in another class?

I'm trying to modify the toString method. 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.

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. 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.

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.

In the main method, you created p and passed it to the first parameter of Circle , which then gets assigned to center . So center stores the same thing as p . You should use center.x and center.y :

String converter = "<Circle(<Point(" + center,x + ", " + center.y + ")>, " + this.radius + ")>";
return converter;

Alternatively, you could call center.toString() directly:

String converter = "<Circle(" + c.toString() + ", " + this.radius + ")>";
return converter;

Notice how I used the syntax foo.bar to mean "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.

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.

You can reference it as this.center or just center . To reference x of the specified Point instance, use

this.center.x

You can use center.x and center.y as follows:

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:

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 + ")>"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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