public class WeatherData extends Observable{
private float temperature; //温度
private float humidity; //湿度
private float pressure; //压强
public WeatherData(){}
/**
*温度变化时调用
*/
public void measurementsChanged(){
setChanged();
notifyObservers();
}
public void setWeatherChanged(float temperature,float humidity,float pressure){
this.pressure=pressure;
this.temperature=temperature;
this.humidity=humidity;
measurementsChanged();
}
public float getTemperature() {
return temperature;
}
public float getHumidity() {
return humidity;
}
public float getPressure() {
return pressure;
}
}
public class CurrentConditionDisplay implements Observer {
@Override
public void update(Observable o, Object arg) {
if (o instanceof WeatherData){
WeatherData weatherData= (WeatherData) o;
weatherData.getHumidity();
System.out.println(“收到变化通知”);
}
}
}