首页 文章

错误:在类myPackage.inheritance中找不到主要方法

提问于
浏览
0
package myPackage;

public class inheritance {
     int salary = 50000;
}

class worker extends inheritance {
    int bonus = 10000;

    public static void main(String[] args) {
        worker obj1 = new worker();
        System.out.println("employee salary is" + obj1.salary);
        System.out.println("employee bonus is" + obj1.bonus);
    }
}

嗨..我是java的新手 . 我正在尝试编写一个继承程序并得到此错误 .

错误:在myPackage.inheritance类中找不到主方法,请将main方法定义为:public static void main(String [] args)或JavaFX应用程序类必须扩展javafx.application.Application

2 回答

  • -1

    此错误可能以多种方式发生

    首先需要澄清您如何编译和运行程序

    1.确保java文件放在正确的包(文件夹)本身中

    2.确保类文件的位置已添加到类路径变量中

    最有可能的问题将通过将继承类设置为public来解决 .

    package myPackage;
    class inheritance {
    int salary = 50000;}
    public  class worker extends inheritance {
    int bonus = 10000;
    public static void main(String[] args) {
    worker obj1 = new worker();
    System.out.println("employee salary is" + obj1.salary);
    System.out.println("employee bonus is" + obj1.bonus);
    }
    }
    
  • 0

    尝试在 inheritance 类中移动main方法,如下所示:

    public class inheritance {
        int salary = 50000;
        public static void main(String[] args) {
            worker obj1 = new worker();
            System.out.println("employee salary is" + obj1.salary);
            System.out.println("employee bonus is" + obj1.bonus);
        }
    }
    
    class worker extends inheritance {
        int bonus = 10000;
    }
    

相关问题