您的位置:首页 > 编程语言 > Java开发

this key -- think in java notes

2014-10-16 21:38 288 查看
this 关键字对于将当前对象传递给其他方法很有用
class Person{
public void eat(Apple apple){
Apple peeled = apple.getPeeled();  //apple.getPeeled() == Peeler.peel(apple) == apple
System.out.println("Yummy");
}
}

class Peeler{                               //削皮
static Apple peel(Apple apple){
return apple;
}
}

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

public class TJ_5_41 {
public static void main(String[] args){
new Person().eat(new Apple());
}
}
apple需要调用Peeler.peel()方法,他是一个外部工具方法,将执行由于某种原因必须放在Apple外部的操作.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: