×

Loading...

Hi Eric

本文发表在 rolia.net 枫下论坛Hi Eric,
I insert my understanding in what you posted above. Good night!

Polymorphism,
First let's think about how animal eats. Human being use mouth to chew, bird use beek to peck, mosquito use its pipe to drink, eat is one conception, but depends on different animal the eating really do differently when the do the same thing. This is Polymorphism. For example, there is a class Father and a class son derived from Father, they may both have a method work, but the father is a doctor and the son is a programmer.

class Father
{
public void work(){.............} // A doctor
}

class Son extends Father
{
//method work inherited from Father
public void work( ){..........} // programmer
}

Polymorphism here is in an inherited tree, a Father-Son chain.

--------------------------------------------------------------
I think the above is not polymorphism exactly, it is inheritance.
Suppose you are a tailor and have a task to design and sew a cloth which is suitble for the Father you mentioned above and is or will be also suitble for his desendants, his son or daughter or grandson or granddaughter or grandgrandson or grandgranddaughter ....
But you do not know how many desendants the Father have or will have. How can you make your design? Do not worry! OO tells you some rules. By obeying these rules you can complete your task successfully. These rules and some mechanism behide the rules is polymorphism. For Java example:

class Cloth {
WearOn(Father f);
}
Father f;
Son s;
GrandSon gs;
Cloth c;
c.WearOn(f);
c.WearOn(s);
c.WearOn(gs);
........
c.WearOn(any object of Father's desendant);

The rules and mechanisms which ensure the above pseudo-code work well is polymorphism.
-----------------------------------------------

And maybe we could take Polymorphism as many methods with same name but different parameter so they will act differently. Say, there is a class Human, it has method eat.
Class Human
{
public void eat ( Soup A)
{
//Use soup spoon to drink
}
public void eat ( Bread B)
{
//Use finger to pick up
}
}
So when you invoke method eat, depends on different food, you will act differently.

-----------------------------------------------
I think the two "eat" methods in Human class is not polymorphism, it is something called "overloading".
-----------------------------------------------

Interface,
Interface is a place to define a method and constrain the programmer to implement it when use this interface. Inside the Interface, you only need to define the method, then in the class which implement the interface you shoud code for the method.
When we build a class, if it implements interface runnable, it must code for method run( ). But the method run( ) is actually defined in Interface Runnable.

Another one of its usage is to implement "Multiple inheritance". For example, son gets character both from mother and father, but Java does not allow multiple inheritance. So we use Interface to complet this work.
Interface Father
{
public void methodF1( ){}

public void methodF2 ( ){}

public int methodF3 ( ) {}
}

Interface mother
{
public void methodM1( ){}

public void methodM2 ( ) {}
}

class Son implements Father, Mother
{
//Remember you have to implement all
//method defined in Father and Mother here.
public void methodF1(){.............}
public void methodF2 (){............}
public int methodF3 ( ){...........}
public void methodM1(){.........}
public void methodM2(){..........}
}
-----------------------------------------------
I do not think it is a "Multiple inheritance".
-----------------------------------------------

Inner class
This is just a method to write your code. You may put it outside a class and the inner class will not be an inner class any longer.

-----------------------------------------------
Inner class is not a method. Probably I misunderstand what you said.
-----------------------------------------------更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report