java编译遇到的错误: 无法从静态上下文中引用非静态 变量 this

记住这句话:静态方法不能引用非静态变量;

我遇到的是因为将Student 放到了Test类当中去了。
解决的办法:
1.Student类写到Test外边去;
2.Student定义为静态类;

package cn.sxt.oo1;

public class Test {
    public static void main(String[] args){
        Student student = new Student("lala1",175,"java");
        student.rest();
        student.study();
    }
class Person{
    String name;
    int height;
    public void rest(){
     System.out.println("休息一会儿他");
    }
   }
   class Student extends Person{
    String major;
    public void study(){
        System.out.println("有点点想你哦~");
    }
    public Student(String name,int height,String major){
        //天然拥有父类的属性
        this.name = name;
        this.height = height;
        this.major = major;
    }
   }
   }

你可能感兴趣的:(java编译遇到的错误: 无法从静态上下文中引用非静态 变量 this)