list方法在java中的工作原理 [英] How list methods works in java

查看:129
本文介绍了list方法在java中的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List是java中的一个接口。它有一些方法。通常,接口是方法原型的规范。即接口由方法签名组成,只有接口中的方法不存在实现。

List is an interface in java. And it has some methods. Generally an interface is a specification of method prototypes. i.e. interface consist of methods signature only no implementation will be there for that methods in interface.

我怀疑如果List接口的方法在接口中没有任何实现,List接口的方法是如何工作的。

I have a doubt that how the methods of List interface work if they don't have any implementation inside the interface.

假设我有一个具有name属性,setter,getter方法的类Book。
我还有另一个类getBooks。

Suppose I have a class Book which has name property, setter,getter methods. and I have another class getBooks like this.

public class GetBooks{
List<Book> list;
public List<Book> getBooks(){
return list;
}
//setter method..
}

I我正在运行时通过其他类将书籍发送到set方法。

I am sending books into the set method at runtime through some other class.

我有另一个类UseBooks就是这样。

I have another class UseBooks like this.

public class UseBooks{
.....
....
List<Book> list = new GetBooks().getBooks();
list.add(new Book("aaa"));
}

我的问题是add方法是如何添加书籍的,即使它在接口列表中也是如此我的getBooks()返回List接口而不是Arraylist或其他一些实现类。

My question is how add method is adding books even it is in interface List because my getBooks() returning List interface not Arraylist or some other implementation class.

推荐答案

Java provides 3 concrete classes which implement the list interface
Vector
ArrayList
LinkedList

这篇关于list方法在java中的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆