Thinking in java-8 单继承和多态

1.单继承 Singly rooted hierarchy

All objects in a singly hierarchy have an interface in common, so they are all ultimately the same fundamental type. All objects in a singly rooted hierarchy can be guaranteed to have certain functionality. You know you can perform certain basic operations on every object in your system. All objects can easily be created on the heap, and arguments passing is greatly simplified.
A singly rooted hierarchy makes it easier to implement garbage collector, which is one of the fundamental improvements of Java over C++.
所有单继承的对象有一个共同的接口,它们最终是同一种基本类型。对于单继承的对象,我们可以对于这类对象执行某些相同操作,因为它们具有相同的interface。
单继承也是简化垃圾回收机制的好方法,这是java相对于C++语言的提升。

2.多态 Polymorphism

Early binding: the function call generated by a non-OOP compiler causes what is called early binding. It means the compiler generates a call to a specific function name, and the runtime system resolves this call to the absolute address of the code to be executed.
In OOP, the program cannot determine the address of the code until run time, so some other scheme is necessary when a message is sent to a generic object.
Late binding: when you send a message to an object, the code being called isn’t determined until run time. The complier does ensure that the method exists and performs type checking on the arguments and return value, but it doesn’t know the exact code to execute.
To perform late binding, Java uses a special bit of code in lieu(处所) of absolute call. This code calculates the address of the method body, using information stored int the object. Thus, each object can behave differently according to the contents of that special bit of code. When you send a message to an object , the object does figure out what to do with that message.
We call this process of treating a derived type as though it were its base type upcasting. And yet the right thing happened because of polymorphism.

Polymorphism: 多态,是一种把子类当作基类的行为。
Early binding:非OOP语言中,编译器在编译时产生给定方法名的调用,在运行时会把该方法的调用解析成代码执行的内存地址。
Late binding: 程序在运行时才能确定要执行代码的内存地址,编译器做的工作只是检验方法存在、方法的返回值和参数类型一致,但编译器并不知道具体要执行代码的内存地址。
Up casting: 将子类当作父类来看的行为,一般称为向上类型转换。这种行为默认可行的,比如可以说:

class Shape{
}
class Circle extends Shape{
}
Circle circle = new Circle();
Shape shape = circle;   //Ok

Down casting: 将父类向下转换为子类的行为, 需要强制类型转换,可能不成功。

你可能感兴趣的:(继承,类型转换,多态,hierarchy)