本文发表在 rolia.net 枫下论坛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.
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.
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(){..........}
}
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.
OK, if what I say is wrong, please give me your criticism .
And, "how are you", please visit my home page jvericwu.top263.net, maybe it could be help.
URL: jvericwu.top263.net更多精彩文章及讨论,请光临枫下论坛 rolia.net
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.
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.
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(){..........}
}
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.
OK, if what I say is wrong, please give me your criticism .
And, "how are you", please visit my home page jvericwu.top263.net, maybe it could be help.
URL: jvericwu.top263.net更多精彩文章及讨论,请光临枫下论坛 rolia.net