【发布时间】:2014-11-09 01:16:47
【问题描述】:
在我的编码中,父类Shape 是一个抽象对象,它有几个子类。我的编码如下:
import java.util.Random;
abstract class Shape {
protected Color color;
protected Point point;
public Shape(Color color, Point point) {
this.color = color;
this.point = point;
}
public abstract String Type();
}
class Rectangle extends Shape {
public Rectangle(Color color, Point point) {
super(color, point);
}
public String Type() {
return "Rectangle";
}
class Triangle extends Shape {
public Triangle(Color color, Point point) {
super(color, point);
}
public String Type() {
return "Triangle";
}
}
class Eclipse extends Shape {
public Eclipse(Color color, Point point) {
super(color, point);
}
public String Type() {
return "Eclipse";
}
}
public class ShapeTest {
public static void main(String[] args) {
Color color = new Color(50, 100, 150);
Point point = new Point(50, 50);
Shape[] theShape = {
new Rectangle(color, point),
new Triangle(color, point),
new Eclipse(color, point)
};
Shape shapechoice;
Random select = new Random();
for (int i = 0; i < 10; i++) {
shapechoice = theShape[select.nextInt(theShape.length)];
System.out.println("The " + (i + 1) + "type you chose is: " + shapechoice.Type());;
}
}
}
Eclipse 在public static void main(String[] args){ 处说“方法 main 不能声明为静态;静态方法只能以静态或顶级类型声明”,但我认为这种语法应该是一种固定的常用格式?为什么在这里我需要删除“静态”?抱歉,我是 java 新手,可能对此感到模糊。
【问题讨论】:
-
将你的 main 移到外部类或重新调整你的代码。
-
你不能在内部类中拥有 main 方法。将其移至外部类。