本文共 2278 字,大约阅读时间需要 7 分钟。
observable
countObservers() method is available in java.util package.
countObservers()方法在java.util包中可用。
countObservers() method is used to count the number of observers exists in this Observable.
countObservers()方法用于计算此Observable中存在的观察者的数量。
countObservers() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
countObservers()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。
countObservers() method does not throw an exception at the time of counting the observer.
在计数观察者时, countObservers()方法不会引发异常。
Syntax:
句法:
public int countObservers();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of the method is int, it gets the number of observers in this Observable.
方法的返回类型为int ,它获取此Observable中的观察者数量。
Example:
例:
// Java program to demonstrate the example // of int countObservers() method of Observableimport java.util.*;// Implement Observers class class Observers_1 implements Observer { public void update(Observable obj, Object ob) { System.out.println("Obs1: "); }}class Observers_2 implements Observer { public void update(Observable obj, Object ob) { System.out.println("Obs2: "); }}// Implement Observed Classclass Observed extends Observable { // Function call void countObs() { setChanged(); // By using notifyObservers() method is // to notify all the observers that are // implemented notifyObservers(); }}public class CountObservers { // Implement Main Method public static void main(String args[]) { Observed observed = new Observed(); Observers_1 obs1 = new Observers_1(); Observers_2 obs2 = new Observers_2(); observed.addObserver(obs1); observed.addObserver(obs2); // By using countObservers() method is // to return the number of observers // in this object int count_obs = observed.countObservers(); System.out.println("observed.countObservers(): " + count_obs); observed.countObs(); }}
Output
输出量
observed.countObservers(): 2Obs2: Obs1:
翻译自:
observable
转载地址:http://ysxzd.baihongyu.com/