observable_Java Observable countObservers()方法與示例

observable

可觀察的類countObservers()方法 (Observable Class countObservers() method)

  • 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 Observable
import 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 Class
class 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(): 2
Obs2: 
Obs1: 

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

observable

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379472.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379472.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379472.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

設計模式--Strategy 策略模式

所謂策略模式(Strategy Pattern),就是將策略 (算法) 封裝為一個對象,易于相互替換,如同 USB 設備一樣可即插即用;如果將策略、具體的算法和行為,編碼在某個類或客戶程序內部,將導至事后的修改和擴展不易。 …

HDU-1518 Square dfs+剪枝

該題問給定的棍子能否組成一個正方形。首先我們要判定是否總長度是4的倍數,然后再決定是否存在某條邊大于組合邊長。 搜索的過程中也是可以進行剪枝了。 首先將邊排序,我們可以假定所有的組合邊由大小遞減的邊組成,那么我們在搜索的時候就不用…

英語思維黃金法則

一、謂語單一原則 英文的句子當中,有且只有一套謂語結構。 要想使用多個謂語,有以下三種方法: 1,利用連詞將不同謂語并列起來 2,把其中的一些動詞給降級(v-ing v-ed 非謂語動詞) 3,…

java getname_Java文件類字符串getName()方法(帶示例)

java getname文件類字符串getName() (File Class String getName()) This method is available in package java.io.File.getName(). 軟件包java.io.File.getName()中提供了此方法。 This method is used to retrieve or return the filename or directory name and represente…

WF中DependencyObject和DependencyProperty的實現

WF中DependencyObject和DependencyProperty的實現 DependencyProperty的Register和RegisterAttached方法,將DependencyProperty存在IDictionary中完成注冊,確保相同name的DependencyProperty在一個ownerType類型中只能有一個。 DependencyObject的GetVal…

hdu2115: I Love This Game

hdu2115: http://acm.hdu.edu.cn/showproblem.php?pid2115題意:輸入n組名字和對應的時間(分:秒),要求按時間長度由短到長排序,并輸出對應排名,若時間一樣,則按名字字典序排序&#…

打開eclipse出現Failed to load the JNI shared library “D:\java\jdk\bin\...\jre\bin\server\jvm.dll”如何解決?

eclipse打開的時候出現Failed to load the JNI shared library “D:\java\jdk\bin…\jre\bin\server\jvm.dll”如何解決?? 如圖所示: 即代表你的jdk與eclipse的位數不一樣!!! 你可以查看一下eclipse和jd…

Java DataOutputStream writeUTF()方法及示例

DataOutputStream類的writeUTF()方法 (DataOutputStream Class writeUTF() method) writeUTF() method is available in java.io package. writeUTF()方法在java.io包中可用。 writeUTF() method is used to write the given string value to the basic data output stream wit…

2010年世界杯分組

A 南非 墨西哥 烏拉圭 法國 B 阿根廷 南非 韓國 希臘 C 英格蘭 美國 阿爾及利亞 斯洛文尼亞 D 德國 澳大利亞 塞爾維亞 加納 E 荷蘭 丹麥 日本 喀麥隆 F 意大利 巴拉圭 新西蘭 斯洛伐克 G 巴西 朝鮮 科特迪瓦 葡萄牙 H 西班牙 瑞士 洪都拉斯 智利 轉載于:https://www.cnblogs.c…

圓形墜落模擬算法設計

目標:實現一個算法,模擬在一個封閉二維區域,圓形小球朝給定方向墜落的過程,實現二維區域的緊密填充。 像下面這樣: 難點,及其簡單解決: 1.如何把粒子移動盡可能遠? 圖中的粒子i&…

Maven詳細教學

一、Maven簡介 maven:是apache下的一個開源項目,是純java開發,并且只是用來管理java項目的 依賴管理:就是對jar包的統一管理 可以節省空間 項目一鍵構建:mvn tomcat:run該代碼可以將一個完整的項目運行起來&#xff0…

Java Character.UnicodeBlock of()方法與示例

Character.UnicodeBlock類的()方法 (Character.UnicodeBlock Class of() method) of() method is available in java.lang package. of()方法在java.lang包中可用。 of() method is used to return the Unicode block containing the given parameter value or it returns null…

simpleDBM的B-link樹實現

參考的是VLDB2005的這篇論文,做個標記把。/Files/YFYkuner/Concurrency_control_and_recovery_for_balanced_B-link_trees.pdf 轉載于:https://www.cnblogs.com/YFYkuner/archive/2009/12/21/1629268.html

網站后臺中對html標簽的處理

最近做一個CMS&#xff0c;后臺中需要使用在線編輯器對新聞進行編輯&#xff0c;然后發表。我用的在線編輯器是CKEditorCKFinder。也許是我為了讓CKEditor更本地化吧&#xff0c;改了很多。后來發現在CKEditor中對文字設置字體、顏色、字號大小時文字的<span>標簽會出現N…

Java Calendar getActualMaximum()方法與示例

日歷類的getActualMaximum()方法 (Calendar Class getActualMaximum() method) getActualMaximum() method is available in java.util package. getActualMaximum()方法在java.util包中可用。 getActualMaximum() method is used to return the maximum value that the given …

軟件研發人員考核的十項基本原則(轉)

軟件研發人員考核的十項基本原則 作者: 任甲林 來源: 萬方數據 軟件研發人員的考核一直是軟件企業管理的難點筆者在長期的研發管理實踐與咨詢實踐中總結了進行軟件研發人員考核的一些基本原則。(1) 要體現公司的價值觀公司的價值觀體現了公司認可什么類型的人員&#xff1f;…

2012.7.24---C#(2)

學習過了C#的基本屬性函數后&#xff0c;接下來的學習我覺得比較重要。C#是一種面向對象的語言&#xff0c;下面復習一下面向對象中的一些名詞。 類&#xff1a;把一些系列東西&#xff0c;把他們的共同的屬性和方法抽象出來&#xff0c;給他起一個名字就是XXX類。類中定義…

匯編語言-001(BYTE、DUP、WORD 、DWORD 、QWORD 、TBYTE 、REAL )

1 : 基礎匯編語言展示 .386 .model flat,stdcall .stack 4096 ExitProcess PROTO,dwExitCode:DWORD.code main PROCmov eax,5add eax,6INVOKE ExitProcess,0 main ENDP END main2:基礎匯編語言展示增加變量的訪問 .386 .model flat,stdcall .stack 4096 ExitProcess PROTO,dw…

<各國地圖輪廓app>技術支持

如在app使用過程中遇到任何問題&#xff0c;請與開發者聯系caohechunhotmail.com

Java BigDecimal longValueExact()方法與示例

BigDecimal類longValueExact()方法 (BigDecimal Class longValueExact() method) longValueExact() method is available in java.math package. longValueExact()方法在java.math包中可用。 longValueExact() method is used to convert this BigDecimal to an exact long val…