我正在尝试Java中的关联示例;编译时,它会显示错误消息“ java在CarClass中找不到main”。我仔细检查了“主要”语法,尝试了多个版本-仍然无法正常工作。感谢任何帮助!
class CarClass{
String carName;
int carId;
CarClass(String name, int id)
{
this.carName = name;
this.carId = id;
}
}
class Driver extends CarClass{
String driverName;
Driver(String name, String cname, int cid){
super(cname, cid);
this.driverName=name;
}
}
class TransportCompany{
public static void main(String args[])
{
Driver obj = new Driver("Andy", "Ford", 9988);
System.out.println(obj.driverName+" is a driver of car Id: "+obj.carId);
}
}
谢谢。
答案 0 :(得分:0)
您的文件名应与包含main方法的类名相同。
并使包含main()方法的类设为公开
ex: Demo.java
public class Demo{
public static void main(String args[]){
......
}
}
在您的情况下 TransportCompany.java
class CarClass{
String carName;
int carId;
CarClass(String name, int id)
{
this.carName = name;
this.carId = id;
}
}
class Driver extends CarClass{
String driverName;
Driver(String name, String cname, int cid){
super(cname, cid);
this.driverName=name;
}
}
public class TransportCompany{
public static void main(String args[])
{
Driver obj = new Driver("Andy", "Ford", 9988);
System.out.println(obj.driverName+" is a driver of car Id: "+obj.carId);
}
}
答案 1 :(得分:0)
如果所有类(CarClass
,Driver
和TransportCompany
)都在同一个.java文件中,则包含main方法和文件名的类应相同
在您的情况下,将关键字public
添加到TransportCompany
中-JVM知道在TransportCompany
中寻找主要方法