Object-oriented Basics

Source: Internet
Author: User
Class and instanceEverything is an object. To be precise, an object is a self-contained object, which is identified by a set of characteristics and behaviors recognized by each other. A class is an abstract set of objects with the same attributes and functions. Constructor:All classes have constructors. If you do not have an encoding, the system generates an empty constructor by default. If you have a defined constructor, the default constructor is invalid. Method overload:Method overloading provides the ability to create multiple methods with the same name, but these methods require different parameter types. EncapsulationDefinition: Each object contains all the information required for the operations it can perform. This feature is called encapsulation, so the object does not have to rely on other objects to complete its own operations.Benefits: good encapsulation can reduce coupling, and internal implementation of the class can be freely modified. The class has clear external interfaces. InheritanceDefinition: Object Inheritance represents a "is-a" relationship. If two objects A and B can be described as B as A, B can inherit, The successor can also be understood as the specificity of the successor, because it not only has the characteristics of the successor, but also has its own personality.Inheritance defines how classes are correlated and shared, and the inherited work method: defines the parent class and subclass, or the base class and the derived class. The subclass inherits all the features of the parent class, subclass not only inherits all features of the parent class, but also defines new feature 1. If the subclass inherits from the parent class, it can inherit all non-private attributes and functions of the parent class; 2: subclass has its own unique attributes and functions. That is, subclass can expand the attributes and functions that the parent class does not have. 3: sub-classes can implement the function of the parent class in their own way. sub-classes can inherit methods, attributes, events, indexers, and domains from their parent classes, however, constructors can only be called and cannot be inherited. for members that call the parent class, you can use the base keyword public CAT (string name): Base (name) {}. Disadvantages: the sub-classes of the parent class have to be changed. Inheritance destroys encapsulation, and the Implementation Details of the parent class are exposed to the sub-classes. PolymorphismPolymorphism indicates that different objects can have the same action, but you must use your own implementation code. Note: 1: subclass appears as a parent class. 2: subclass is executed in its own way during work. 3: When a subclass appears as a parent class, the attributes and Methods unique to the subclass cannot be used. to completely replace the class member from the parent class with the subclass instance, the parent class must declare the member as virtual, that is to say, adding a virtual keyword before the return type is usually not virtual except for the field, and all attributes, events, and indexers can be virtual, the virtual method can have its own method body subclass to replace the implementation of the parent class with its own implementation. The override keyword must be added. Abstract classKeyword: abstract1: the abstract class cannot be instantiated. 2: The abstract method must be implemented by the quilt class. 3: If the class contains an abstract method, it must be declared as an abstract class, even if a class contains a general method, you should consider making the abstract class have as much common code as possible. The abstract class with as few data as possible represents an abstract concept, which is the starting point of an inheritance point, when we design a new abstract class, it must be used for inheritance. In a hierarchy formed by inheritance relationships, leaf nodes should be specific classes, and tree branches should be abstract classes, in general, as long as there is a derived class, this class should be an abstract class that can have constructor, but public constructor is not allowed, even if we declare an abstract class without constructor, the compiler will automatically add a default protected non-parameter constructor to the constructor. when instantiating the sub-class (whether or not it includes parameters), it will use: 1. initialize the member of the abstract class. 2. Use InterfaceAn interface is a combination of implicit public methods and attributes to encapsulate a set of specific functions. Once an interface is implemented, the class can support all attributes and members specified by the interface. The declared interface has the same syntax as the abstract class, but does not allow the execution of any member of the interface. Therefore, the interface cannot be instantiated, no constructor is required to implement all methods and attributes in the interface. The interface cannot have any modifiers. Interfaces can include methods, properties, events, and indexers, which are defined as private, The interface cannot contain constants, fields, constructors, destructor, and static members.   Destructor: User destructor class instance, and constructor Idea 1: No need to define the destructor in the structure 2: A class can only have one destructor 3: unable to inherit or overload destructor 4: unable to call the destructor, they are self-and automatically called. 5: no modifier or constructor for the destructor Differences between abstract classes and interfaces1: abstract classes can provide implementation of some Members, and interfaces cannot. 2: Abstract members in the abstract class can be partially implemented by the quilt class, And all interfaces must be implemented. 3: A class can only inherit one abstract class, but can inherit multiple interfaces. A class is the abstraction of objects, an abstract class is the abstraction of classes, and an interface is the abstraction of behaviors.

Object-oriented Basics

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.