First, what is polymorphism? From what angle should we understand polymorphism? In fact, we might as well think of polymorphism into a variety of forms of things, such as cats and dogs can become animals, and animals can become cats and dogs.
In order to fully understand polymorphism, we can understand polymorphism from the following aspects.
First, from the manifestation of polymorphism: When we use polymorphic, we create a reference to the parent class, open up a subclass of space, because there is no space to open up the parent class, so the reference to the parent class to the child class object, not the real parent class itself created the object, Also, references to parent objects can be used as function parameters to receive subclass objects.
Second, the precondition of the multi-state realization: Must be the class and the class to have the relation, either inherits, either implements, exists the overwrite, actually has the abstract function.
Third, from the multi-state benefits: Greatly improve the scalability of the program
Application of polymorphism: 1. The reference of the parent class object to the subclass object is essentially an upward transformation, just as int turns into a double, and the son wears a father's clothes and pretends to be a father. 2. But after becoming a father, can only use the father's unique skills, how can the son use his own skills? This is the time to move down, take off the disguise, and turn the parent object's reference into a subclass type, you can use the subclass-specific skills.
The disadvantages of polymorphism: improved extensibility, but only the members of the parent class can be accessed using a reference to the parent class.
------------------------------------------------------------------------------------------Example----------------------------- -------------------------------------------------------------------
/*
Polymorphism: Can be understood as the existence of a variety of forms of expression.
Man: Man, woman
Animals: cats, dogs.
Cat x = new Cat ();
Animal x = new Cat ();
1, the manifestation of polymorphism
A reference to the parent class points to its own subclass object.
A reference to the parent class can also receive its own subclass object.
2, the precondition of polymorphism
Must be a relationship between a class and a class. Either inherit, or implement.
There is usually a precondition: there is coverage.
3. The benefits of polymorphism
The appearance of polymorphism greatly increases the expansibility of the program.
4, the drawbacks of polymorphism:
Extensibility is improved, but members in the parent class can only be accessed using a reference to the parent class.
5, polymorphic applications
*/
/*
Animals
Cat, dog.
*/
Abstract class Animal
{
abstract void eat ();
}
Class Cat extends Animal
{
public void Eat ()
{
System.out.println ("Eat fish");
}
public void Catchmouse ()
{
System.out.println ("Catch Mouse");
}
}
Class Dog extends Animal
{
public void Eat ()
{
System.out.println ("Eat Bones");
}
public void Kanjia ()
{
System.out.println ("housekeeping");
}
}
Class Pig extends Animal
{
public void Eat ()
{
System.out.println ("feed");
}
public void Gongdi ()
{
System.out.println ("Arch Ground");
}
}
//-----------------------------------------
Class Duotaidemo
{
public static void Main (string[] args)
{
Cat C = new Cat ();
C.eat ();
Dog d = new Dog ();
D.eat ();
Cat C = new Cat ();
/*
Cat C1 = new Cat ();
function (c1);
function (new Dog ());
function (new Pig ());
*/
Animal C = new Cat ();
C.eat ();
function (new Cat ());
function (new Dog ());
function (new Pig ());
}
public static void function (Animal a)//animal a = new Cat ();
{
A.eat ();
A.catchmouse ();
}
/*
public static void function (Cat c)//
{
C.eat ();
}
public static void function (Dog d)
{
D.eat ();
}
public static void function (Pig p)
{
P.eat ();
}
*/
}
--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------
/*
Polymorphism: Can be understood as the existence of a variety of forms of expression.
Man: Man, woman
Animals: cats, dogs.
Cat x = new Cat ();
Animal x = new Cat ();
1, the manifestation of polymorphism
A reference to the parent class points to its own subclass object.
A reference to the parent class can also receive its own subclass object.
2, the precondition of polymorphism
Must be a relationship between a class and a class. Either inherit, or implement.
There is usually a precondition: there is coverage.
3. The benefits of polymorphism
The appearance of polymorphism greatly increases the expansibility of the program.
4, the drawbacks of polymorphism:
Although extensibility is improved, members in the parent class can only be accessed using a reference to the parent class.
5, polymorphic applications
6. Characteristics of polymorphic occurrences in code (considerations for polymorphism use)
The second question: How to use subclass-specific methods.
*/
/*
Animals
Cat, dog.
*/
Class Cat extends Animal
{
public void Eat ()
{
System.out.println ("Eat fish");
}
public void Catchmouse ()
{
System.out.println ("Catch Mouse");
}
}
Class Dog extends Animal
{
public void Eat ()
{
System.out.println ("Eat Bones");
}
public void Kanjia ()
{
System.out.println ("housekeeping");
}
}
Class Pig extends Animal
{
public void Eat ()
{
System.out.println ("feed");
}
public void Gongdi ()
{
System.out.println ("Arch Ground");
}
}
//-----------------------------------------
Class DuoTaiDemo2
{
public static void Main (string[] args)
{
Animal a = new Cat ();//Type promotion. Upward transformation.
A.eat ();
What do I do if I want to invoke the cat's unique method?
Forces a reference to the parent class. Turn into a subclass type. Downward transformation.
Cat C = (cat) A;
C.catchmouse ();
Never do this by turning the parent class object into a subclass type.
What we can convert is that when the parent application points to its own subclass object, the app can be promoted or cast.
Polymorphism is a subclass of objects that are changing from beginning to finish.
Animal a = new Animal ();
Cat C = (cat) A;
/*
Uncle Bi x = new teacher ();
X. Lectures ();
Bi Teacher y = (bi) x;
Y. Watch a movie ();
*/
function (new Dog ());
function (new Cat ());
}
public static void function (Animal a)//animal a = new Cat ();
{
A.eat ();
/*
if (a instanceof Animal)
{
System.out.println ("haha");
}
Else
*/
if (a instanceof Cat)
{
Cat C = (cat) A;
C.catchmouse ();
}
else if (a instanceof Dog)
{
Dog C = (dog) A;
C.kanjia ();
}
/*
Instanceof: Used to determine the type of object. Object intanceof type (class type interface type)
*/
}
}
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------
The use of Java polymorphism