`

j2se----Observer

    博客分类:
  • J2SE
阅读更多
class Rouse extends Observable { //房子可以被观察
   private float price;
   public House(float price){
     this.price = price;
   }
   public float getPrice(){
      return this.price;
   }
   public float setPrice(float price){
      super.setChanged();         //设置变化点
      super.notifyObservers(price); //价格别改变了
      this.price = price;
   }
}


class HousePriceObserver implements Observer{
    private String name;
    public HousePriceObserver(String name){
       this.name = name;
    }
    
    public void update(Observable o,Object arg){
       //收到更新消息
    }
}

public static void main(String args){
    House b = new House(1000);
    HousePriceObserver h1 = new HousePriceObserver("a");
    HousePriceObserver h2 = new HousePriceObserver("b");
    HousePriceObserver h3 = new HousePriceObserver("c");
    b.addObserver(h1);
    b.addObserver(h2);
    b.addObserver(h3);
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics