您的位置:首页 > 编程语言 > Java开发

The basic of Java -- Abstract, Interface, Polymorphism, Inheritance, Wrap, Lambda(day 05)

2017-12-09 21:05 381 查看
1. Abstract class can realize methods, but interface only can define abstract and constant(before version 1.8), and then the following versions allow interface create static method.

2. Superclass can call the overried method of subclass, but not the overloading method.

3. From version 1.8, interface are able to have default method in order to let subclass extend and override. Additionally, if we tend to call the method of superclass, we should use
superclass.super.default method()

4. Polymorphism: 

①upwards modeling: the class's superclass or implemented interface.

②dynamic binding: binding according with the object in runtime.

5.There need to cast when  the big type converted in to small type, but if there is no connection between two types, we cannot convert them. Besides, throwing exception is common when we cast, in this way, using
instanceof is a effective way.

6. Inner class: mostly, inner classes are private, which are always serve for outer class, so if we want to call the inner class, we can new inner class in the constructor of outer class.

7. Anonymous inner class: sometimes, we only call a class's method one time, and when we create its object, we will not use it again. So in order to reduce
unnecessary trouble. we use anonymous.

Animal a = new Animal(){
@override
public void run(){
//sentence
}
};
8. Lambda: replace anonymous inner class: () - > {}

parentheses:
add the formal parameters of method that wait to realize.

brace:
method body of method.

Animal a = (a,b){return a+b;};


9. Lambda only are used in FunctionalInterface(the interface only have one abstract method beside Object)

example of Lambda: 

Integer[] a = {1,3,2,5,4,6,7,9,8,0};
Arrays.sort(a);
Arrays.toString(a);
Arrays.sort(a, (o1,o2) -> {return o2 - o1;});
System.out.println(Arrays.toString(a));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 对象 class lambda 多态