0
点赞
收藏
分享

微信扫一扫

Java调用类方法

沐之轻语 2022-05-02 阅读 217
java

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

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 条评论