Three aspects of C # Object-oriented inheritance, encapsulation, polymorphism learning Summary

Source: Internet
Author: User

1. base classes and Extension classes

(1) How C # Implements inheritance: Class inheritance and interface inheritance

(2) The inheritance is represented by a colon (:). The inherited class is called the parent or base class, and classes that inherit from the base class are called extension classes, or derived classes or subclasses. Base class for all classes System.Object

declaratively: [Access modifier]class Extension class Name: base class name

{

}

If b inherits from a, you can also use a cast operation to convert it to a object. such as: a b= (a) new B (); At this point, the B object will be limited to the data and behavior of the A object, and the data and behavior in the B object can no longer be accessed, unless the method in a is overloaded with B, the method of B is accessed. After you cast B to a, you can also convert a to B, but not all instances of a can be coerced to B under any circumstances, only those instances that are actually instances of B can be cast to B.

(3) The extension class cannot inherit private methods defined in the base class, only the public members of the base class, or the protected members.

(4) When initializing a base class, the constructor of the base class is called first when the constructor of the extension class is called.

2. Polymorphism:

(1) Definition: The same operation acts on different classes of instances, different classes will be interpreted differently, and finally produce different results. That is: To create a parent class object, its contents can be the parent class, or it can be the subclass, the subclass and the parent class have defined the same method, when using the object call this method, the parent class of the same name object will be called, when the parent class of this method to add the virtual keyword, A method with the same name as the child class is called.

(2) The method of realizing polymorphism:

(I) implemented through inheritance. Multiple classes can inherit from the same class, and each extension class can provide different functionality based on overriding the base class members.

(II) implemented through an abstract class. Abstract classes cannot be instantiated by themselves and can only be used by inheritance in an extension class. Some or all of the members of an abstract class are not necessarily implemented, but all implementations in the inheriting class can be overridden by the implemented members of the abstract class, and the inheriting class can still implement other functions.

(III) implemented through interfaces. The interface declares only the methods, properties, leave that the class needs to implement, and the types of arguments each member needs to accept and return, and their particular implementation needs to implement the class to complete.

Three aspects of C # Object-oriented inheritance, encapsulation, polymorphism learning Summary

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.