谁帮帮我看看java代码错在哪?

来源:百度知道 编辑:UC知道 时间:2024/06/15 10:17:12
package A;

public class Point2D {
private int x;
private int y;
public Point2D()
{}
public Point2D(int x,int y)
{
this.x=x;
this.y=y;
}
public void setX(int x){this.x=x;}
public void getY(int y){this.y=y;}

public int getX(){return x;}
public int getY(){return y;}

class Point3D extends Point2D
{
private int z;
Point3D()
{
super();
}
Point3D(int x,int y,int z)
{
super(x,y);
this.z=z;
}
public void setZ(int z){this.z=z;}
public int getZ(){return z;}
}

/**
* @param args
*
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Point3D aa=new Point3D();
Point3D bb=new Point3D(1,4,6);
System.out.println(aa.getX()+aa.getY()+aa.getZ());
System.out.println(bb.getX()+bb.ge

lz在Point2D 内部又声明了他的子类Point3D,那么Point3D是不能够被直接访问到的,也就不能实例化,这就是 为什么这两句:
Point3D aa=new Point3D();
Point3D bb=new Point3D(1,4,6);
通不过编译的原因.这是一个概念理解的问题。建议楼主,将该类改写成两个类,如下:
===========
Point2D.java
------------
package A;
public class Point2D {
private int x;

private int y;

public Point2D() {
}

public Point2D(int x, int y) {
this.x = x;
this.y = y;
}

public void setX(int x) {
this.x = x;
}

public void getY(int y) {
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

}
------------
Point3D.java
------------
package A;
public class Point3D extends Point2D {
private int z;

Point3D() {
super();
}

Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}