15. Polymorphism
Polymorphism is the abstract method defined by the parent class, after the subclass implements it, assigns the subclass to the parent class, and in the parent class, implements the specific functionality of the subclass by invoking an abstract method.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5E/08/wKiom1UpGI-joaEIAAHpH8hgcng808.jpg "title="] ' p]f9 }5~y0l3n))%590y ' G.png "alt=" Wkiom1upgi-joaeiaahph8hgcng808.jpg "/>
The lower path is a virtual method similar to the abstract method.
Namespace ConsoleApplication15
{
Class Program
{
public static void Fun (Player p)
{
P.train ();
}
static void Main (string[] args)
{
Player P = new player ();
Footballplayer f = new Footballplayer ();
Swimplay s= new Swimplay ();
Spriter sp = new Spriter ();
P.train ();
F.train ();
S.train ();
Sp. Train ();
Fun (s);
Fun (f);
Console.readkey ();
}
}
public class Player
{
public virtual void Train ()
{
Console.WriteLine ("Training! " );
}
}
public class Swimplay:player
{
public override void Train ()
{
Console.WriteLine ("Swimmer! " );
}
}
public class Footballplayer:player
{
public override void Train ()
{
Console.WriteLine ("Football player! " );
}
}
public class Spriter:player
{
public override void Train ()
{
Console.WriteLine ("Sprinter!");
}
}
}
All implement the polymorphism of the class.
There is also an interface (a class, similar to an abstract Class) (interface), which is similar. The difference is that if its members are abstract, a class can inherit multiple interface classes.
This article from "Very blog" blog, reproduced please contact the author!
C # Self-study Road 15