我正在使用繼承和多態。 我只得到這個輸出而不是顯示所需的點。意外的輸出NULL
但是打印點本身工作正常。打印線會產生問題。
輸出: 連線空空
package serial;
import java.io.*;
class Point
{
private int x,y;
Point(int x,int y)
{
this.x = x;
this.y = y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
void setX(int x)
{
this.x = x;
}
void setY(int y)
{
this.y = y;
}
public String toString()
{
//String s = "Points joining line are ("+p1.getX()+","+p1.getY()+") and ("+p2.getX()+","+p2.getY()+")";
return "(" + x + "," + y + ")";
}
}
class Line
{
private Point p1, p2;
Line()
{
}
Line(Point p1, Point p2)
{
p1 = new Point(2,2);
p2 = new Point(3,3);
}
void setP1(Point p1)
{
p1.setX(2);
p1.setY(2);
}
Point getP1()
{
return p1;
}
void setP2(Point p2)
{
p2.setX(3);
p2.setY(3);
}
Point getP2()
{
return p2;
}
public String toString()
{
String str;
str = "line joining "+this.p1+" "+this.p2+"";
return str;
}
}
public class chumma {
public static void main(String args[])
{
Point p1 = new Point(2,2);
Point p2 = new Point(3,3);
Line l1 = new Line(p1,p2);
//l1.setP1(p1);
//l1.setP2(p2);
System.out.println(l1);
}
}
呃...謝謝你。這是我身邊的一個愚蠢的錯誤。我沒有注意到。 – Sam