Java中一个方法在另一个方法中调用

同一个类中:
在main方法中,静态方法可以被正常调用;
非静态方法需要通过实例化对象才能调用;

public class Test {

    public static void one(){
        System.out.println("111");
    }
    public void two(){
        System.out.println("222");
    }
    public static void main(String[] args){
        one();
        Test test = new Test();
        test.two();
    }
}

不同类中:
在main方法中调用静态方法,需要通过类名调用;
非静态方法通过实例化对象调用;

public class Test {

    public static void one(){
        System.out.println("111");
    }
    public void two(){
        System.out.println("222");
    }
}

class Test2{
    public static void main(String[] args){
        Test.one();
        Test test = new Test();
        test.two();
    }

}
上一篇:SQL66 牛客每个人最近的登录日期(一)


下一篇:机器学习分类算法之支持向量机