【问题标题】:When I return the call super.method(), my debugger shows a value and the method returns null当我返回调用 super.method() 时,我的调试器显示一个值并且该方法返回 null
【发布时间】:2016-04-25 17:50:31
【问题描述】:

当我调用下面的方法时,它返回 null。

public String determineAction(String someAction){
    if(someAction.length() > 4){
        return someAction;
    }else{
        return super.determineAction(someAction)
    }
 }

当我将return语句分解为String返回值时

 String action = super.determineAction(someAction);
 return action;

返回值。

愿意分享一些机制吗?我不记得在其他对象或原始数据类型中看到过这种行为。

下面是超级方法

protected String determineAction(String someAction) {
        if(someAction!= null) {
            switch(someAction) {
                case ACCEPT: return ACTION_ACCEPT;
                case CANCEL: return ACTION_CANCEL;
                case UPDATE: return ACTION_UPDATE;
                case PASS: return ACTION_PASS;
                default: return null;
            }
        }
        return null;
    }

【问题讨论】:

  • 您可以发布您要覆盖的方法的来源吗?
  • 能贴出parentesco方法的代码吗?
  • 请发布您正在扩展的超类
  • someAction.length 如果是字符串则不是有效的 Java。
  • 谢谢,我没有输入编辑器。我只是应用了正确的语法。

标签: java string null return


【解决方案1】:

请参考以下代码sn-ps。

父类

public class Father {
    protected String determineAction(String someAction) {
        if(someAction!= null) {
            switch(someAction) {
                case "abc": return someAction+someAction+someAction;
                case "ab": return someAction+someAction;
                default: return null;
            }
        }
        return null;
    }
}

儿童班

public class Son extends Father {

    public String determineAction(String someAction){
        if(someAction.length() > 4)
            return someAction;
        return super.determineAction(someAction);
     }

    public static void main(String[] args) {
        Son mySon = new Son();
        System.out.println(mySon.determineAction("abc"));   // prints abcabcabc // father determineAction()
        System.out.println(mySon.determineAction("a1234")); // prints a1234 // son determineAction()
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多