Java programming — consider polymorphism in testing
Object oriented programming has three characteristics: encapsulation, inheritance and polymorphism.
Encapsulation hides the internal implementation mechanism of the class, which can change the internal structure of the class without affecting the use, and also protect the data. To the outside world, its internal details are hidden, and only its access methods are exposed to the outside world.
Inheritance is to reuse the parent code. If there is an is-a relationship between two classes, inheritance can be used., At the same time, inheritance also paves the way for the realization of polymorphism. So what is polymorphism? What is the implementation mechanism of polymorphism? Please see me uncover for you one by one:
The so-called polymorphism refers to the specific type pointed to by the reference variable defined in the program and the method call issued through the reference variable. It is not determined during programming, but only during the running of the program, that is, a reference variable will point to the instance object of which class, and the method call issued by the reference variable is the method implemented in which class, It can only be determined during the running of the program. Because the specific class is determined only when the program is running, the reference variable can be bound to various class implementations without modifying the source program code, resulting in the change of the specific method called by the reference, that is, the specific code bound when the program is running can be changed without modifying the program code, so that the program can select multiple running states, This is polymorphism. Exercise (1): create a cycle class with Subclasses unicycle, bycycle and tricycle. Demonstrate that instances of each type can be transformed upward to cycle through the ride () method
Upward transformation is to allow multiple derived classes from the same base class to be regarded as the same type.
Polymorphic method calls allow one type to distinguish from other similar types as long as they are derived from the same base class. This difference is shown by the specific implementation of each export type method, although these methods are called by the base class.
Output:
Unicycleis riding
Bicycleis riding
Tricycleis riding
In the above example, the three subclasses can be regarded as cycles, and passing them into the method ride () is an upward transformation.
However, the upward transformation is only regarded as, not forced transformation, so the result of the final method call is different, which is polymorphism.
Polymorphism is also called dynamic binding. What is dynamic binding?
The opposite of dynamic binding is static binding. All methods in C language are statically bound by default. Static binding, also known as early binding, is to complete the binding before the program runs. In other words, the code is what it is written.
Dynamic binding does not decide which entity the method calls until runtime.
All static and final methods in Java are statically bound, and all other methods are dynamically bound.
Exercise (2): add the @ override annotation to the geometry example.
Exercise (3): add a new method in the base class shape.java to print a message, but do not overwrite this method in the export class. Please explain what happened. Now, override this method in one of the export classes and not in other export classes. Observe what happens. Finally, override this method in all export classes.
Exercise (4): add a new shape type to shapes.java and verify in the main () method whether polymorphism works on the new type as it does in the old type.
The above three exercises are completed in one example.
Exercise (5): Based on exercise 1, add the wheels () method to cycle, which will return the number of wheels. Modify the ride () method to call the wheels () method and verify that polymorphism works.
Add to the base class in the code in exercise (1)
Then in Main:
Finally, the output results successfully output the statements in the method, which proves that polymorphism does work.
Exercise (6): modify music3.java so that the what () method becomes the toString () method of the root object. Try system out. The printtln () method prints the instrument object (without upward transformation)
Exercise (7): add a new type instrument to music3.java and verify whether polymorphism works on the added new type
Exercise (8): modify music3.java so that it can randomly create instrument objects as in shapes.java.
Three exercises will be completed in one code.
Exercise (9): create a rodent: mouse, gerbil, hamster (hamster), and so on. This kind of inheritance hierarchy. In the base class, provide a method that is common to all Rodent. In the derived class, cover these methods according to the specific Rodent type, so as to observe them performing different behaviors. Create a Rodent array, fill different Rodent types, and then call the base class method to observe what happens.
This is similar to the previous example of instrument, In instrument, there is what () which is common to all instruments. In each subclass, we override this method and give different behaviors. Finally, we create an instrument array in main and call the base class method. The final result is that different classes call the base class method, and the output is the rewritten result of the class. It will not be repeated.
Exercise (10): create a base class containing two methods. In the first method, you can call the second method. Then, generate an exported class that inherits from the base class and overrides the second method in the base class. Create an object for the exported class, transform it upward into a base class, and call the first method to explain what happened.
Exercise (11) skip
Exercise (12): modify exercise (9) to demonstrate the initialization order of the base class and the exported class. Then add member objects to the base and export classes and explain the order in which initialization occurs during construction.
summary
The above is all about Java programming -- considering polymorphism in testing. I hope it will be helpful to you. Welcome to: java object-oriented programming (encapsulation / Inheritance / polymorphism) instance analysis, Java implementation of wechat public platform circle of friends sharing function detailed code, Java programming BigDecimal usage instance sharing, etc. if there are any shortcomings, please leave a message. Thank you for your support to this site!