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!