3 回答
![?](http://img1.sycdn.imooc.com/5333a1bc00014e8302000200-100-100.jpg)
TA贡献1827条经验 获得超9个赞
您是说您想在 的方法中打印“p”的“x”和“p”的“y” toString,Cirlce但toString不知道任何事情,p因为p在方法中本地声明main。
在该main方法中,您创建p并将其传递给 的第一个参数Circle,然后将其分配给center。所以center存储与p. 你应该使用center.xand center.y:
String converter = "<Circle(<Point(" + center,x + ", " + center.y + ")>, " + this.radius + ")>";
return converter;
或者,您可以center.toString()直接致电:
String converter = "<Circle(" + c.toString() + ", " + this.radius + ")>";
return converter;
请注意我是如何使用语法foo.bar来表示“foo 的 bar”的。这是点符号,您似乎对此不熟悉。
![?](http://img1.sycdn.imooc.com/5458643d0001a93c02200220-100-100.jpg)
TA贡献1878条经验 获得超4个赞
p
是main
方法的局部变量,因此变量p
本身不能在您要使用的地方使用。
但我有个好消息——您将Point
实例作为参数传递给Circle
构造函数,并将其存储在center
字段中。
您可以将其引用为this.center
或只是center
. 要引用x
指定的Point
实例,请使用
this.center.x
![?](http://img1.sycdn.imooc.com/533e4cde000148e602000200-100-100.jpg)
TA贡献1895条经验 获得超7个赞
您可以使用 center.x 和 center.y,如下所示:
String converter = "<Circle(<Point(" + this.center.x + ", " + this.center.y + ")>, " + this.radius + ")>";
或者您只需将 Point 类的 x 和 y 变量设为私有并使用 getter 方法,如下所示:
private double x;
private double y;
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
并使用
String converter = "<Circle(<Point(" + this.center.getX() + ", " + this.center.getY() + ")>, " + this.radius + ")>";
添加回答
举报