识趣。 2014-07-22 13:26
浏览 351
已采纳

希望写一个类的抽象,使得实现它的类都包含返回自己的方法

譬如有这么一个类
[code="java"]
interface ITreeNode{
public ITreeNode getParent(){return parent;}
public List getSons() {return sons;}
}
[/code]

现在的问题是:
如果有个类RealTreeNode从它继承,结果一定是这样:
[code="java"]
class RealTreeNode implements ITreeNode{
List sons;
RealTreeNode parent;
public ITreeNode getParent(){return parent;}
public List getSons() {return sons;}
}
[/code]

我希望能是这样:
[code="java"]
class RealTreeNode implement ITreeNode{
List sons;
RealTreeNode parent;
public RealTreeNode getParent(){return parent;}
public List getSons() {return sons;}
}
[/code]

换句话说,我希望写这样的类的抽象,即包含两个方法getParent和getSons,它们返回[b]该类自身[/b]和[b]List<该类自身>[/b]。应该如何写?

  • 写回答

2条回答 默认 最新

  • tianchao_ 2014-07-22 14:26
    关注

    你试试这个,不知道行不

    [code="java"]
    public abstract class ITreeNode {
    List sons;
    ITreeNode parent;

    ITreeNode(List<ITreeNode> sons, ITreeNode parent) {
        this.sons = sons;
        this.parent = parent;
    }
    
    public abstract T getParent();
    
    public abstract List<T> getSons();
    

    }[/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题