MultipleIt allows objects of different classes to respond to the same message. Polymorphism includes parameterized polymorphism and inclusion polymorphism. Polymorphism language is flexible, abstract, behavior sharing,CodeThe advantages of sharing have effectively solved the applicationProgramThe same name of the function. There are two manifestations of polymorphism: overload and overload. First, overload occurs in the same class. It has nothing to do with what parent class subclass and inheritance. Besides the function name, a function parameter (number and type) is used to identify a function ). That is to say, a class can have two or more functions called the same name and their parameters are different. There is no relationship between them. They are different functions, but they may have similar functions. Therefore, they are named the same and increase readability! Override occurs in the subclass! That is to say, if inheritance is required, overwriting occurs. We know that we inherit a class, and we have all the methods of the parent class. If you are uncomfortable with the method and want to change the function, implement the function again in the subclass. In this way, when this method is called, it is the process of executing the subclass. The function in the parent class is overwritten. (Of course, when overwriting, the function name and parameter must be exactly the same as that in the parent class. Otherwise, your method will not have any effect on the methods in the parent class, because the two are two functions and have no relationship.
----------------------------------------
MultipleThe State is opposite to the inheritance concept.Polymorphism is the opposite of inheritance.
Polymorphism is the implementation of the parent class, and inheritance is the implementation of the Child class to tune the parent class. Because I don't know which subclass will inherit the polymorphism, all the things I define are virtual functions, when you execute this virtual function, first find the object subclass represented by this virtual function and execute the relevant code in the subclass.
----------------------------------------------------------------
MultipleState is also a very important part of Java, that is, a method name can be used multiple times. The specific method you call is determined based on different parameters, if there is a method named "Animal call", when the parameter you pass is an animal dog, call the dog call. If it is a cat, call the cat call. This is polymorphism.
Bytes ------------------------------------------------------------------------------------------------
This article is intended for beginners of Java. We often see that Java is polymorphism in teaching materials. However, general textbooks only focus on theory and have very few practical operations. Now I will briefly describe the concept of polymorphism in the Code. Due to my limited level, it is inevitable that there are deficiencies.
First of all, we have several types of computers, PCs, and laptops.
Java code
-
- ClassComputer {
-
-
- Public VoidTurnon (){
-
- }
- }
Now we have computers, so personal PCs and laptops all inherit from them.
Java code
-
- ClassPCExtendsComputer {
- Public VoidTurnon (){
-
- System. Out. println ("PC has turn on");
-
- }
-
- }
-
-
- ClassNBExtendsComputer {
- Public VoidTurnon (){
-
- System. Out. println ("Nb has turn on");
-
- }
-
- }
We can see that each class has a turnon () method, but in the parent class, this method is empty, and the sub-classes have different implementations, which may not be obvious here. If there is another method that accepts these classes as parameters, just like this
Java code
- ClassTest {
- Public VoidGo (computer ){
- Computer. turnon ();
- }
- }
This method receives base class parameters. Of course we can pass sub-classes to the method. We should also do this.
Java code
- Go (NewPC ());
- Go (NewNB ());
In this way, he will call the turnon () method of the specific subclass, so the output of these two calls is different. if we want to call the turnon () method of each subclass without using the base class as the parameter, we need to get the go () method with the same two methods bodies and receive the overload of different parameters.
Java code
-
- Public VoidGo (PC Computer ){
-
- Computer. turnon ();
-
- }
-
- Public VoidGo (NB computer ){
-
- Computer. turnon ();
-
- }
This is also true, but it will produce a lot of repeated code and is not easy to maintain. Of course, this is only the most basic part of polymorphism. the Java polymorphism mechanism also has many features, for these suggestions, let's take a look at the think in Java book, which describes in detail. Here we just briefly describe it and hope you will not laugh at it.
Another example of polymorphism.
Code
Class Order
{
Public Order (customer)
{
ID = Guid. newguid (). tostring ();
M_customer = Customer;
M_products = New List < Product > ();
}
Public Readonly StringID;
PrivateCustomer m_customer;
private List product > m_products;
Public void addproduct (product)
{< br> m_products.add (product);
}
Public DoubleTotalamount ()
{
DoubleTotalamount= 0.0;
Foreach(Product ProductInM_products)
Totalamount+ =Product. price;
ReturnTotalamount*M_customer.discount;
}
Public Override String Tostring ()
{
Return " Dear " + M_customer.name " , \ N " +
" \ TTotal amount of order " + ID +
" Is " + Totalamount (). tostring ();
}
}
Abstract ClassCustomer
{
Public Abstract StringName {Get;}
Public Abstract DoubleDiscount {Get;}
}
class supervipcustomer: customer
{< br> Public supervipcustomer ( string name)
{< br> m_name = name;
}
private string m_name;
Public override string name
{< br> Get { return m_name ;}< BR >}
private const double c_discount = 0.65 ;
Public override double discount
{< br> Get { return c_discount ;}< BR >}
Public override string tostring ()
{< br> return " Name: " + m_name + " [supervip] " ;
}< BR >}
ClassVipcustomer: customer
{
//Code here
}
ClassNormalcustomer: customer
{
//Code here
}