//Vehicle.java
import java.util.Random;
class VehicleConstructor{
private int speed;
private int timeDriven;
private int run;
private int startSpeed; //Pradinis greitis
public VehicleConstructor() {
setSpeed();
System.out.println("Vehicle");
}
private void setSpeed(){
int minSpeed = 1;
int maxSpeed = 40;
Random random = new Random();
this.startSpeed = random.nextInt(maxSpeed - minSpeed + 1) + minSpeed;
}
protected int getSpeed(){
return startSpeed;
}
}
class Bicycle extends VehicleConstructor{
public void Bicycle(){
System.out.println(getSpeed());
}
}
public class Vehicle{
public static void main(String[] args){
Bicycle bicycle = new Bicycle();
}
}
我想创建Bicycle
扩展并从父级VehicleConstructor
调用方法的对象。getSpeed()
问题是我得到一个错误:
错误:在类 Bicycle 中找不到主方法,请将主方法定义为:public static void main(String[] args)
据我所知,Java 构造函数与类同名,所以我有
public void Bicycle(){
System.out.println(getSpeed());
}
但是错误说我需要public static void main
,所以其他类也需要与static
,但我不想要这个。是否可以在不需要静态方法的情况下以某种方式创建对象?