Java Reference
In-Depth Information
}
public String getName() {
return name;
}
private String name = "Dr. Jekyll";
}
Directory "Horrific"
Now we can define the class of person who's looking out for this kind of thing:
import java.util.Observer;
import java.util.Observable;
public class Person implements Observer {
// Constructor
public Person(String name, String says) {
this.name = name;
this.says = says;
}
// Called when observing an object that changes
public void update(Observable thing, Object o) {
System.out.println("It's " + ((JekyllAndHyde)thing).getName() +
"\n" + name + ": " + says);
}
private String name;
// Person's identity
private String says;
// What they say
when startled
}
Directory "Horrific"
We can gather a bunch of observers to watch Dr. Jekyll with the following class:
// Try out observers
import java.util.Observer;
public class Horrific {
public static void main(String[] args) {
Search WWH ::




Custom Search