⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javautil.doc6.html

📁 java语言规范
💻 HTML
字号:
<html>
<head>
<title>The Java Language Specification The Package java.util</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
 
<a href="index.html">Contents</a> | <a href="javautil.doc5.html">Prev</a> | <a href="javautil.doc7.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
 
<a name="7569"></a>
<center><h1>21.7  The Class  <code>java.util.Observable</code></h1></center>
<a name="21698"></a>
Each instance of class <code>Observable</code> maintains a set of "observers" that are notified 
whenever the <code>Observable</code> object changes in some significant way. An observer 
may be any object that implements interface <code>Observer</code> <a href="javautil.doc6.html#7590">(&#167;21.8)</a>.
<p><a name="23819"></a>
Note that this notification mechanism is has nothing to do with threads <a href="javalang.doc18.html#2658">(&#167;20.20)</a> and is completely separate from the <code>wait</code> and <code>notify</code> mechanism of class <code>Object</code> <a href="javalang.doc1.html#46442">(&#167;20.1)</a>.<p>
<pre><a name="7570"></a>public class <code><b>Observable</b></code> {
<a name="7571"></a>	public void <code><b>addObserver</b></code>(Observer o);
<a name="7572"></a>	public void <code><b>deleteObserver</b></code>(Observer o);
<a name="21752"></a>	public void <code><b>deleteObservers</b></code>();
<a name="21758"></a>	public int <code><b>countObservers</b></code>();
<a name="7573"></a>	public void <code><b>notifyObservers</b></code>();
<a name="7574"></a>	public void <code><b>notifyObservers</b></code>(Object arg);
<a name="7576"></a>	protected void <code><b>setChanged</b></code>();
<a name="7577"></a>	protected void <code><b>clearChanged</b></code>();
<a name="7578"></a>	public boolean <code><b>hasChanged</b></code>();
<a name="21702"></a>}
</pre><a name="21703"></a>
When an observable object is newly created, its set of observers is empty.<p>
<a name="21707"></a>
Two observers are considered the same if and only if the <code>equals</code> method <a href="javalang.doc1.html#14865">(&#167;20.1.3)</a> returns <code>true</code> for them.<p>
<a name="21704"></a>
<p><font size=+1><strong>21.7.1   </strong> <code>public void <code><b>addObserver</b></code>(Observer o)</code></font>
<p>
<a name="21711"></a>
The observer <code>o</code> is added to this <code>Observable</code> object's set of observers, provided 
that it is not the same as some observer already in the set.
<p><a name="7582"></a>
<p><font size=+1><strong>21.7.2   </strong> <code>public void <code><b>deleteObserver</b></code>(Observer o)</code></font>
<p>
<a name="21717"></a>
The observer <code>o</code> is removed from this <code>Observable</code> object's set of observers.
<p><a name="21746"></a>
<p><font size=+1><strong>21.7.3   </strong> <code>public void <code><b>deleteObservers</b></code>()</code></font>
<p>
<a name="21788"></a>
All observers are removed from this <code>Observable</code> object's set of observers.
<p><a name="21784"></a>
<p><font size=+1><strong>21.7.4   </strong> <code>public int <code><b>countObservers</b></code>()</code></font>
<p>
<a name="21794"></a>
The number of observers in this <code>Observable</code> object's set of observers is returned.
<p><a name="7583"></a>
<p><font size=+1><strong>21.7.5   </strong> <code>public void <code><b>notifyObservers</b></code>()</code></font>
<p>
<a name="21725"></a>
If this <code>Observable</code> object has been marked as changed, this method causes all 
observers to be notified with <code>null</code> as the second argument; in other words, this 
method is equivalent to:
<p><pre><a name="21726"></a>notifyObservers(null)
</pre><a name="7584"></a>
<p><font size=+1><strong>21.7.6   </strong> <code>public void <code><b>notifyObservers</b></code>(Object arg)</code></font>
<p>
<a name="21731"></a>
If this <code>Observable</code> object has been marked as changed <a href="javautil.doc6.html#7588">(&#167;21.7.9)</a>, this method 
causes all observers to be notified with <code>arg</code> as the second argument. An observer 
is notified by calling its <code>update</code> method <a href="javautil.doc6.html#7594">(&#167;21.8.1)</a> on two arguments: this 
<code>Observable</code> object and <code>arg</code>. The mark on this object is then cleared <a href="javautil.doc6.html#7587">(&#167;21.7.8)</a>.
<p><a name="7586"></a>
<p><font size=+1><strong>21.7.7   </strong> <code>protected void <code><b>setChanged</b></code>()</code></font>
<p>
<a name="21773"></a>
This <code>Observable</code> object is marked as having been changed; the <code>hasChanged</code> 
method will now return <code>true</code>.
<p><a name="7587"></a>
<p><font size=+1><strong>21.7.8   </strong> <code>protected void <code><b>clearChanged</b></code>()</code></font>
<p>
<a name="21775"></a>
This <code>Observable</code> object is marked as not having been changed; the <code>hasChanged</code> 
method will now return <code>false</code>.
<p><a name="7588"></a>
<p><font size=+1><strong>21.7.9   </strong> <code>public boolean <code><b>hasChanged</b></code>()</code></font>
<p>
<a name="21780"></a>
The result is true if and only if the <code>setChanged</code> method has been called for this 
<code>Observable</code> object more recently than either the <code>clearChanged</code> method or the 
<code>notifyObservers</code> method.
<p><a name="7590"></a>
<h1>21.8  The Interface  <code>java.util.Observer</code></h1>
<a name="21802"></a>
A class should implement the <code>Observer</code> interface if it is to be notified whenever 
an <code>Observable</code> object has been changed. See the <code>Observable</code> class <a href="javautil.doc6.html#7569">(&#167;21.7)</a> for a 
discussion of how <code>Observer</code> objects are notified.
<p><pre><a name="7591"></a>public interface <code><b>Observer</b></code> {
<a name="7592"></a>	public void <code><b>update</b></code>(Observable o, Object arg);
<a name="7593"></a>}
</pre><a name="7594"></a>
<p><font size=+1><strong>21.8.1   </strong> <code>public void <code><b>update</b></code>(Observable o, Object arg)</code></font>
<p>
<a name="21807"></a>
When an <code>Observable</code> object has been changed and its <code>notifyObservers</code> 
method <a href="javautil.doc6.html#7584">(&#167;21.7.6)</a> is called, every <code>Observer</code> object in its set of <code>observers</code> is notified
by invoking its <code>update</code> method, passing it two arguments: the <code>Observable</code> 
object and another argument specified by the call to the <code>notifyObservers</code> 
method.
<p>

<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javautil.doc5.html">Prev</a> | <a href="javautil.doc7.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright &#169 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -