Vital Java Travel One

xiaoxiao2021-03-06  39

Polymorphism is also known as dynamic binding "Dynamic Binding", which is bound to "late binding" or run "run-time bingding". It means to determine which method of the binding according to the object when the program is running. Polymorphism is the third basic characteristic of the object-oriented programming language after data abstraction and inheritance.

Binding: Early Binding: Early Binding: When the binding occurs before the program runs (Late Binding): When the program is running, it is determined which method is determined according to the type of object.

Shape s = new circle (); // Upcasting: Put the Circle object Upcast as shape type s.draw ();

UPCAST ​​is used to use the object's Reference as a base class. (Note: What types of Java know what type of object belongs) Because Derived Class Is A Type Of Base Class, the base class Reference (Shape S) After accepting the object of the Circle, the base class REFERENCE calls are the base class's own method (Late Bind) unless this Method is Late-Bound, it is derived classification (OVERRIDE) this Method The corresponding Method (polymorphism) will be selected according to the object type. Take the above code: s is a Shape type Reference unless Draw () is a dynamically binding method (derived Circle overwritten this DRAW) ()), S.DRAW () will call the Cicle Draw (), otherwise, all the base class Shape own MethodPrivate and final Method will use Early-binding because they cannot be override. (Note: The private method is automatically finaly) It is recommended to name the derived class of Method with the name of the Private Method of the base class. Because this will make people mistaken this Method, actually private is automatically final, and cannot be override.

Abstract base class and interface in some design, you just want the base class to represent the interface that is derived. That is, you don't want to create a base class object, but just want to pass the object to it, in order to use This class interface. The compiler will prevent anyone from creating the object of Abstract. Inface Keywords are deepened to the Abstract class concept, it does not allow you to implement any ways.

Not finished.

转载请注明原文地址:https://www.9cbs.com/read-52221.html

New Post(0)