16、interface中的成员变量默认为public static final类型,方法只能是public(默认为public)
17、内部类访问外部类成员:
Outer.this.num;
18、一道有趣的题目
使用内部类实现该程序。(答案附在本文末尾)
interface Inter { void show(); } class Outer { /* */ } public class Test { public static void main(String[] args) { Outer.method().show(); } }
19、实例化内部类的方式
Outer.Inner inner=new Outer().new Inner();
静态内部类
Outer.Inner inner=new Outer.Inner();
20、泛型的使用
类中使用
class Test{}
方法中使用
publicextends T> void testDemo(T t,S s){}
这样定义时,要求传入的S必须要是T的子类
这样写也无妨:
publicextends T> void testDemo(){}
只是没有卵用,因为在里面定义的T的变量不能初始化。
21、通配符
public void testDemo(List> s)
它等价于
publicvoid testDemo(List s)
通配符只在修饰变量中用到。
22、异常处理
throw扔出异常,如果本方法中catch了,就处理,没有就扔到上级方法。
finally会在返回前,把返回值压栈然后执行,执行完后弹出返回。(延伸链接:https://www.cnblogs.com/justinli/p/4067998.html)
23、float z=12.14f; //没有f会被看做是double而报错
24、System.out 是一个PrintStream(你可以通过查看源码来了解它是如何运行的)
25、java标识符:52个字母,数字,下划线,美元符$;不能数字开头,不能是关键字,不能有空格
26、逻辑表达式的返回值
false:1:'a'
如果两边同类型,则返回该类型。
如果一边是byte,short,char,另一边是int,且不溢出,则结果为byte,short,char类型。
不符合上述条件,隐式转换为高精度类型。
27、switch支持的六种类型
byte short char int enum String(java7后)
28、函数调用参数为null时,会调用参数最子类的方法,如果最底层有2个或以上的兄弟类,则会报错。
参考链接:https://www.cnblogs.com/holybell/p/6568939.html
29、协变
父类的一个方法返回另一个类的父类;
子类的这个方法返回另一个类的子类。
例:
class Flower { Plant kind() { return new Plant(); } } class Luoyangred extends Flower { Peony kind() { return new Peony(); } }
30、interface中可定义内部类,默认为public static
附:18的答案
interface Inter { void show(); } class Outer { public static Inter method(){ return new Inter(){ public void show(){ System.out.println("oo"); } }; } } public class Test { public static void main(String[] args) { Outer.method().show(); } }