在接口声明一个抽象方法,为什么,请大家讨论一下

weimenren 2003-02-21 12:01:09
Java的类库
接口List是这样声明的:
请问为什么要把addAll(),方法声明为抽象

public interface List extends Collection {
// Positional Access
Object get(int index);
Object set(int index, Object element); // Optional
void add(int index, Object element); // Optional
Object remove(int index); // Optional
abstract boolean addAll(int index, Collection c); // Optional

// Search
int indexOf(Object o);
int lastIndexOf(Object o);

// Iteration
ListIterator listIterator();
ListIterator listIterator(int index);

// Range-view
List subList(int from, int to);
}

...全文
231 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hayai 2003-02-21
  • 打赏
  • 举报
回复
For those Classes directly implementing List, addAll method is NOT necessory to be implemented. Those Classes could be AbstractList.
It was designed for those Classes extending AbstractList to implement.

Interface A.
Class B implements A.
Class C extends B.

abstract methods in A are idealy implemented in C, not in B. However, if B implements those methods, I think it's allowable as well anyway.
accp 2003-02-21
  • 打赏
  • 举报
回复
还是不明白,在这里加个abstract是不是有点多余,或者说有什么用?
我想接口中的任何方法都可以达到楼上所说的功能。郁闷。。
weimenren 2003-02-21
  • 打赏
  • 举报
回复
to glassprogrammer()

List 本身就是一个接口,根本就不实现任何的方法。

glassprogrammer 2003-02-21
  • 打赏
  • 举报
回复
也就是说, List本身没有实现这个方法(所以其实List根本没什么用处), 但是他强制它的子类实现这个接口, 所以你可以看看ArrayList就实现了这个接口, 也就是一个实际可以使用的List了
weimenren 2003-02-21
  • 打赏
  • 举报
回复
我这里不是讨论这个方法是不是abstract的,而是讨论为什么要这样申明

这段代码是从

http://java.sun.com/docs/books/tutorial/collections/interfaces/list.html

copy过来的

shine333 2003-02-21
  • 打赏
  • 举报
回复
not abstract,
visit http://java.sun.com/j2se/1.4.1/docs/api/java/util/List.html
or see java.util.List
煜知搬砖者 2003-02-21
  • 打赏
  • 举报
回复
楼上得对,看了一下源码,不是抽象方法。我的是1.4
weimenren 2003-02-21
  • 打赏
  • 举报
回复
我看的是sun的教程:

http://java.sun.com/docs/books/tutorial/collections/interfaces/list.html
easyinfonet 2003-02-21
  • 打赏
  • 举报
回复
不是抽象方法呀

boolean addAll(Collection c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
weimenren 2003-02-21
  • 打赏
  • 举报
回复
up
zrhk 2003-02-21
  • 打赏
  • 举报
回复
接口中的方法本来就是抽象的
weimenren 2003-02-21
  • 打赏
  • 举报
回复
这个贴子的正确答案在http://expert.csdn.net/Expert/topic/1451/1451519.xml?temp=.7631647

有兴趣的朋友可以去看一下

skylineforever 2003-02-21
  • 打赏
  • 举报
回复
接口本来就是抽象的啊,所以有些多余?

我是初学

不知道为什么

up
weimenren 2003-02-21
  • 打赏
  • 举报
回复
up

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧