简体   繁体   English

如何解决这个问题? class中找不到主要方法

[英]How to fix this? Main method not found in class

After executing the program, it shows an error of "Main method not found in class" and a = new ChinaBank();执行程序后,报错“Main method not found in class”和a = new ChinaBank(); is highlighted.突出显示。 I tried solutions similar to my problems but it wont work.我尝试了与我的问题类似的解决方案,但它不起作用。

public class quiz2 {
        abstract class Bank{
        abstract int getInterestRate();
        }
        
class ChinaBank extends Bank{
    int getInterestRate() {
        return 17;
        }
    }

    class LandBank extends Bank{
    int getInterestRate() {
        return 30;
        }
    }
    
    class BDO extends Bank{
        int getInterestRate() {
            return 15;
            }   
    }
class Main{    
       public static void main(String[] args) {
           Bank a;
           a = new ChinaBank();
           System.out.println("ChinaBank Rate of Interest is: "+a.getInterestRate()+ "%");
           a = new LandBank();
           System.out.println("LandBank Rate of Interest is: "+a.getInterestRate()+"%");
           a = new BDO();
           System.out.println("BDO Rate of Interest is: "+a.getInterestRate()+"%");
       }
    }
}
public class quiz2 {
static abstract class Bank{
    abstract int getInterestRate();
}

static class ChinaBank extends Bank{
    int getInterestRate() {
        return 17;
    }
}

static class LandBank extends Bank{
    int getInterestRate() {
        return 30;
    }
}

static class BDO extends Bank{
    int getInterestRate() {
        return 15;
    }
}
static class Main{
    public static void main(String[] args) {
        Bank a;
        a = new ChinaBank();
        System.out.println("ChinaBank Rate of Interest is: "+a.getInterestRate()+ "%");
        a = new LandBank();
        System.out.println("LandBank Rate of Interest is: "+a.getInterestRate()+"%");
        a = new BDO();
        System.out.println("BDO Rate of Interest is: "+a.getInterestRate()+"%");
    }
}

} You can try on it,then I explain it. } 你可以试一下,然后我解释一下。 "public static void main" is the entrance of a applicatioin,it must be static(it exist in the JVM before you excute your class) and public(your program can run everywhere),so the Class Main must be static(main method belongs to class Main not quiz2), then the other classes must also are static. “public static void main”是一个应用程序的入口,它必须是静态的(在你执行你的类之前它存在于JVM中)和public(你的程序可以到处运行),所以Z9BD8132039FEBF6EFAFE2278到 class Main not quiz2),那么其他类也必须是 static。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM