gpt4 book ai didi

java - 在接口(interface)方法中返回子类

转载 作者:行者123 更新时间:2023-11-30 02:49:49 24 4
gpt4 key购买 nike

我想制作一个链表项目,在其中创建了一个接口(interface)和一个类

类别:

  public class MainNode<T> implements Node<T>
{
public T data;
public MainNode<T> next;
public MainNode<T> nextIs()
{
return next;
}
}. //all other methods are defined and work fine

接口(interface):

public interface Node<T>
{
public SUB_CLASS_RETURN_TYPE nextIs();
} //all other methods are declared as needed

问题:问题是应该在 SUB_CLASS_RETURN_TYPE 中编写什么来返回派生/子类的对象或引用

最佳答案

你能做的就是使用泛型

public interface Node<N extends Node<N>> {
public N nextLs();
}

public class MainNode implements Node<MainNode> {
@Override
public MainNode nextls() {
...
}
}

关于java - 在接口(interface)方法中返回子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39050547/

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