List list = new ArrayList 和ArrayList list = new ArrayList 之间的区别是什么? [重复]

时间:2022-07-13 02:43:30

This question already has an answer here:

这个问题在这里已有答案:

Usually declaration type is interface type and initialization part has implementation type. what is differnce between List<String> list = new ArrayList<String> and ArrayList<String> list = new ArrayList<String>? What is difference beside polymorphism?

通常声明类型是接口类型,初始化部分具有实现类型。 List list = new ArrayList 和ArrayList list = new ArrayList 之间的区别是什么?多态性有什么区别?

2 个解决方案

#1


1  

There are no important differences. But if you use something like this

没有重要的区别。但如果你使用这样的东西

void doSomething(List list)
{}

you can use any object which extends the List class, but if you use something like this void doSomething(ArrayList list) {} you can use only ArrayList object and its subclasses.

你可以使用任何扩展List类的对象,但是如果你使用类似这样的东西,那么你可以只使用ArrayList对象及其子类。这可以使用void doSomething(ArrayList list){}。

#2


0  

List is an interface and ArrayList is its implimentation class. We can't create object of interface because they are abstract but we can create reference of interface which is nothing but List list . Using this reference we can invoke methods of ArrayList.

List是一个接口,ArrayList是它的implimentation类。我们不能创建接口的对象因为它们是抽象的但我们可以创建接口的引用,它只是List列表。使用此引用,我们可以调用ArrayList的方法。

#1


1  

There are no important differences. But if you use something like this

没有重要的区别。但如果你使用这样的东西

void doSomething(List list)
{}

you can use any object which extends the List class, but if you use something like this void doSomething(ArrayList list) {} you can use only ArrayList object and its subclasses.

你可以使用任何扩展List类的对象,但是如果你使用类似这样的东西,那么你可以只使用ArrayList对象及其子类。这可以使用void doSomething(ArrayList list){}。

#2


0  

List is an interface and ArrayList is its implimentation class. We can't create object of interface because they are abstract but we can create reference of interface which is nothing but List list . Using this reference we can invoke methods of ArrayList.

List是一个接口,ArrayList是它的implimentation类。我们不能创建接口的对象因为它们是抽象的但我们可以创建接口的引用,它只是List列表。使用此引用,我们可以调用ArrayList的方法。