renReturn在Java中类型为同一个类?

时间:2016-03-20 04:04:09

标签: java inheritance polymorphism

我有这个班级

public class Child extends Parent
{
    private Child name;
    public Child addToParent(int num)
    {
        name = new Child();
        name+num;
        return name;
    }
}

我正在尝试让函数返回一个Child对象。不幸的是,我不能选择不同的返回类型,因为我被指派这样做。请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

    This is the best I can derive from your code.I am not sure what your trying to do. But this Will avoid your error.
    public class Child extends Parent {
    public String name = "";
    public Child addToParent(int num) {
   Child child = new Child(); 
    child.name = child.name + num;
    return child; } }