C # Self-study Road 15

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.