//: initialization/PassingThis.javaclass 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());
}
} /*
 * Output: Yummy
 */// :~谁帮我解释一下,有些不大懂。

解决方案 »

  1.   

    //: initialization/PassingThis.java class Person {                     //Person类
    public void eat(Apple apple) {     //Person类的内部公有函数操作
    Apple peeled = apple.getPeeled();  //创建一个苹果实例并调用其静态的去皮方法
    System.out.println("Yummy");       //输出美味

    } class Peeler {                    //Peeler-削皮器类
    static Apple peel(Apple apple) {  //Peeler的静态方法
    // ... remove peel 
    return apple; // Peeled 

    } class Apple {                      //Apple类
    Apple getPeeled() {                //Apple的内部方法
    return Peeler.peel(this);          //通过Peeler对当前的Apple实例进行削皮处理并返回

    } public class PassingThis { 
    public static void main(String[] args) { 
    new Person().eat(new Apple());    //创建Person实例并吃一个削过皮的苹果

    } /* 
    * Output: Yummy 
    */// :~