Home > Backend Development > C#.Net Tutorial > Virtual methods and abstract methods in C#

Virtual methods and abstract methods in C#

Guanhui
Release: 2020-07-23 17:51:34
forward
3349 people have browsed it

Virtual methods and abstract methods in C#

I learned a lot today at Yunhe Academy. This time I can only summarize the use of virtual methods and abstraction in C#.

Theory:

Virtual method:

  • The method modified with virtual is called a virtual method

  • Virtual Methods can be overridden in subclasses through the override keyword

  • Common virtual methods: ToString() Equals

Abstract Method:

  • Abstract classes and abstract methods are modified by abstract

  • Note on the use of abstract

– Abstract methods have no method bodies
– Abstract members can only exist in abstract classes
– Abstract classes can have non-abstract members
– Derived classes of abstract classes must implement abstract method bodies
–Abstract classes can only be used as base classes and cannot be instantiated

Supplement:

Rewrite: different behaviors of different objects for the same method call (referring to when different objects receive the same message, Will produce different behaviors)

Practical operation:

Define a People parent class, including Name, Gender, Age, and a PeoHi virtual method, Define two subclasses Teacher, including two attributes Year and Salary and a PeoHi method; subclass Student, including StadyNum and a PeoHi method.

Define subclass Teacher (Student The subclass is defined in the same way as Teacher and will not be introduced here)

Output in the Main function

Output result

Abstract method, define a parent class Person, and subclass Chinese

##Recommended tutorial: "

C#"

The above is the detailed content of Virtual methods and abstract methods in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template