JAVA 15th (polymorphism and basic application)
Polymorphism:
The third feature of object-oriented definition: a function with the same name has multiple forms. For example, a function with a polymorphism has different parameter lists, and its existence is different, there is also a function that is placed in the parent class and in the subclass, and its existence is also different.
The object also has polymorphism.
Example: animals include pigs, cats, and dogs.
The cat object corresponds to the cat class.
Cat x = new CAT ();
At the same time, a cat is a type of animal, which can make a cat an animal.
Animal y = new CAT ();
Animal z = new dog ();
Animals are the parent class extracted from the group of dogs and cats.
The parent class references a subclass object.
I. Overview
// Object polymorphism // The manifestation of polymorphism, the parent type is directed to the self-object class animal {} class cat extends animal {} class dog extends animal {} public class Main {public static void main (String [] args) {// an object two forms animal small animal = new cat (); // a small animal creates an object through a cat. Animals are directed to/** cats and other things have the form of a cat, it also has the animal form. * This is the polymorphism of things * that is, an object corresponds to different types * the embodiment of polymorphism in code: * (parent class/interface) references point to the object of its subclass **/}}
Ii. Advantages of Polymorphism
It provides code scalability, and the code defined in the earlier stage can use the later content (only when animals are available)
The following code is used:
Abstract class animal {abstract void sing (); // call} class cat extends animal {void sing () {System. out. println ("");} void fun () // unique features of cat {System. out. println ("rat") ;}} class dog extends animal {void sing () {System. out. println ("Wangwang");} void fun () // unique function of the dog {System. out. println ("") ;}} class pig extends animal {void sing () {System. out. println ("hum");} void fun () {System. out. println ("") ;}} public class Main {public static void main (String [] args) {// a cat kitten = new cat (); kitten. sing (); // many cats cat two cats = new cat (); cat three cats = new cat (); catqun (two cats); catqun (three cats ); //.... // The embodiment of polymorphism, multiple animals dog 1 = new dog (); cat kitten 1 = new cat (); catqun (puppy 1 ); catqun (kitten 1); catqun (new pig ();} static void catqun (animal c) // animal c = new cat ()/dog () /pig (); {c. sing ();}}
Iii. Disadvantages and prerequisites of Polymorphism
1. disadvantages of polymorphism:
The content defined in the earlier stage cannot use the special content of the later stage subclass.
Static void catqun (animal c) {c. sing (); // c. fun ();-> there is no fun method in animal}
PS: Of course, the cat static void catqun (cat c) can be called directly, but we don't know how many species will appear in the future, with poor reusability.
2. Prerequisites for Polymorphism:
(1). It must be related, either inherited or implemented.
(2) overwrite (the parent class defines the function, and sub-classes are implemented. It is very troublesome to call a dog or a wolf. It's easy)
If we do not meet the requirements (2), we will use polymorphism if we do not have coverage. For example, if a dog looks at the house, it is normal, and a wolf looks at the house, there will be no problem.
The two prerequisites of polymorphism can be ensured to improve program scalability.
Iv. Transformation
Reflected in code:
Abstract class animal {abstract void sing (); // call} class cat extends animal {void sing () {System. out. println ("");} void fun () // unique features of cat {System. out. println ("rat") ;}} class dog extends animal {void sing () {System. out. println ("Wangwang");} void fun () // unique function of the dog {System. out. println ("") ;}} class pig extends animal {void sing () {System. out. println ("hum");} void fun () {System. out. println ("") ;}} public class Main {public static Void main (String [] args) {// previous command object/** cat kitten = new cat (); kitten. sing (); */animal a = new cat (); // The automatic type is increased. The cat object is promoted to an animal, similar to byte x = 3; int y = x;. sing (); // PS: Once a cat is promoted to an animal, its unique features cannot be accessed. // Professional statement, upward transformation. Objective: To restrict access to special functions // if the cat's special functions are still available, this object can be transformed down to cat c = (cat); // transform animal a to CAT cc. fun (); // The purpose of downward transformation is to use the special method in the subclass/* animal d = new animal (); * animal f = new dog (); * cat g = (cat) f; cat e = (cat) d; this type is not allowed. is a small animal a cat */}}
Note: For transformation, sub-class objects are being converted from beginning to end: the cat object will be transformed into an animal, and the cat object will be transformed into a cat
PS: Transition is purposeful
Exercise:
/** BLF and BLF2 story * BLF2 is the son of BLF **/class BLF {void function () {System. out. println ("write a program in C ++");} void speaks English () {System. out. println ("hello, world") ;}} class BLF2 extends BLF {void function () {System. out. println ("write programs in java");} void speaks Chinese () {System. out. println ("Hello, world") ;}} public class Main {public static void main (String [] args) {BLF x = new BLF2 (); // One day BLF2 impersonates BLFx. function (); // It can only be written in java, because BLF2 only uses javax. speaking English (); // yes. Make BLF2 speak English like BLF. // x. speaking Chinese (); // No, BLF2 has been transformed to BLF, and BLF2 features cannot be used to use BLF2 Z = (BLF2) x; // change to Z. speak Chinese ();}}
Not complete...