Observable类deleteObserver()方法
- deleteObserver() 方法可在
java.util
包。 - deleteObserver() 方法用于从这个 Observable 对象的观察者集中移除给定的观察者(obs)。
- deleteObserver() 方法是一个非静态方法,它只能通过类对象访问,如果我们尝试使用类名访问方法,那么我们将得到一个错误。
- deleteObserver() 方法删除观察者时不会抛出异常。
用法:
public void deleteObserver(Observer obs);
参数:
Observer obs
– 表示要从集合中删除的观察者。
返回值:
该方法的返回类型是void
,它什么都不返回。
例:
// Java program to demonstrate the example
// of void deleteObserver(Observer obs) 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 deleteObs() {
setChanged();
// By using notifyObservers() method is
// to notify all the observers that are
// implemented
notifyObservers();
}
}
public class DeleteObservers {
// 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.deleteObs();
// By using deleteObserver() method is used
// delete the given observer if exists
observed.deleteObserver(obs2);
count_obs = observed.countObservers();
System.out.println("observed.deleteObserver(obs2):" + count_obs);
observed.deleteObs();
}
}
输出
observed.countObservers():2 Obs2: Obs1: observed.deleteObserver(obs2):1 Obs1:
相关用法
- Java Observable hasChanged()用法及代码示例
- Java Observable addObserver()用法及代码示例
- Java Observable clearChanged()用法及代码示例
- Java Observable setChanged()用法及代码示例
- Java Observable countObservers()用法及代码示例
- Java Observable notifyObservers()用法及代码示例
- Java ObjectInputStream readDouble()用法及代码示例
- Java ObjectStreamField getOffset()用法及代码示例
- Java ObjectStreamField compareTo()用法及代码示例
- Java Object hashCode()用法及代码示例
- Java ObjectInputStream readUnsignedByte()用法及代码示例
- Java ObjectInputStream readFields()用法及代码示例
- Java ObjectOutputStream writeFields()用法及代码示例
- Java ObjectInputStream readFully()用法及代码示例
- Java ObjectStreamField getName()用法及代码示例
- Java ObjectInputStream readInt()用法及代码示例
- Java ObjectStreamClass forClass()用法及代码示例
- Java ObjectInputStream read()用法及代码示例
- Java ObjectStreamField getTypeCode()用法及代码示例
- Java ObjectOutputStream writeBytes()用法及代码示例
注:本文由纯净天空筛选整理自Preeti Jain大神的英文原创作品 Java Observable deleteObserver() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。