1. Dynamic binding
Associating a method call with the same method body is called a binding.
Binding is based on the type of the object at run time, called late-bound or run-time binding. In addition to the static method and final in Java
For example, the following defines a variable of shape type, which is a shape reference that, due to late binding, gives an object reference to its subclass circle, which is ultimately called the Circle.draw () method.
1 Packagecom.khlin.binding.test;2 3 Public classApp {4 Public Static voidMain (string[] args) {5Shape shape =NewCircle ();6 Shape.draw ();7 }8 }9 Ten classShape { One Public voidDraw () { ASystem.out.println ("i m a shape ..."); - } - } the - classCircleextendsShape { - Public voidDraw () { -System.out.println ("I m a circle ..."); + } -}
Output: i m a circle ...
2. No Dynamic binding defects
any domain will be parsed by the compiler and therefore not polymorphic.
static methods are also not polymorphic.
Take a look at the following example:
1 Packagecom.khlin.binding.test;2 3 Public classApp {4 Public Static voidMain (string[] args) {5 //shape shape = new Circle ();6 //Shape.draw ();7 8Shape shape =NewTriangel ();9 /**direct access to the domain, resulting in 0, indicating that the domain cannot be dynamically bound*/TenSystem.out.println ("Shape Field[degreeofangels]:" One+shape.degreeofangels); A /**call method, get 180, description is dynamic bound*/ -System.out.println ("Shape [Getdegreeofangels () method]:" -+shape.getdegreeofangels ()); the /**static methods cannot be dynamically bound*/ - shape.print (); - /**static domains also cannot be dynamically bound*/ -System.out.println ("Shape Field[lines]:" +shape.lines); + } - } + A classShape { at /**the sum of internal angles*/ - Public intDegreeofangels = 0; - - /**There are several sides*/ - Public Static Final intLines = 1; - in Public intGetdegreeofangels () { - returnDegreeofangels; to } + - Public voidDraw () { theSystem.out.println ("i m a shape ..."); * } $ Panax Notoginseng Public Static voidprint () { -System.out.println ("Printing shapes ..."); the } + } A the classTriangelextendsShape { + /**the sum of internal angles*/ - Public intDegreeofangels = 180; $ $ /**There are several sides*/ - Public Static Final intLines = 3; - the Public intGetdegreeofangels () { - returnDegreeofangels;Wuyi } the - Public voidDraw () { WuSystem.out.println ("i m a Triangel ..."); - } About $ Public Static voidprint () { -System.out.println ("Printing Triangel ..."); - } -}
The output is:
Shape field[degreeofangels]:0
Shape [Getdegreeofangels () method]:180
Printing Shapes ...
Shape Field[lines]:1
Java Dynamic binding