|
Java Polymorphism is one of the three main features of object-oriented. (Encapsulation, inheritance, polymorphism), which refers to the specific type of reference variable defined in the program and the method call made by the reference variable are not deterministic during programming, but are determined during the program's run, that is, the instance object of the class to which a reference variable is inverted. The method call that the reference variable emits is exactly the method that is implemented in the class that must be determined by the time the program is run. Because when the program is run to determine the specific class, so that, without modifying the source code, you can bind the reference variable to a variety of different class implementations, resulting in the invocation of the specific method of the reference change, that is, do not modify the program code can be changed when the program runs the specific code, so that the program can select multiple running state, This is polymorphism. Polymorphism enhances the flexibility and extensibility of the software.
Java polymorphism is divided into:
1. Overloads and overrides of the method.
Overloading: Two methods with the same return value and function name in the same class, and a different argument list.
Overriding: A method of a subclass that overrides a method of a parent class that requires the same method name and parameters
2. The polymorphism of the object
Here we mainly introduce the polymorphism of the object
My own understanding is that small turn large automatic conversion, large turn small need to cast. Parent class is greater than child class.
(1) Parent class reference to child class instance
-
public class Mystring {
-
-
public static void Main (string[] args) {
-
-
Parent class is large, sub-class small, small turn large automatic conversion, can only call the parent class also has the method
-
Person temp=new student ();//reference to parent class to child class object
-
Temp.say ();
-
-
Person temp1=new teacher ();
-
Temp1.say ();
-
}
-
-
}
-
Abstract class
-
Abstract class Person
-
{
-
public String name;
-
-
-
Public String GetName () {
-
return name;
-
}
-
-
-
public void SetName (String name) {
-
THIS.name = name;
-
}
-
-
public abstract void Say ();//abstract method
-
}
-
Student class
-
Class student extends person//inherit person class
-
{
-
Implementing an abstract method
-
public void Say ()
-
{
-
System.out.println ("I am a student ....") ");
-
-
}
-
}
-
Teacher Class
-
Class teacher extends person//inherit person class
-
{
-
-
public void Say ()
-
{
-
System.out.println ("I am a teacher ....") ");
-
-
}
-
Extension methods
-
public void SayHello ()
-
{
-
System.out.println ("Hello, I am a teacher ....") ");
-
}
-
}
Copy Code
650) this.width=650; "id=" aimg_288 "src=" http://techfoxbbs.com/data/attachment/forum/201505/21/ 110413br5n9255r2fnk889.png "class=" Zoom "width=" 274 "alt=" 110413br5n9255r2fnk889.png "/> (2) The subclass object points to the parent class reference
-
public class Mystring {
-
-
public static void Main (string[] args) {
-
-
The parent class is large, the sub-class is small, auto-converted, and only the parent class can call methods
-
Person temp=new student ();//reference to parent class to child class object
-
Temp.say ();
-
-
Person temp1=new teacher ();
-
Temp1.say ();
-
Extension method for------------call teacher SayHello ()
-
The parent class person does not have a SayHello method and cannot directly Temp1.sayhello ()
-
-
Child class is less than parent class, requires casting
-
Teacher temp3= (teacher) Temp1;
-
Temp3.sayhello ();
-
-
}
-
-
}
-
Definition of omitted class
Copy Code
Execution Result:650) this.width=650; "id=" aimg_289 "src=" http://techfoxbbs.com/data/attachment/forum/201505/21/ 110443w77q858wvwrr4067.png "class=" Zoom "width=" 313 "alt=" 110443w77q858wvwrr4067.png "/>
I think remember small turn large automatic conversion, large turn small need to cast. The parent class is better understood than the subclass
Since the parent class can be automatically converted to subclasses, we can use this feature to solve a problem that is often encountered
-
public class Mystring {
-
public static void Main (string[] args) {
-
Student temp=new student ();
-
Say (temp);
-
Teacher Temp1=new teacher ();
-
Say (TEMP1);
-
}
-
public static void Say (Student temp)
-
{
-
Temp.say ();
-
}
-
-
public static void Say (teacher Temp1)
-
{
-
Temp1.say ();
-
}
-
Definition of omitted class
-
}
Copy Code
we found that say () overloaded two parts due to the different parameters required. In this case, if the class is many, do not overload a lot of copies. Here we use the polymorphic of the object to be able to solve very well. Change the code to:
-
public class Mystring {
-
-
public static void Main (string[] args) {
-
-
Person temp=new student ();
-
Say (temp);
-
-
Person temp1=new teacher ();
-
Say (TEMP1);
-
-
-
}
-
public static void Say (person temp)
-
{
-
Temp.say ();
-
}
-
Definition of omitted class
-
}
Copy Code
Of course, the method of invocation can only be used by the parent class, and this is because the parent class also has the Say () method. To solve this problem, we introduced the keyword instanceof.
The basic syntax for instanceof is: Object instanceof type-----------Returns a Boolean value
To determine whether an object belongs to an instance of a class, returns true, otherwise false
Below we call the teacher extension method SayHello () in the Say () method. Both the student and the person classes do not use this method, and the code above is definitely an error. This is where we can change the code to:
-
public class Mystring {
-
-
public static void Main (string[] args) {
-
-
Person temp=new student ();
-
Say (temp);
-
-
Person temp1=new teacher ();
-
Say (TEMP1);
-
-
-
}
-
public static void Say (person temp)
-
{
-
Cast to teacher if it is a teacher object (big turn small)
-
if (temp instanceof teacher)
-
{
-
Teacher ok= (teacher) temp;
-
Ok.sayhello ();
-
}
-
-
}
-
-
-
Definition of omitted class
-
}
Copy Code
Parent class Big turn small automatic conversion, reminds me of all classes of the parent classObjectclass, of course it is the biggest. All of our code can also be changed to:
-
public class Mystring {
-
-
public static void Main (string[] args) {
-
-
Person temp=new student ();
-
Say (temp);
-
-
Person temp1=new teacher ();
-
Say (TEMP1);
-
-
-
}
-
Using the object class
-
public static void Say (Object temp)
-
{
-
Cast to teacher if it is a teacher object (big turn small)
-
if (temp instanceof teacher)
-
{
-
Teacher ok= (teacher) temp;
-
Ok.sayhello ();
-
}
-
-
}
-
-
-
Definition of omitted class
-
}
-
Abstract class
Copy Code
This blog is a summary of my study of Java polymorphism, but also come out and share with you, there is nothing wrong still look. |