Java调用类方法

阅读 223

2022-05-02

一、调用的是静态类
可以用类名直接调用,调用格式:类名.方法名(参数表)

public class StaticCall {
    public static void staticCall(){
        System.out.println("Successfully called the method in the static class");
    }
}

public class Main {
    public static void main(String[] args) {
        StaticCall.staticCall();
    }
}


成功输出

调用的是非静态类
将类实例化后调用

public class NotStaticCall {
    public void notStaticCall(){
        System.out.println("Successfully called the method in the non static class");
    }
}

public class Main {
    public static void main(String[] args) {
        /*
        调用非静态类中的方法
         */
        NotStaticCall nSC=new NotStaticCall();
        nSC.notStaticCall();
    }
————————————————

精彩评论(0)

0 0 举报