简体   繁体   中英

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(); 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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