32 lines
628 B
Java
32 lines
628 B
Java
// housekeeping/PassingThis.java
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://OnJava8.com for more book information.
|
|
|
|
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());
|
|
}
|
|
}
|
|
/* Output:
|
|
Yummy
|
|
*/
|