- public class Student{
-
-
- public static void study(){
-
- System.out.println("我爱学习");
-
- }
-
- public static void main(String[] args){
-
- //因为是static修饰的静态方法,不用把Student真的new出来
- //直接用类名Student点.方法名study()就可以直接调用
- Student.study();
- }
-
- }
this翻译过来这个,就是这个类,当前这个类,这个对象,当前的这个对象,就是你现在正在使用的这个类,也算是一种简写的方法。比如你想调用当前这个方法。
可以看下面的代码在没有static修饰的eat()方法里,用this代表当前对象Student来调用study()方法,但是main方法用了static修饰,this就不能不用了

因为上面出了问题,看下面这个,因为this的不可用,只能把StringTest对象创建出来,然后再去调用我们的eat方法,然后eat方法里用this调用了study方法,输出的时候就是先:学习学习然后在吃吃吃
- public class StringTest {
-
- public static void main(String[] args) {
- StringTest s = new StringTest();
- s.eat();
- //this.eat();
- }
- public void study(){
- System.out.println("学习学习");
- }
- public void eat(){
- this.study();
- System.out.println("吃吃吃");
- }
- }