. NET abstract classes to implement polymorphism

Source: Internet
Author: User

One: What is polymorphism?

Multiple forms, both different objects are different for the same operation.

Ii. several considerations for abstract classes

1, abstract class using the abstraction modifier

2, abstract methods can only be found in abstract classes

3, abstract classes cannot be instantiated

4, there is no method body for abstract methods

5, abstract class can not be static class or sealed class

6, subclasses must override all the abstract methods of the parent class, unless the subclass is also an abstract class

7, there can be ordinary methods in the abstract class

8, abstract can have a constructor function

9, abstract methods in abstract classes are intended to constrain the method form of subclasses.

Three: "Instantiation" of an abstract class

Although the abstract class itself cannot be instantiated by new, he can point the Reference object to the real object of the subclass, or it can be called an indirect instantiation.

Person as parent class

Public abstract class person{public    int Age {get; set;} public string Name {get; set;} public person (int age,string name) {this. Age = Age;this. name = name;} public abstract void Say ();p ublic void Eat () {Console.WriteLine ("I am the parent class");}   }

Student class to inherit person

public class student:person{Public      Student (String Name,int age) {public                     Student (int., String name): Base (age , name) {this                 . Age = age;                 This. name = name;                 }                  public override void Say ()               {               Console.WriteLine ("Subclass Speak");                 } public void Eat () {Console.WriteLine ("I Am a Subclass");}}}     

When a parent class object points to a true object of a subclass, the subclass first walks the constructor of the parent class, and assigns a value to its properties in the constructor of the child class. ,

Person p = new Student (18, "Tommy"); P.say ();                  In this case, the parent class variable points to the subclass object, calls the method of the subclass,                                      //conforms to polymorphism, the parent class and the subclass method call the method P of the child class. Eat ();                  If no method does not have a relationship, the method of the parent class is called by default.                  Student stu = (Student) p; Stu. Eat ();                If you want to invoke a specific method of a subclass, you need to convert the type, called in Java, and             Console.readkey () down.
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.