Contents

61B-9: Extends, Casting, Higher Order Functions

Implementation Inheritance: Extends

/61b-9/image.png extends

  • Because of extends, RotatingSLList inherits all members of SLList:
  • All instance and static variables. (注意public, private, protected的区别)
  • All methods.
  • All nested classes.
  • Constructors are not inherited.

super

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class VengefulSLList<Item> extends SLList<Item> {
private SLList<Item> deletedItems;
	public VengefulSLList() {
       deletedItems = new SLList<Item>();
	}
  
	@Override
	public Item removeLast() {
    		Item oldBack = super.removeLast();
    		deletedItems.addLast(oldBack);
    		return oldBack;
	}
 
	public void printLostItems() {
    		deletedItems.print();
	}
}

注意没有super.super的情况

constructor

/61b-9/image-1.png /61b-9/image-2.png

Object class

/61b-9/image-3.png

Encapsulation

Module

Module: A set of methods that work together as a whole to perform some task or set of related tasks.

Implementation Inheritance Breaks Encapsulation

注意private 还有 反复自我调用↓ /61b-9/image-4.png

Type Checking and Casting

/61b-9/image-5.png /61b-9/image-6.png 子类赋值给基类可以,反之不行 /61b-9/image-7.png

Dynamic Method Selection and Casting Puzzle

/61b-9/image-8.png /61b-9/image-9.png

Higher Order Functions (A First Look)

执行类似f(f(x))的操作 Java7及之前不能使用函数指针,考虑实体化一个函数对象 /61b-9/image-10.png

/61b-9/image-11.png

Java8及其以后: /61b-9/image-12.png 一张图总结继承 /61b-9/image-13.png