静态(类)成员不能访问非静态成员的理解

整理归纳下Java的基础知识点。解释在注释内。

public class staticTest {

    public void play(){
        //方法体
    }

    public static void main(String[] args) {
        //main()为静态方法,play()为非静态方法;
        //静态方法是类本身的方法,用类.方法调用,而不是该类的实例
        //play()方法是实例方法,需要通过实例去调用.

        play();//错误的

        staticTest st = new staticTest();//正确的
        st.play();

    }
}
或者更改play()方法为static。



猜你喜欢

转载自blog.csdn.net/victoryshen/article/details/79154918