在Java中可以观察到

编程入门 行业动态 更新时间:2024-10-22 20:31:51
本文介绍了在Java中可以观察到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一个我试图找出的例子:

public class IntegerDataBag extends Observable implements Iterable< Integer> { private ArrayList< Integer> list = new ArrayList< Integer>(); public void add(Integer i){ list.add(i); setChanged(); notifyObservers(); } public Iterator< Integer> iterator(){ return list.iterator(); } public Integer remove(int index){ if(index< list.size()){ Integer i = list.remove(index); setChanged(); notifyObservers(); return i; } 返回null; } } public class IntegerAdder implements Observer { private IntegerDataBag bag; public IntegerAdder(IntegerDataBag包){ this.bag = bag; bag.addObserver(this); } public void update(Observable o,Object arg){ if(o == bag){ System.out.println( IntegerDataBag已更改); } } }

  • bag.addObserver()只能由于 IntegerDataBag 扩展可观察?

  • 这个观察者在哪里添加?什么是创建和在哪里?

  • setChanged()和 notifyObservers()?

  • 我不明白更新方法; arg 代表什么?为什么我需要检查 o == bag ?为什么我会更新另一个可观察的?

  • 为什么我还需要这个观察者?

  • 解决方案

  • 是的。 addObserver 是 Observable 抽象类中的一种方法。请参阅Java文档中的可观察到的。
  • 它被添加到 Observable 中的列表中。
  • 调用 notifyObservers 将不会执行任何操作,直到 setChanged 设置为止。 / li>
  • 您可能在同一个应用程序中有多个Observables。
  • 观察者是一种常见的设计模式。通常的例子是当你有一个模型和多个视图。每个视图都是模型的观察者;如果模型更改,则视图将被更新。
  • I'm trying to understand the Observer and the Observable.

    Here's an example that I'm trying to figure out:

    public class IntegerDataBag extends Observable implements Iterable<Integer> { private ArrayList<Integer> list= new ArrayList<Integer>(); public void add(Integer i){ list.add(i); setChanged(); notifyObservers(); } public Iterator<Integer> iterator(){ return list.iterator(); } public Integer remove (int index){ if (index< list.size()){ Integer i = list.remove(index); setChanged(); notifyObservers(); return i; } return null; } } public class IntegerAdder implements Observer { private IntegerDataBag bag; public IntegerAdder(IntegerDataBag bag) { this.bag = bag; bag.addObserver(this); } public void update(Observable o, Object arg) { if (o == bag) { System.out.println("The contents of the IntegerDataBag have changed"); } } }

  • The bag.addObserver() can be made only because IntegerDataBag extends Observable?

  • Where is this observer being add to? What is being created and where?

  • What is the difference between setChanged() and notifyObservers()?

  • I don't understand the update method; what does arg stand for? Why do I need to check that o==bag? Why would I update another observable?

  • Why should I need this observer anyway?

  • 解决方案

  • Yes. addObserver is a method in the Observable abstract class. See Observable in the Java documentation.
  • It is added to a list in Observable.
  • A call to notifyObservers will do nothing until setChanged is set.
  • You might have multiple Observables in the same application.
  • Observer is a common design pattern. The usual example is when you have a Model and multiple Views. Each View is an Observer on the Model; if the Model changes, the Views get updated.
  • 更多推荐

    在Java中可以观察到

    本文发布于:2023-10-27 18:46:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1534225.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:观察到   Java

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!