当前位置: 代码迷 >> J2SE >> this代表什么,该怎么解决
  详细解决方案

this代表什么,该怎么解决

热度:23   发布时间:2016-04-23 22:38:39.0
this代表什么
this代表什么
class Person {
  public void eat(Apple apple) {
    Apple peeled = apple.getPeeled();
    System.out.println("Yummy");
  }
}

class Peeler {
  static Apple peel(Apple apple) {
    // ... remove peel
    return apple; // Peeled
  }
}

class Apple {
  Apple getPeeled() { return Peeler.peel(this); }
}

public class PassingThis {
  public static void main(String[] args) {
    new Person().eat(new Apple());
  }
}

------解决方案--------------------
class Apple {
  Apple getPeeled() { return Peeler.peel(this); }
}

this 是指Apple对象本身

Apple  a = new Apple();
这个时候如何执行下面的语句
a.getPeeled();
效果是这样的
return Peeler.peel(a);
  相关解决方案