Implementation of CSharp polymorphism (abstract class)

Source: Internet
Author: User

1. What is abstract class

Abstract classes are virtual classes, cannot create objects, are modified with an abstract, override in subclasses with override

Abstract classes can hold abstract methods, properties, or non-abstract methods, attributes (this can be seen in the following code)

Non-abstract classes can only be stored in non-abstract methods (this can be seen in the code below the class)

If the subclass is also an abstract class, then all the abstract methods in the parent class, the attributes do not have to be fully implemented; If the subclass is not an abstract class, then all the abstract methods in the parent class must be fully implemented (if not implemented, the error will be observed)

2. Let's take a look at the implementation of the code to do something

//Define a car parent class firstAbstract classCar {Private string_name;//modified with private, can only be used in the parent class         Public  stringName//can be inherited from subclasses again using the        {            Get            {                return_name; }            Set{_name=value; }        }         Public Abstract voidSay ();//abstract method, which must be overridden in a subclass         Public  voidStart ()//can be inherited from subclasses again using the{Console.WriteLine ("I'm a car, I'm starting now."); }      }//define a Benz class that inherits car  classBenz:car { Public Override  voidSay ()//overriding in subclasses{Console.WriteLine ("I'm a Big Ben, my name is {0}", name); }       }//define a BMW class, inherit car classBmw:car { Public Override voidSay ()//overriding in subclasses{Console.WriteLine ("I'm BMW, my name is {0}", name); }    }

3. If you are using polymorphic

A variable that defines a parent class Car B = new Benz ();
b = new BMW ();

First loaded is the Mercedes-Benz class, later loaded is the BMW class, installed that class, showing the characteristics of that class, which is polymorphic

3.1 A parent class can be loaded with different subclasses to achieve different patterns

3.2 When a parent class is inherited by more than one class, the methods in the subclass are many, and the methods in the parent class cannot be determined, the abstract method is defined in the parent class and then implemented in the subclass, and can be used directly when using polymorphism.

An overview of the implementation of CSharp polymorphism

Implementation of CSharp polymorphism (abstract class)

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.