C # some confusing concepts (7) --------- parse abstract classes and abstract methods

Source: Internet
Author: User
Tags define abstract

The C # knowledge on my laptop is basically sorted out, so this series of estimates about C # tips is coming to an end, in this process, thank you for your constant support. Your valuable suggestions have helped me a lot. Before introducing abstract classes and abstract methods, we should first mention the basic concepts of polymorphism. As a matter of fact, in the previous article, the replacement principle of the LITH has already explained the essence of polymorphism, "subclass objects can replace the location of parent objects, and the functions of programs are not affected ". Let's take a look at the code: copy the code /// <summary> // Create By: ZhiQiang // Create Time: /// </summary> class Person {// defines the virtual method for override of the subclass. When the subclass replaces the location of the parent class object, it can show a multi-state public virtual void Run () {Console. writeLine ("I am a person, I will run! ");} Public virtual void Say () {Console. WriteLine (" I am a person, I will speak! ") ;}} The code used to copy the code subclass is as follows: copy the code // define the Teacher class to inherit from the Person class Teacher: Person {public override void Run () {Console. writeLine ("I'm a teacher, I have to run slowly");} public override void Say () {Console. writeLine ("I'm a teacher. I Have To Say praise! ") ;}} // Defines the Student class to inherit from the Person class Student: Person {// The subclass overrides the virtual method public override void Run () {Console. writeLine ("I'm a student, I will speed up! ");} Public override void Say () {Console. WriteLine (" I'm a student and I can speak English! ") ;}} To copy the code, you need a class that implements polymorphism. The Code is as follows: copy the code // class FeatureHuman that implements polymorphism {/// <summary> /// this method extracts polymorphism. When a subclass object is passed in, p points to a subclass object, you can call the subclass to override the method after the parent class method /// </summary> /// <param name = "p"> parent class or subclass object </param> public void outPutFeature (Person p) {p. run (); p. say () ;}} copy the code body code and implement polymorphism as follows: copy the code class Program {static void Main (string [] args) {// Person features Person p = new Person (); Program pro = new Program (); pro. outPutFeature (P); // Student features Student s = new Student (); pro. outPutFeature (s); // instructor features Teacher t = new Teacher (); pro. outPutFeature (t); Console. readKey () ;}} copies the code to run. The printed result is as follows: here we can find that the outputFeature method varies with the passed object (the parent class variable points to the subclass object ), the characteristics of different characters are printed, Which is polymorphism. The Code diagram is as follows: polymorphism is summarized as follows: 2. abstract classes and Methods Modified by abstract keywords in C # are called abstract classes and abstract methods. 1) abstract classes can have non-Abstract members and can be called to inherit them (abstract classes are called to define abstract members and inherited to sub-classes for implementation, at the same time, sub-classes can also call non-abstract members of the parent class) copy the code abstract class Person {// private int nAge; // abstract string strName; // The abstract class can contain non-abstract members, you can use public void Say () {Console for the inherited subclass. writeLine ("I am a parent class, I am a person! ");} Public virtual void Sing () {Console. WriteLine (" I am a parent class, I am a person, I can Sing! ");} // Run abstract method public abstract void Run ();} copy Code 2) the virtual Methods Modified by virtual in the abstract class can be as follows, the virtual modifier method is defined in the abstract class and compiled. Abstract classes are implemented to define abstract members and inherit them to sub-classes. Therefore, sub-classes can also implement virtual methods in abstract classes.

Related Article

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.