Multi-state virtual methods, abstract classes, interfaces, and multi-state abstract class interfaces

Source: Internet
Author: User

Multi-state virtual methods, abstract classes, interfaces, and multi-state abstract class interfaces

Virtual method:

1. Add the virtual keyword before the return value of the parent method and mark it as a virtual method, which indicates that this method can be overwritten by the quilt class.

2. The virtual method must have a method body, which does not contain any content.

3. The subclass can choose whether to override the virtual method as needed. If you want to rewrite the data, add the override keyword before the return value of the subclass method.

4. When a subclass overrides a virtual method, it can choose whether to use the base keyword to call the method in the parent class as required.

The syntax format of the virtual method is as follows:

1 public class Father 2 {3 public virtual void Do () 4 {5 //..... 6} 7} 8 public class Son: Father 9 {10 public override void Do () 11 {12 base. do (); // select whether to call. 13 //... code body 14} 15}

 

Abstract class:

1. Add abstract modifier before the key class of the defined class to indicate that this class is an abstract class. After the subclass inherits the abstract class, it uses the override keyword to override all abstract methods in the parent class.

2. abstract classes do not necessarily contain abstract methods, but abstract methods must exist in abstract classes. Abstract METHODS also need to be modified with abstract keywords.

3. There is no method body in the abstract method, and the abstract method must be implemented in the subclass.

4. abstract classes cannot be instantiated, but they can have constructors. Abstract classes have Abstract METHODS (without method bodies). If an abstract class is instantiated, it makes no sense to call these abstract methods without method bodies, so they cannot be instantiated.

The syntax format of abstract classes and abstract methods is as follows:

1 public abstract class Father // abstract class 2 {3 public abstract void Do (); // abstract Method 4} 5 public class Son: Father 6 {7 public override void Do () 8 {9 //... 10} 11}

 

Interface:When all methods in an abstract class are abstract methods, they can be expressed in the form of interfaces.

1. The interface is defined by the interface keyword without the class keyword. The interface name generally uses "IXxxx ".

2. The interface cannot contain fields, but can contain attributes (automatic attributes ).

3. methods defined in interfaces cannot have method bodies. They are all abstract methods, but do not need to be modified using abstract keywords. Therefore, interfaces cannot be instantiated and cannot have constructors.

4. Access modifiers cannot be added to interface members. The default value is public.

5. A class can implement multiple interfaces. Implemented interfaces are separated by commas. An interface can inherit multiple interfaces, and interfaces must be separated by commas; when an interface implements an interface, if the two interfaces have the same method, you can use the new keyword to hide the methods in the parent interface.

6. The successor must implement all methods in the interface.

The interface syntax format is as follows:

1 interface IFather 2 {3 void Do (); 4} 5 6 interface IMother: IFather 7 {8 new void Do (); // hide the method of the parent interface with the same name 9 void Do1 (); 10} 11 12 public class Son: IFather, IMother13 {14 public void Do () 15 {16 //...... 17} 18 19 public void Do1 () 20 {21 //..... 22} 23}

 

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.