this key -- think in java notes

来源:互联网 发布:3d建筑设计软件知乎 编辑:程序博客网 时间:2024/06/05 18:27
this 关键字对于将当前对象传递给其他方法很有用
class Person{public void eat(Apple apple){Apple peeled = apple.getPeeled();  //apple.getPeeled() == Peeler.peel(apple) == appleSystem.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外部的操作.
0 0
原创粉丝点击