gpt4 book ai didi

java - Java集合接口(interface)

转载 作者:行者123 更新时间:2023-12-01 06:36:56 25 4
gpt4 key购买 nike

import java.util.*;

public class ArrayListDemo{
public static void main(String[] args) {
ArrayList<Object> arl=new ArrayList<Object>();
Integer i1=new Integer(10);
Integer i2=new Integer(20);
Integer i3=new Integer(30);
Integer i4=new Integer(40);

arl.add(i1);
arl.add(i2);
arl.add(s1);
System.out.println("The content of arraylist is: " + arl);
System.out.println("The size of an arraylist is: " + arl.size());

这是一个简单的 arrayList 程序。ArrayList 扩展了 AbstractList 并实现了 List、Cloneable、Serialized。

这里我的问题是:是否没有必要通过 arrayList 类(ArrayListDemo)实现上述接口(interface)(List、Cloneable、Serialized)中存在的所有方法。

最佳答案

是的,除非你的类扩展了另一个已经提供这些方法的类,或者你的类是一个抽象类。例如:

interface Foo {
void bar();
void baz();
}

class Superclass {
public void bar() {}
}

class Subclass extends Superclass implements Foo {
// We need to implement baz, but bar is okay already
@Override public void baz() {}
}

abstract class AbstractClass implements Foo {
// No need to do anything... a subclass will need
// to implement both bar and baz though
}

关于java - Java集合接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4826031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com