/**定义一个点(Point)类用来表示三维空间中的点(有三个坐标).要求如下:
1.可以生成具有特定坐标的点对象.
2.提供可以设置三个坐标的方法.
3.提供可以计算改点距原点距离平方的方法.
4.变现程序验证上述三条.
*/
class Point{
int x;
int y;
int z;
int Point(int _x, int _y , int _z){
x = _x;
y = _y;
z = _z;
return x*x + y*y + z*z;
}
void setX(int x1){
x = x1;
}
void setY(int y1){
y = y1;
}
void setZ(int z1){
z = z1;
}
}
public class Dian{
public static void main (String[] args){
Point p = new Point(1, 2, 3);
System.out.println(p.Point());
}
}
1.可以生成具有特定坐标的点对象.
2.提供可以设置三个坐标的方法.
3.提供可以计算改点距原点距离平方的方法.
4.变现程序验证上述三条.
*/
class Point{
int x;
int y;
int z;
int Point(int _x, int _y , int _z){
x = _x;
y = _y;
z = _z;
return x*x + y*y + z*z;
}
void setX(int x1){
x = x1;
}
void setY(int y1){
y = y1;
}
void setZ(int z1){
z = z1;
}
}
public class Dian{
public static void main (String[] args){
Point p = new Point(1, 2, 3);
System.out.println(p.Point());
}
}
所以你System.out.println(p.Point());
调用不了`````要出错!
x = _x;
y = _y;
z = _z;
return x*x + y*y + z*z;
} 改写
Point(int _x, int _y , int _z){
}
就可以了!
public static void main (String[] args){
Point p = new Point(1, 2, 3); System.out.println(p.Point());
[/code]你想实例化Point,但是你未定义构造器,这时虚拟机会为你设置一个默认的无参数的构造器,所以你通过
Point p = new Point(1, 2, 3); 就无法初始化你属性中的x,y,z也就是你只能Point p = new Point();实例这个对象.
具体修改如下: class Point{
int x;
int y;
int z; Point(int _x, int _y , int _z){
x = _x;
y = _y;
z = _z;
} void setX(int x1){
x = x1;
}
void setY(int y1){
y = y1;
}
void setZ(int z1){
z = z1;
}
int Point(int _x, int _y , int _z){
x = _x;
y = _y;
z = _z;
return x*x + y*y + z*z;
} } public class Dian{
public static void main (String[] args){
Point p = new Point(1, 2, 3);
System.out.println(p.Point(1,2,3));}
}
最好解决方法 class Point{
int x;
int y;
int z; Point(int _x, int _y , int _z){
x = _x;
y = _y;
z = _z;
} void setX(int x1){
x = x1;
}
void setY(int y1){
y = y1;
}
void setZ(int z1){
z = z1;
}
int getpoint(){
return x*x + y*y + z*z;
} } public class Dian{
public static void main (String[] args){
Point p = new Point(1, 2, 3);
System.out.println(p.getpoint());}
}