gpt4 book ai didi

java - 初学者 : Trying to have a class with an interface extend another class

转载 作者:行者123 更新时间:2023-11-29 03:40:55 25 4
gpt4 key购买 nike

我有两个类(class),我想让我的第二个类(class)访问第一个类(class)的私有(private)数据。

通常我会:

public class GenericDictionary<T> implements GenericDictionary_interface<T> {

public class Iterator<T> extends GenericDictionary<T> {

但我有一个接口(interface),我不完全知道它们是如何工作的,有时它们似乎没有必要。

public class Iterator<T> implements Iterator_interface<T> {

如何扩展另一个类并拥有一个接口(interface)?不需要接口(interface)吗?应该是我的界面 Iterator<T>类在晚饭的界面?如果是这样……怎么办?

P.S. 请不要告诉我为什么我正在做我正在做的事情(也就是制作我自己的东西而不是使用 java.util.* 中的东西)

编辑:尽管我决定使用内部类,但我的问题的答案是扩展和实现的顺序,谢谢。

最佳答案

How can I extend another class and have an interface ?

您首先放置extends,然后放置implements:

public class Iterator<T>
extends GenericDictionary<T>
implements Iterator_interface<T>
{

例如,在 JDK 中,java.util.ArrayList 给出为

public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable

(参见 its Javadoc。)

关于java - 初学者 : Trying to have a class with an interface extend another class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13219690/

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