observable

可观察的类notifyObservers()方法 (Observable Class notifyObservers() method)

Syntax:

句法:

    public void notifyObservers();
public void notifyObservers(Object o);

  • notifyObservers() method is available in java.util package.

    notifyObservers()方法在java.util包中可用。

  • notifyObservers() method is used to notify all of its observers in the list when the object has changed.

    当对象已更改时, notifyObservers()方法用于通知列表中的所有观察者。

  • notifyObservers(Object o) method is used to notify all of its observers in the list when this object has changed and later will invoke the clearChanged() to denotes that this object has no longer required to change.

    notifyObservers(Object o)方法用于在此对象发生更改时通知其列表中的所有观察者,稍后将调用clearChanged()表示该对象不再需要更改。

  • These methods don't throw an exception at the time of notifying an observer.

    在通知观察者时,这些方法不会引发异常。

  • These are non-static methods and it is accessible with the class object and if we try to access these methods with the class name then also we will get an error.

    这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则也会收到错误消息。

Parameter(s):

参数:

  • In the first case, notifyObservers() – it does not accept any parameter.

    在第一种情况下, notifyObservers() –它不接受任何参数。

  • In the first case, notifyObservers(Object o) – Object o – represents an object of any type.

    在第一种情况下, notifyObservers(Object o) – Object o –表示任何类型的对象。

Return value:

返回值:

In both the cases, the return type of the method is void, it returns nothing.

在这两种情况下,方法的返回类型都是void ,它什么也不返回。

Example 1:

范例1:

// Java program to demonstrate the example
// of notifyObservers() method of Observable
import java.util.*;
// Implement Observers class
class Observers_1 implements Observer {public void update(Observable obj, Object ob) {System.out.println("Obs1 is notified");
}
}
// Implement Observed Class
class Observed extends Observable {// Function call with setChanged()
void notifyObserver() {setChanged();
// By using notifyObservers() method is
// to notify all the observers that are
// implemented
notifyObservers();
}
}
public class NotifyObserver {// Implement Main Method
public static void main(String args[]) {Observed observed = new Observed();
Observers_1 obs1 = new Observers_1();
observed.addObserver(obs1);
observed.notifyObserver();
}
}

Output

输出量

Obs1 is notified

Example 2:

范例2:

import java.util.*;
class Observers_2 implements Observer {public void update(Observable obj, Object ob) {System.out.println("Obs2 is notified: " + ((Float) ob).floatValue());
}
}
// Implement Observed Class
class Observed extends Observable {// Function call with setChanged()
void notifyObserver1() {setChanged();
// By using notifyObservers() method is
// to notify all the observers that are
// implemented
notifyObservers();
}
void notifyObserver2() {setChanged();
// By using notifyObservers() method is
// to notify all the observers that are
// implemented with the given object
notifyObservers(new Float(10.0));
}
}
public class NotifyObserver {// Implement Main Method
public static void main(String args[]) {Observed observed = new Observed();
Observers_2 obs2 = new Observers_2();
observed.addObserver(obs2);
observed.notifyObserver2();
}
}

Output

输出量

Obs2 is notified: 10.0

翻译自: https://www.includehelp.com/java/observable-notifyobservers-method-with-example.aspx

observable

observable_Java Observable notifyObservers()方法与示例相关推荐

  1. observable_Java Observable countObservers()方法与示例

    observable 可观察的类countObservers()方法 (Observable Class countObservers() method) countObservers() metho ...

  2. .net连接mysql数据_.net连接MYSQL数据库的方法及示例!

    连接MYSQL数据库的方法及示例 方法一: 使用MYSQL推出的MySQL Connector/Net is an ADO.NET driver for MySQL 该组件为MYSQL为ADO.NET ...

  3. set.difference() 的用法(python3)_Python 集合 difference_update() 使用方法及示例

    Python 集合 difference_update() 使用方法及示例 Difference_update()使用集合的差集更新集合,并调用difference_update()方法. 如果A和B ...

  4. doc python 颜色_Python wordcloud.ImageColorGenerator方法代码示例

    本文整理汇总了Python中wordcloud.ImageColorGenerator方法的典型用法代码示例.如果您正苦于以下问题:Python wordcloud.ImageColorGenerat ...

  5. isdigit函数在C语言什么意思,C 库函数 isdigit() 使用方法及示例

    C 库函数 isdigit() 使用方法及示例 isdigit()函数检查字符是否为数字字符(0-9). isdigit()的函数原型int isdigit( int arg ); 函数isdigit ...

  6. python中append的用法_Python 列表 append() 使用方法及示例

    Python 列表 append() 使用方法及示例 append()方法将一个项目添加到列表的末尾. append()方法将单个项目添加到列表的末尾. append()方法的语法为:list.app ...

  7. java kryo_Kryo框架使用方法代码示例

    Kryo框架的source已移至https://github.com/EsotericSoftware/kryo ,进入此页面,然后点击右边的Download Zip按钮,就能下载到最新版本的Kryo ...

  8. python文件的用法,Python 文件 readlines() 使用方法及示例

    Python 文件 readlines() 使用方法及示例 概述 readlines() 方法用于读取所有行(直到结束符 EOF)并返回列表,该列表可以由 Python 的 for... in ... ...

  9. php一些单选、复选框的默认选择方法(示例)

    转载 http://www.php.cn/php-weizijiaocheng-360029.html 一. radio和checkbox及php select默认选择的实现代码 1.radio单选框 ...

最新文章

  1. 奇异值分解SVD和偏最小二乘奇异值分解PLSSVD
  2. python继承 子类init_Python类继承:使用自己的和父的默认值和init方法进行动态子类初始化...
  3. 如何关闭win10自动更新_如何关闭win10系统自动更新
  4. 面向对象——构造方法(重载)
  5. Linux运行级详解
  6. open-falcon_NASA在Falcon 9上带回了蠕虫-其背后的故事是什么?
  7. cd mysql 权限不够_.bash_profile权限不够_cdmysql权限不够
  8. [转帖]公司内网机器病毒
  9. c语言数据页,c语言基础--数据类型(51页)-原创力文档
  10. MySQL 主主配置
  11. 向架构师进军---系统架构设计基础知识
  12. mysql存储过程开启事务_mysql的存储过程会自动开启一个事务吗?
  13. VeraCrypt文件硬盘加密使用教程
  14. PadOS迅雷testflight下载教程
  15. 计算机的睡眠和休眠设置密码,怎么设置笔记本电脑睡眠唤醒密码
  16. python无法打开_终端里为什么无法运行python?
  17. bzoj 5185 Lifeguards - 动态规划 - 贪心
  18. aho-corasick php,更快的Aho-Corasick PHP实现
  19. 明光市机器人_明光市安保巡逻机器人在线咨询
  20. Educoder头歌单总线CPU设计(定长指令周期3级时序)(HUST)谭志虎 华中科技大学计算机组成原理实验计算机硬件系统设计

热门文章

  1. php中的缓,php中的缓存机制解释
  2. oracle定义变量sql赋值_ORACLE获取SQL绑定变量值的方法总结
  3. ubuntu学习摘要-ubuntu root用户
  4. 布局 —— 左侧固定,右侧自适应
  5. 搞懂 Java HashMap 源码
  6. React 入门学习笔记2
  7. ThreadLocal线程范围内的共享变量
  8. 从业回忆,一次大胆的冒险,程序员转岗项目经理
  9. erwin 不能输入中文_国产开源建模软件PDMan与国外商业建模软件ERwin的主要功能比较...
  10. linux pcre静态编译,Linux下,Nginx部署静态网站