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

Source: Internet
Author: User
Tags define abstract

Directory:

 

[C # tips] C # some confusing concepts -------- data type storage location, method call, out and ref Parameters

 

[C # tips] C # some confusing concepts (ii) -------- constructor, this keyword, partial classification, enumeration [C # tips] C # some confusing concepts (iii) -------- structure, GC collection, static members, static class [C # tips] C # some confusing concepts (iv) --------- parse Console. writeLine () [C # tips] C # some confusing concepts (v) --------- inheritance[C # tips] C # A summary of some confusing concepts (6) --------- resolve Lishi replacement principles and Virtual Methods

----------------------------------------- Split line ----------------------------------------------

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 a piece of code:

////// Create By: ZhiQiang // Create Time: 2014-2-9 ///Class Person {// defines the virtual method for override of the subclass. When the subclass replaces the location of the parent class object, the public virtual void Run () {Console can be displayed. writeLine ("I am a person, I will run! ");} Public virtual void Say () {Console. WriteLine (" I am a person, I will speak! ");}}

The subclass code is as follows:

// Defines that the Teacher class inherits 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! ");}}

The following code needs a class that implements polymorphism:

// Implement the polymorphism class FeatureHuman {////// This method extracts polymorphism. When a subclass object is passed in, p points to the subclass object, you can call a subclass to override the method of the parent class //////Parent class or subclass object public void OutPutFeature (Person p) {p. Run (); p. Say ();}}

 

The main code and the method for implementing polymorphism are as follows:

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 ();}}

Run and print the result as follows:

 

Here we can find that the outputFeature method is different based on the passed object (the parent class variable points to the object of the subclass), and prints out the characteristics of different characters, which is polymorphism.

The code is illustrated as follows:

The polymorphism is summarized as follows:

 

Ii. abstract classes and abstract methods

Classes and Methods Modified with 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, sub-classes can also call non-Abstract members of the parent class)

Abstract class Person {// private int nAge; // abstract string strName; // an abstract class can contain non-abstract members and 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! ");} // The abstract method of Run public abstract void Run ();}

2) virtual methods can be modified in abstract classes.

The code above defines the virtual modifier method in the abstract class, which is compiled successfully. 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.

 

3) abstract classes cannot be instantiated because they have abstract members, but abstract Members do not have a method body, for example,

 

 

4) Abstract members cannot be private. If the private subclass cannot be accessed

We can define private members in abstract classes, but it does not make sense. Because the subclass cannot access these private members, and the abstract class itself cannot be instantiated, the Private Members cannot access them.

5) The subclass must override the abstract method of the parent class.

Based on the above Code, we define a Student class that inherits the abstract class, but does not implement the abstract method of the abstract class. An error is reported during compilation. The Code is as follows:

6) There is no way to call the abstract method of the parent class through the base keyword in the subclass.

The principle is the same as above. Abstract issuance of abstract classes does not implement statements, even if it is called. However, you can use the base keyword to call non-abstract methods. The Code is as follows:

Class Program {static void Main (string [] args) {// Person p = new Person (); Student s = new Student (); s. run (); Console. readLine () ;}} class Student: Person {public override void Run () {base. say (); Console. writeLine ("I am a student and inherit the parent class. I can run it! ");}}

The output is as follows:

 

Abstract class mind map is summarized as follows:

 

 

Abstract Method

1) abstract methods must be defined in abstract classes,

Class Student: Person {public abstract void grouping (); public override void Run () {base. say (); Console. writeLine ("I am a student and inherit the parent class. I can run it! ");}}

An error is reported during code compilation, for example:

 

2) The abstract method must be modified with a keyword. The sample code is as follows:

Abstract class Person {// private int nAge; // abstract string strName; // an abstract class can contain non-abstract members and 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! ");} // The abstract method of Run. The method body cannot be left to the subclass to implement public abstract void Run ();

 

Abstract method mind map is summarized as follows:

 

So when can I use abstract classes?

① Sub-classes must override the parent class method (equivalent to defining a standard, specification)

② If the parent class does not need to be instantiated, the abstract class is used.

③ Abstract classes are for inheritance and Polymorphism

Finally, let's take a look at the sample code:

Define an abstract class that contains the abstract method Run ()

Abstract class Person {// Run abstract method. This method must be implemented as long as it inherits from my subclass. abstract void Run ();}

Define two subclasses respectively to inherit the abstract class Person

Class Student: Person {// public abstract void grouping (); public override void Run () {// base. say (); Console. writeLine ("I am a student and inherit the parent class. I can run it! ") ;}} Class Worker: Person {public override void Run () {Console. WriteLine (" I am a Worker and inherit the parent class. I Run in the factory every day! ");}}

To present polymorphism, we compile a method as follows:

// This method realizes polymorphism and returns the public static Person GetEntity (string str) {if (str = "Student") {return new Student ();} object of the subclass as needed ();} else if (str = "Worker") {return new Worker ();} return null ;}

The code in the main function is as follows:

Static void Main (string [] args) {// do not directly instantiate the parent class object, but declare the variable of a parent class object to receive the return value of the method Person p = GetEntity (Console. readLine (); p. run (); Console. readLine ();}

Run, enter the printed results of "worker" and "student" respectively as follows:

This part of content is over. I hope you will give more valuable comments.

 

Graduation internship exchange group: 221376964. You can also pay attention to my Sina Weibo.

 

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.