本人现在在看thinking in java,里面有个关于this作用的程序,不是很明白,望指教。
程序如下:
class Person{
          public void eat(Apple apple){
                    Apple peeled = apple.getPeeled();
                    System.out.println("Yummy");
          }
}class Peeler{
          static Apple peel(Apple apple){
                    return apple;
          }
}class Apple{
          Apple getPeeled(){
                    return Peeler.peel(this);  //this指当前对象
          }
}public class PassingThis {
          public static void main(String[] args){
                    new Person().eat(new Apple());
          }
}
不太明白this的作用.

解决方案 »

  1.   

    class Peeler{
      static Apple peel(Apple apple){
      return apple;
      }
    }里面的peel方法的参数是Apple对象,return Peeler.peel(this); 此处的this正是指向Apple对象,这有什么不明白的
      

  2.   

    class Apple{
      Apple getPeeled(){
      return Peeler.peel(this); //this指当前对象
      }
    }
    这个还真找不出比你注释更好的解释了。
      

  3.   

    应当是return Peeler.peel(this); //this指当前对象的首地址
      

  4.   

    this指的是当前对象,
    静态方法里面不能this但是普通方法里面可以this.静态
      

  5.   

    package CSDN;class Parent{
    String name;
    int age;
    Parent(String n,int a){
    this.name = n;
    this.age = a;
    }
    public static void run(){
    System.out.println(" run ");
    }}class Child extends Parent{
    String sex;
    Child(String n,int a,String s){
    super(n,a);//相当于执行 Parent(n,a)的{this.name = n;this.age = a;}
    this.sex = s;//相当于Child对象.sex = s;
    }
    public static void main(String args[]){
    Child c = new Child("黎明",23,"男");
    run();
    Parent.run();
    }
    }
      

  6.   

    类就是对于一类东西的抽象,这类东西总有些个实实在在的特性可以被抽象出来,写成类的代码;当一个类被实例化的时候,就变成了对象,程序中楼主的this实在Apple这个类里面,相当于Apple这个类再说:“对于有我的特性的东西(其实就是一个实实在在的苹果)去皮!!!”那个苹果就是实例化后的类对象,就是this
      

  7.   

    this就是传给方法的第一个参数   字节码中aload 0就是加载this