以下是我的项目代码的一部分:
public class Body extends Point{ public double x,y,mass; public Body() { x = y = mass = 0; } public Body(double x,double y,double mass) { this.mass = mass; this.x = x; this.y = y; } } public class Point { public double x; public double y; public Point(double x,double y) { this.x = x; this.y = y; } }
我很快意识到,这样做会在Body类中创建两个变量,称为x,另外两个变量在Body中称为y.这甚至是甚么可能的,为什么Java甚至可以让它呢?
我假设这是Body的正确代码:
public class Body extends Point{ public double mass; public Body() { super(); mass = 0; } public Body(double x,double mass) { super(x,y); this.mass = mass; } }
谢谢你的时间