想不到的坑

1.interface default 方法的精确匹配

public class Foole implements IFoole {
    public void bar(long bar) {
        System.out.println("father:" + bar);
    }

    public static void main(String[] args) {
        Foole foole = new Foole();
        foole.bar(42L);
        foole.bar(44);
        IFoole ifoo = foole;
        // 当声明为IFoole时下面的就会被劫持到接口的默认方法上去,更精准的匹配
        //ifoo.bar(42L);  报错
        ifoo.bar(43);
    }

}
interface IFoole {
    default void bar(int bar) {
        System.out.println(bar);
    }
}

单命令行下,if 必须加"{}" 的问题

参考

        if (true)
            System.out.println("Test");

        String i;
        if (true)
            i = new String("Test");

        // 报错 Declaration not allowed here
        if (true)
            String i = new String("Test");

方法体必须是一个block。
Block是由花括号包围的零或多个BlockStatement
BlockStatement可以是局部变量声明,或者类/接口声明(注:这样的类或接口被称为“局部类”(local class)“局部接口”(local interface)),或者是可选带label的语句Statement。

语句有若干中可能,其中一种是if语句;if语句的then分支必须是一个Statement。
被花括号包围起来的话它就是一个Block了,而Block是Statement的一种所以没问题
局部变量声明就不是语句

posted @ 2020-01-07 14:31  jojoworld  阅读(111)  评论(0)    收藏  举报