Files
Gists/PlayBall.java/PlayBall.java
2023-06-20 22:03:49 -04:00

38 lines
621 B
Java

class Ball extends Throwable { }
class P {
private String name;
private P target;
P(String name, P target) {
this.name = name;
this.target = target;
}
public String getName() {
return name;
}
public void setTarget(P target) {
this.target = target;
}
public void aim(Ball ball) {
try {
System.out.println(target.getName() + ", catch!");
throw ball;
} catch (Ball b) {
target.aim(ball);
}
}
}
public class PlayBall {
public static void main(String args[]) {
P parent = new P("Dad", null);
P child = new P("Son", parent);
parent.setTarget(child);
parent.aim(new Ball());
}
}