刚刚在QQ问问里有人提到这样的问题,
问题是这样的:// 为什么super(x,y), 父类Point中x,y必须是静态的?
我也没弄懂,
于是发上来请各位大哥帮帮解释一下:public class Point {
protected static int x;
protected static int y; public Point(int x, int y) {
this.x = x;
this.y = y;
} public String getPosition() {
return ("(" + this.x + "," + this.y + ")");
} public void draw() { System.out.println("在坐标:x:" + x + ",y:" + y + "处画一个点");
}
}class Trigle extends Point {
public Point a;
public Point b;
public Point c; public Trigle(int x1, int y1, int x2, int y2, int x3, int y3) {
super(x, y); // 为什么super(x,y), 父类Point中x,y必须是静态的?
Trigle(x1, y1, x2, y2, x3, y3);
} public void Trigle(int x1, int y1, int x2, int y2, int x3, int y3) {
this.a = new Point(x1, y1);
this.b = new Point(x2, y2);
this.c = new Point(x3, y3);
} public void drawTrigle(int x1, int y1, int x2, int y2, int x3, int y3) { System.out.println("三角形三个顶点为:a:" + a.getPosition() + ",b:"
+ b.getPosition() + ",c:" + c.getPosition()); }
}
问题是这样的:// 为什么super(x,y), 父类Point中x,y必须是静态的?
我也没弄懂,
于是发上来请各位大哥帮帮解释一下:public class Point {
protected static int x;
protected static int y; public Point(int x, int y) {
this.x = x;
this.y = y;
} public String getPosition() {
return ("(" + this.x + "," + this.y + ")");
} public void draw() { System.out.println("在坐标:x:" + x + ",y:" + y + "处画一个点");
}
}class Trigle extends Point {
public Point a;
public Point b;
public Point c; public Trigle(int x1, int y1, int x2, int y2, int x3, int y3) {
super(x, y); // 为什么super(x,y), 父类Point中x,y必须是静态的?
Trigle(x1, y1, x2, y2, x3, y3);
} public void Trigle(int x1, int y1, int x2, int y2, int x3, int y3) {
this.a = new Point(x1, y1);
this.b = new Point(x2, y2);
this.c = new Point(x3, y3);
} public void drawTrigle(int x1, int y1, int x2, int y2, int x3, int y3) { System.out.println("三角形三个顶点为:a:" + a.getPosition() + ",b:"
+ b.getPosition() + ",c:" + c.getPosition()); }
}
public class Point {
protected int x;
protected int y; public Point(int x, int y) {
this.x = x;
this.y = y;
} public String getPosition() {
return ("(" + this.x + "," + this.y + ")");
} public void draw() { System.out.println("在坐标:x:" + x + ",y:" + y + "处画一个点");
}
}class Trigle extends Point {
public Point a;
public Point b;
public Point c; public Trigle(int x1, int y1, int x2, int y2, int x3, int y3) {
//super(x, y); // 为什么super(x,y), 父类Point中x,y必须是静态的?
super(x1,y1);//super有两个作用,一个是在构造方法中调用父类的构造方法,就像这个。
Trigle(x1, y1, x2, y2, x3, y3);
} public void Trigle(int x1, int y1, int x2, int y2, int x3, int y3) {
this.a = new Point(x1, y1);
this.b = new Point(x2, y2);
this.c = new Point(x3, y3);
} public void drawTrigle(int x1, int y1, int x2, int y2, int x3, int y3) {
super.draw();//第二个就是调用父类的方法
System.out.println("三角形三个顶点为:a:" + a.getPosition() + ",b:"
+ b.getPosition() + ",c:" + c.getPosition()); }
//下面来说说,你的问题,因为你在Trigle类中并没有定义x,y。所以super(x, y)的时候的x,y
//是从父类Point中继承来的;在执行super(x, y)时还没有产生父类的对象,所以不能访问父类的非static的属性,
//为了在子类的构造方法中能访问,必须要定义成类属性(即static属性)
}在程序中注释中说明了,楼主可以看看!
1 Trigle 继承 Point 是错误。没有什么 is-a 这种关系。
2 对super的调用也是错用。
“X Y并不一定必须要是静态的,不声明STATIC静态的也可以用,只不过调用的时候必须实例化对象,这样会占用内存的。但是你的X Y并不会随着对象的变化而调用方法的效果也会变化,它与具体对象无关。为了减少占用内存,能用静态的尽量用静态!直接用类名+static的方法名或变量名引用就可以了。”
super(x,y)时,如果父类的x,y不是static的,那么如果创建了一个子类对象,但是并没有创建父类的对象,那么当调用super(x,y)时就会出错,因为就没有实际的x,y与之对应。
和你写super(a,b)错误效果一样的,这样你应该就能理解了吧
看的很蛋疼的程序,貌似是一个固定点,和3个非固定点 的故事?
protected static int x;
protected static int y; public Point(int x, int y) {
this.x = x;
this.y = y;
}
//这个着实被累到了,首先是 通过this.来对类变量进行赋值……
再来,类变量时公用的……这样一弄还有三角形?都是一个点了嘛……你就算再定义1000个点,其实都是一个点诶……
继续,严重逻辑错误……继承也是让我很无语……那个super(x,y)着实让我震惊
super(x, y); // 为什么super(x,y), 父类Point中x,y必须是静态的?
Trigle(x1, y1, x2, y2, x3, y3);
}
在这个构造方法是参数中,似乎你没有 X Y两个参数