What is multi-state overload overwrite inheritance?

Source: Internet
Author: User

 
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
  1. ClassComputer {
  2. Public VoidTurnon (){
  3. }
  4. }

    Now we have computers, so personal PCs and laptops all inherit from them.

    Java code
    1. ClassPCExtendsComputer {
    2. Public VoidTurnon (){
    3. System. Out. println ("PC has turn on");
    4. }
    5. }
    6. ClassNBExtendsComputer {
    7. Public VoidTurnon (){
    8. System. Out. println ("Nb has turn on");
    9. }
    10. }
    11. 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
        1. ClassTest {
        2. Public VoidGo (computer ){
        3. Computer. turnon ();
        4. }
        5. }

      This method receives base class parameters. Of course we can pass sub-classes to the method. We should also do this.

      Java code
        1. Go (NewPC ());
        2. 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
      1. Public VoidGo (PC Computer ){
      2. Computer. turnon ();
      3. }
      4. Public VoidGo (NB computer ){
      5. Computer. turnon ();
      6. }
      7. 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
}

      1. Contact Us

        The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

        If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

        A Free Trial That Lets You Build Big!

        Start building with 50+ products and up to 12 months usage for Elastic Compute Service

        • Sales Support

          1 on 1 presale consultation

        • After-Sales Support

          24/7 Technical Support 6 Free Tickets per Quarter Faster Response

        • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.