一段魔性的java代码

版权声明: https://blog.csdn.net/xichengqc/article/details/88538523

坐标,某大型互联网公司,有个小伙伴发来一段代码,因源码涉及公司,处理之后如下:

for (flag = true; father instanceof Son; father = ((Son) father).mfather) {}

苦思不得其解,于是写了个demo,测试结果之后分析如下:

package com.xicheng.validator;

/**
 * @author xichengxml
 * @date 2019/3/7 16:45
 */
public class Main {

    public static void main(String[] args) {
        Son son = new Son();
        son.mfather = new Son();
        magicCode(son, false);
        son.mfather = new Father();
        magicCode(son, false);
    }

    /**
     * 对这段魔性代码做个解释,进行了两个赋值操作,这个需要了解for循环的执行机制:
     * 第一个语句是赋值语句;第二个语句是判断语句,如果为true则一直执行,直到为false退出;
     * 第三个语句是赋值语句
     *
     * 所以上述的解释为:首先对flag赋值,然后判断father的类型,一直循环到不是son类型,把值
     * 赋给father
     * @param father
     * @param flag
     */
    private static void magicCode(Father father, boolean flag) {
        int count = 0;
        for (flag = true; father instanceof Son; father = ((Son) father).mfather) {
            count++;
        }
        System.out.println("loop times:" + count);
        System.out.println(flag);
        if (father == null) {
            System.out.println("null");
        } else {
            System.out.println(father.getClass());
        }
    }

    static class Father {

    }

    static class Son extends Father {
        Father mfather;
    }
}

猜你喜欢

转载自blog.csdn.net/xichengqc/article/details/88538523