In my coding, parent class Shape
is an abstract object and it has several child classes. My coding is as follow:
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 says "The method main cannot be declared static; static methods can only be declared in a static or top level type" at public static void main(String[] args){
, but i thought this syntax should be a commonly used format which is kinda fixed? Why at here i need to remove "static"? Sorry I'm new at java and might be blur for this.