import java.Math.*;public class Line extends Point{ Point point1; Point point2; int w;
public void move(){ w=Math.sqrt((point1.x-point2.x)^2+(point1.y-point2.y)^2) } }
3楼的代码能运行?自身包含自身,很明显的死循环。这个题有两种设计思路(本人推荐第二种):1. Line继承自Point: [code] //Point.java public class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } }//Line.java public class Line extends Point { int x1, y1, w; public Line(int startX, int startY, int endX, int endY, int width) { super(startX, startY); x1 = endX; y1 = endY; w = width; } public void move(int newStartX, int newStartY, int newEndX, int newEndY) { x = newStartX; y = newStartY; x1 = newEndX; y1 = newEndY; } public void print() { System.out.println("Line: x=" + x + "; y=" + y + "; x1="+ x1 +"; y1=" + y1 + "; w=" + w); } }[/code] 2. Line组合Point: [code] // Point类定义同上// Line.java public class Line { Point start, end; int w; public Line(int startX, int startY, int endX, int endY, int width) { start = new Point(startX, startY); end = new Point(endX, endY); w = width; } public void move(int newStartX, int newStartY, int newEndX, int newEndY) { start = new Point(newStartX, newStartY); end = new Point(newEndX, newEndY); } public void print() { System.out.println("Line: start from (" + start.x+ "," + start.y + "), end at (" + end.x+ "," + end.y + "), width: " + w); } } [/code]
3楼的代码能运行?自身包含自身,很明显的死循环。这个题有两种设计思路(本人推荐第二种):1. Line继承自Point://Point.java public class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } }//Line.java public class Line extends Point { int x1, y1, w; public Line(int startX, int startY, int endX, int endY, int width) { super(startX, startY); x1 = endX; y1 = endY; w = width; } public void move(int newStartX, int newStartY, int newEndX, int newEndY) { x = newStartX; y = newStartY; x1 = newEndX; y1 = newEndY; } public void print() { System.out.println("Line: x=" + x + "; y=" + y + "; x1="+ x1 +"; y1=" + y1 + "; w=" + w); } } 2. Line组合Point:// Point类定义同上// Line.java public class Line { Point start, end; int w; public Line(int startX, int startY, int endX, int endY, int width) { start = new Point(startX, startY); end = new Point(endX, endY); w = width; } public void move(int newStartX, int newStartY, int newEndX, int newEndY) { start = new Point(newStartX, newStartY); end = new Point(newEndX, newEndY); } public void print() { System.out.println("Line: start from (" + start.x+ "," + start.y + "), end at (" + end.x+ "," + end.y + "), width: " + w); } }
import java.Math.*; public class Line extends Point { Point point1; Point point2; int w; public void move( Point point1, Point point2 ) { w = Math.sqrt( ( point1.x - point2.x ) ^2 + ( point1.y - point2.y ) ^2) } public void printPara() { System.out.println( "Point 1: (" + point1.x + ", " + point1.y + ")"); System.out.println( "Point 2: (" + point2.x + ", " + point2.y + ")"); System.out.println( "width: " + w ); } }
首先不明白你说的move()移动直线要的是什么结果,如果是按照三楼计算2点间直线长度,代码如下看看是否可以: public class Point { private int x; private int y; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; }} public class Line extends Point { private Point p1; private Point p2; private int w; public Line(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } public void move() { int length = Math.abs(this.p1.getX() - this.p2.getX()); int height = Math.abs(this.p1.getY() - this.p2.getY()); w = (int) Math.sqrt((length * length + height * height)); } public void print() { System.out.println("Point 1 is : x=" + this.p1.getX() + ",y=" + this.p1.getY()); System.out.println("Point 2 is : x=" + this.p2.getX() + ",y=" + this.p2.getY()); System.out.println("The line width is " + w); } public static void main(String[] args) { Point p1 = new Point(0, 0); Point p2 = new Point(3, 4); Line l = new Line(p1, p2); l.move(); l.print(); } }
Point point1;
Point point2;
int w;
public void move(){
w=Math.sqrt((point1.x-point2.x)^2+(point1.y-point2.y)^2) }
}
[code]
//Point.java
public class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}//Line.java
public class Line extends Point {
int x1, y1, w;
public Line(int startX, int startY, int endX, int endY, int width) {
super(startX, startY);
x1 = endX;
y1 = endY;
w = width;
}
public void move(int newStartX, int newStartY, int newEndX, int newEndY) {
x = newStartX;
y = newStartY;
x1 = newEndX;
y1 = newEndY;
}
public void print() {
System.out.println("Line: x=" + x + "; y=" + y + "; x1="+ x1 +"; y1=" + y1 + "; w=" + w);
}
}[/code]
2. Line组合Point:
[code]
// Point类定义同上// Line.java
public class Line {
Point start, end;
int w;
public Line(int startX, int startY, int endX, int endY, int width) {
start = new Point(startX, startY);
end = new Point(endX, endY);
w = width;
}
public void move(int newStartX, int newStartY, int newEndX, int newEndY) {
start = new Point(newStartX, newStartY);
end = new Point(newEndX, newEndY);
}
public void print() {
System.out.println("Line: start from (" + start.x+ "," + start.y + "), end at (" + end.x+ "," + end.y + "), width: " + w);
}
}
[/code]
public class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}//Line.java
public class Line extends Point {
int x1, y1, w;
public Line(int startX, int startY, int endX, int endY, int width) {
super(startX, startY);
x1 = endX;
y1 = endY;
w = width;
}
public void move(int newStartX, int newStartY, int newEndX, int newEndY) {
x = newStartX;
y = newStartY;
x1 = newEndX;
y1 = newEndY;
}
public void print() {
System.out.println("Line: x=" + x + "; y=" + y + "; x1="+ x1 +"; y1=" + y1 + "; w=" + w);
}
}
2. Line组合Point:// Point类定义同上// Line.java
public class Line {
Point start, end;
int w;
public Line(int startX, int startY, int endX, int endY, int width) {
start = new Point(startX, startY);
end = new Point(endX, endY);
w = width;
}
public void move(int newStartX, int newStartY, int newEndX, int newEndY) {
start = new Point(newStartX, newStartY);
end = new Point(newEndX, newEndY);
}
public void print() {
System.out.println("Line: start from (" + start.x+ "," + start.y + "), end at (" + end.x+ "," + end.y + "), width: " + w);
}
}
import java.Math.*;
public class Line extends Point
{
Point point1;
Point point2;
int w; public void move( Point point1, Point point2 )
{
w = Math.sqrt( ( point1.x - point2.x ) ^2 + ( point1.y - point2.y ) ^2)
} public void printPara()
{
System.out.println( "Point 1: (" + point1.x + ", " + point1.y + ")");
System.out.println( "Point 2: (" + point2.x + ", " + point2.y + ")");
System.out.println( "width: " + w );
}
}
public class Point { private int x; private int y; public Point() { } public Point(int x, int y) {
this.x = x;
this.y = y;
} public int getX() {
return x;
} public void setX(int x) {
this.x = x;
} public int getY() {
return y;
} public void setY(int y) {
this.y = y;
}}
public class Line extends Point { private Point p1; private Point p2; private int w; public Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
} public void move() {
int length = Math.abs(this.p1.getX() - this.p2.getX());
int height = Math.abs(this.p1.getY() - this.p2.getY());
w = (int) Math.sqrt((length * length + height * height));
} public void print() {
System.out.println("Point 1 is : x=" + this.p1.getX() + ",y="
+ this.p1.getY());
System.out.println("Point 2 is : x=" + this.p2.getX() + ",y="
+ this.p2.getY());
System.out.println("The line width is " + w);
} public static void main(String[] args) {
Point p1 = new Point(0, 0);
Point p2 = new Point(3, 4);
Line l = new Line(p1, p2);
l.move();
l.print();
}
}