---restore content starts---
Just finished Java, began the leak-check phase, in order to consolidate their knowledge and provide others with some inconsiderable help decide to open a blog, ask you to point out my shortcomings, do not begrudge words, but also hope that my summary can be helpful to others, to their own responsibility for others.
Start body: Term polymorphism: can be defined as " there are multiple patterns ", a polymorphic reference is a reference variable that can point to different types of objects at different times. multiple-state references allow you to invoke different, specific methods.
There are two ways to implement the polymorphism of a class method:
1: Method Overloading: You can declare multiple methods with the same name but with different parameters (number, type, and order). Notice that the overloaded method can only be declared in a class, and when the program compiles, it calls the specific method according to the different parameters. It also leads to another concept- static binding .
Static binding: The invocation of a method is bound when the program starts compiling. (amount)
PackageAnimal; Public classDog {//the Eat method of dog Public voidEatinta) {System.out.println ("My Dog eat foot num" +a); } //an overloaded Dog--eat method within a class (with different parameters) Public voideat (String str) {System.out.println ("My Dog eat foot" +str); } Public Static voidMain (string[] args) {Dog dog=NewDog (); Dog.eat ("Chicken"); Dog.eat (3); }}
My Dog eat footchickenmy dog eat foot num3
2: Method overrides: Derived classes "Inherit" the signature method from the base class, (The derived class is the same as the signature of the base class method, but the specific method differs-- a method name in different classes has a different implementation method ). Then the relationship between the derived class and the base class can be many, such as inheritance, and the interface is in short the relationship of is a. The override of the method also corresponds to the dynamic binding .
Dynamic binding: A program determines the specific method of invocation at run time . (There are examples)
PackageAnimal; Public classMydogextendsanimal{ Public voideat () {System.out.println ("It's a dog's meal."); } Public voidsleep () {System.out.println ("The dog is going to sleep .... "); } /*The program determines the specific method to invoke at run time. */ Public Static voidMain (string[] args) {Mydog mydog1=NewMydog (); Mydog1.eat ();//calling a derived class method eatMydog1.sleep ();//calling a derived class method SleppAnimal Mycat=NewMydog (); System.out.println ("The following is the upward transformation-the way animals Implement dogs (polymorphic) ——-————"); Mycat.eat ();//upward transformation-the way animals Implement dogs (polymorphic); /*for a downward transition, you must show that you specify that you must use a cast*/Animal mycat2=NewMydog (); Mydog mydog2=(Mydog) mycat2; System.out.println ("Animal-downward transformation realizes the sleep method of the dog ——————"); Mydog2.eat ();//pay attention to who eats, or how the dog is. Mydog2.sleep ();//Animal downward transformation of the realization of the sleep method of the dog }}
because the class to write about one hours, ashamed ... The above dynamic binding is divided into upward and downward transformation of two aspects, downward is a forced conversion, I think the most can reflect the multi-state of the idea (call not the same kind of different methods), write to this, salute!
---restore content ends---
--java polymorphism of leak-checking and vacancy