(1) List<?> myList = new ArrayList<?>();
(2) ArrayList<?> myList = new ArrayList<?>();
Run Code Online (Sandbox Code Playgroud)
我理解,使用(1),可以交换List接口的实现.似乎(1)通常在应用程序中使用而不管需要(我自己总是使用它).
我想知道是否有人使用(2)?
此外,经常(并且我可以得到一个例子)情况实际上需要使用(1)over(2)(即where(2)不足以对接口和最佳实践等进行编码)
Java中的ArrayList或List声明质疑并回答了如何声明一个空,ArrayList但是如何声明一个带有值的ArrayList?
我尝试了以下但它返回语法错误:
import java.io.IOException;
import java.util.ArrayList;
public class test {
public static void main(String[] args) throws IOException {
ArrayList<String> x = new ArrayList<String>();
x = ['xyz', 'abc'];
}
}
Run Code Online (Sandbox Code Playgroud) 在我正确掌握接口最佳实践的过程中,我注意到了以下声明:
List<String> myList = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
代替
ArrayList<String> myList = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
- 要我的理解,是因为它可以让有一天你不希望实现一个ArrayList但也许另一种类型列表的情况下的灵活性.
有了这个逻辑,我建立了一个例子:
public class InterfaceTest {
public static void main(String[] args) {
PetInterface p = new Cat();
p.talk();
}
}
interface PetInterface {
public void talk();
}
class Dog implements PetInterface {
@Override
public void talk() {
System.out.println("Bark!");
}
}
class Cat implements PetInterface {
@Override
public void talk() {
System.out.println("Meow!");
}
public void batheSelf() {
System.out.println("Cat bathing");
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,我无法访问batheSelf()方法,因为它仅适用于Cat.这使我相信,我应该只从接口中声明,如果我只打算使用的接口(而不是从子类中额外的方法)声明的方法,否则我应该直接从类(在这种情况下,猫)申报.我在这个假设中是否正确?
我一直听到大多数与节目相关的网站上的声明:
编程到接口而不是实现
但是我不明白其含义?
例子会有所帮助.
编辑:我收到了很多好的答案,所以你可以用一些代码片段来补充它,以便更好地理解这个主题.谢谢!
你能告诉我这三个在用法方面的区别吗:-
Superclass p1 = new Superclass();
Superclass p2 = new Subclass();
Subclass p3 = new Subclass();
Run Code Online (Sandbox Code Playgroud) 我是 Java 的初学者。我有两个基本问题。
当我们将数组列表的声明写为
List l=new ArrayList<String>();List<String> l=new ArrayList<String>();List<String> l=new ArrayList<>();我看到以上所有陈述都没有给出任何错误。当我们选择其中一种方法来声明任何集合时,它会产生什么不同?
当我们将集合初始化为
List<String>l = new ArrayList<String>();所以如果我们像下面这样声明
ArrayList<String> l= new ArrayList<String>();1 和 2 有什么区别,为什么我们在使用集合时主要选择 2?
我对为什么我们将代码实现到接口的设计方法有疑问。这在原始数据类型中非常常见。就像我没有得到这两者之间的区别:
Map<Integer, String> mymap = new HashMap<Integer, String>();
Run Code Online (Sandbox Code Playgroud)
和
HashMap<Integer, String> mymap = new HashMap<Integer, String>();
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别吗?我的意思是,mymap在这两种情况下,我们将要使用的每个地方都将保持不变。
如果问题似乎没有用,我很抱歉,但我真的不明白这将如何在以后使用的地方产生任何影响mymap。请帮忙?
谢谢..
注意 - 我已经在 SO 上看到了这个问题,但它没有给出我想要的。
可能重复:
ArrayList声明Java
在Java中,为什么经常使用List声明ArrayList?
List<String> strList1 = new ArrayList<String>();
ArrayList<String> strList2 = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
我无法理解这两者之间的区别.这个比那个好吗?或具有特定用途?
这个公式在java中是合法的来声明ArrayList
List<Integer> list1 = new ArrayList<Integer>() {1,2,3,4,5};
Run Code Online (Sandbox Code Playgroud) 所以,我只是想知道为什么我们需要在c#上声明两次列表的类型,如下例所示:
List<Product> products = new List<Product>();
Run Code Online (Sandbox Code Playgroud)
我在Java上使用Lists(也就是ArrayLists),但是我们不必这样做.
java ×9
interface ×4
arraylist ×3
oop ×2
.net ×1
c# ×1
collections ×1
declare ×1
decoupling ×1
list ×1