shape.java
来自「Java 入门书的源码」· Java 代码 · 共 58 行
JAVA
58 行
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.
import java.awt.Point;
public abstract class Shape {
Point center;
public Shape() {
center = new Point(0,0);
}
public Shape(Point p) {
center = p;
}
public abstract void draw();
public String toString(){
return "Shape with center " + center;
}
public void move(int xamount, int yamount) {
center.translate(xamount,yamount);
}
public static void main(String argv[]) {
Shape s0 = new Line(2,5,6,7);
Shape s1 = new Circle( new Point(3,4) ,5);
s0.move(3,-1);
s0.draw();
s1.move(3,-1);
s1.draw();
}
}
class Line extends Shape {
Point end;
public Line(int x1, int y1, int x2, int y2) {
super(new Point(x1,y1));
end = new Point(x2,y2);
}
public void draw() {
System.out.println(toString());
}
public String toString() {
return "Line from "+center + " to " + end;
}
public void move(int xamount, int yamount) {
super.move(xamount,yamount);
end.translate(xamount,yamount);
}
}
class Circle extends Shape {
int radius;
public Circle(Point p, int r) {
super(p);
radius = r;
}
public void draw() {
System.out.println(toString());
}
public String toString() {
return "Circle at "+center+" with radius "+radius;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?