Vector的Enumeration方法返回一个实现了Enumeration接口的匿名类 Vector部分源代码如下 public Enumeration elements() { return new Enumeration() { int count = 0; public boolean hasMoreElements() { return count < elementCount; } public Object nextElement() { synchronized (Vector.this) { if (count < elementCount) { return elementData[count++]; } } throw new NoSuchElementException("Vector Enumeration"); } }; public Enumeration elements() { return new Enumeration() { int count = 0; public boolean hasMoreElements() { return count < elementCount; } public Object nextElement() { synchronized (Vector.this) { if (count < elementCount) { return elementData[count++]; } } throw new NoSuchElementException("Vector Enumeration"); } }; public Enumeration elements() { return new Enumeration() { int count = 0; public boolean hasMoreElements() { return count < elementCount; } public Object nextElement() { synchronized (Vector.this) { if (count < elementCount) { return elementData[count++]; } } throw new NoSuchElementException("Vector Enumeration"); } };Interface是Java把世界的实现和描述分离的有力工具!
public Enumeration elements() { return new Enumeration() }Enumeration()是构造函数?Enumeraton是一个接口,有构造函数吗?可以实例化吗?在哪定义?
public Enumeration elements() { return new Enumeration() { ........... }return new Enumeration()这里的Enumeration()不是构造函数,它就是一个接口(一个新的对象),是elements()的返回值,在后面实现了该接口,所以接口的函数就可以用了。
>public Enumeration elements() { > return new Enumeration() >} > >Enumeration()是构造函数?Enumeraton是一个接口,有构造函数吗?可以实 >例化吗?在哪定义 不好意思,我忘记告诉你那是个匿名类了 new Enumeration() {//do something int .... public void find().... ....... } 这相当于 class XXX implements Enumeration { //........ } new XXX 也可以用extends class的 IE一变慢,我不小心连贴了三次
Vector部分源代码如下 public Enumeration elements() {
return new Enumeration() {
int count = 0; public boolean hasMoreElements() {
return count < elementCount;
} public Object nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return elementData[count++];
}
}
throw new NoSuchElementException("Vector Enumeration");
}
}; public Enumeration elements() {
return new Enumeration() {
int count = 0; public boolean hasMoreElements() {
return count < elementCount;
} public Object nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return elementData[count++];
}
}
throw new NoSuchElementException("Vector Enumeration");
}
}; public Enumeration elements() {
return new Enumeration() {
int count = 0; public boolean hasMoreElements() {
return count < elementCount;
} public Object nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return elementData[count++];
}
}
throw new NoSuchElementException("Vector Enumeration");
}
};Interface是Java把世界的实现和描述分离的有力工具!
return new Enumeration()
}Enumeration()是构造函数?Enumeraton是一个接口,有构造函数吗?可以实例化吗?在哪定义?
return new Enumeration()
{
...........
}return new Enumeration()这里的Enumeration()不是构造函数,它就是一个接口(一个新的对象),是elements()的返回值,在后面实现了该接口,所以接口的函数就可以用了。
> return new Enumeration()
>}
>
>Enumeration()是构造函数?Enumeraton是一个接口,有构造函数吗?可以实
>例化吗?在哪定义
不好意思,我忘记告诉你那是个匿名类了
new Enumeration()
{//do something
int ....
public void find()....
.......
}
这相当于
class XXX implements Enumeration
{
//........
}
new XXX
也可以用extends class的
IE一变慢,我不小心连贴了三次