Object-oriented Basics

Source: Internet
Author: User
From: http://blog.csdn.net/wlccomeon/article/details/8581687

In August 5, 2014, the Ministry of Education issued the document "graduate student exemption new regulation", which was originally intended to successfully research and follow the teacher's research plan. After explaining to Mr Zeng, start to work hard. Recently, I began to learn Object-Oriented Programming slowly. Every step must be the best and best, so that I won't give myself a chance to regret it. I found the blog summarized by my predecessors on the Internet and learned it step by step.

 

Class and instance

1. An object is a self-contained entity identified by a set of identifiable features and behaviors.

2. A class is an abstract set of objects with the same attributes and functions.

3. The first letter of the class name must be capitalized. Multiple words are capitalized, And the 'public' modifier must be used to make public.

4. An instance is a real object. instantiation is the process of creating an object, which is created using the new keyword.

 

Constructor

1. constructor is used to initialize the class.

The constructor has the same name as the class. It has no returned value and does not require void. It is called when new is used.

2. All classes have constructor methods. If you do not encode the constructor, the system generates an empty constructor by default. If you have a defined constructor, the default constructor fails.

 

Method overload

1. Method overloading provides the ability to create multiple methods with the same name, but these methods need to use different parameter types.

2. During method overloading, the two methods must have the same method name, but the parameter type or number must be different.

3. Its advantage is that it can add features without changing the original method.

 

Attributes and modifiers

1. attribute is a method or a pair of methods, but in the code that calls it, it is a field (the field is the data that the storage class needs to meet its design requirements, fields are class-related variables), that is, attributes are suitable for the use of method calls in the form of fields.

2. the attribute has two methods: Get and set. The get accessor returns the same data type as the life attribute, indicating that the internal field value or reference can be obtained during the call;

The Set access issue does not display the set parameter, but it has an implicit parameter, which is expressed by the keyword value. Its function is to assign values to internal fields or references when calling properties.

3. Public indicates that the modified class members can be accessed by any other class, which is commonly known as common.

Private indicates that only members in the same class are allowed to access the data. Other classes, including its subclass, are not accessible, which is commonly known as private.

Generally, fields are private, and attributes are public. Protected indicates that the subclass can have full access to the base class during inheritance.

 

Encapsulation

1. Each object contains all the information required for operations. This feature is called encapsulation. Therefore, an object does not have to rely on other objects to complete its own operations.

2. Encapsulation has many advantages:

First, good encapsulation can reduce coupling,

Second, internal implementation of the class can be freely modified,

Third, the class has clear external interfaces.

 

Inheritance

1. Object Inheritance represents the relationship between 'is-a'. If two objects A and B can be described as 'B is A', B can inherit. Inheritance 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 unique personality.

2. Inheritance defines how classes are associated and shared. The inheritance method is to define the parent class and subclass, or the base class and derived class. The subclass inherits all the features of the parent class, And the subclass can also define new features.

3. If the subclass inherits from the parent class,

1. Subclass is used for attributes and functions of non-private parent classes;

Second, subclass has its own attributes and functions, that is, subclass can expand the attributes and functions that the parent class does not have;

Third, subclass can also implement the function of the parent class in its own way (method rewriting ).

4. The members inherited by a subclass from its parent class include methods, domains, attributes, events, and index indicators. However, for Constructor methods, it cannot be inherited but can only be called. You can use the base keyword to call Members of the parent class.

5. If you do not need inheritance, you must modify the function in all repeated methods. The more code you need, the more likely the error will be. The advantage of inheritance is, inheritance puts the common parts of all sub-classes in the parent class, so that the code is shared, which avoids duplication. In addition, inheritance makes it easier to modify or extend the inherited implementations.

6. Inheritance has a disadvantage, that is, if the parent class is changed, all child classes have to be changed. Inheritance destroys the packaging, and the Implementation Details of the parent class are exposed to the Child class. Inheritance is obviously a strongly coupled relationship between classes.

 

Polymorphism

1. polymorphism indicates that different objects can execute the same action, but they must execute the same action through their own implementation code.

2. In polymorphism,

1. Child classes appear as parent classes;

Second, subclass is implemented in its own way during work;

Third, when a subclass appears as a parent class, its unique attributes and methods cannot be used.

3. In order for the subclass instance to completely replace the class member from the parent class, the parent class must declare the member as virtual. This is achieved by adding the virtual keyword before the returned type of the member. Subclass can use the override keyword to replace the parent class implementation with its own implementation. This is the method to override, or it is called method override.

4. The principle of polymorphism is that when a method is called, the method implementation at the end of the Object Inheritance chain is called no matter whether the object is converted to the parent class or not. That is to say, virtual methods are dynamically bound and called based on their runtime type rather than the compilation type.

5. If you have never learned the design model, your understanding of polymorphism and object orientation is mostly superficial and one-sided.

 

Abstract class

1. C # allows class and method declaration as abstract, namely abstract class and abstract method.

2. Notes for abstract classes:

First, the abstract class cannot be instantiated;

Second, the abstract method must be overwritten by the quilt class;

Third, if a class contains abstract methods, the class must be defined as an abstract class, whether or not it contains other general methods.

3. abstract classes generally represent an abstract concept, which provides a starting point for inheritance. When designing a new abstract class, it must be used for inheritance. Therefore, in a hierarchy formed by inheritance relationships, leaf nodes should be specific classes, and tree branches should all be abstract classes.

4. abstract classes have as much code as possible and as little data as possible.

 

Interface

1. Interfaces combine implicit public methods and attributes to encapsulate a set of specific functions. Once the class implements the interface, the class can support all attributes and members specified by the interface. The Declaration interface is syntactically identical to the declaration abstract class, but it cannot provide the execution method of any member of the interface.

2. The class implementing the interface must implement all the methods and attributes of the interface. A class can support multiple interfaces, and multiple classes can also support the same interface.

3. The interface should be declared with an interface instead of a class. 'I' should be added before the interface name. There must be no modifier before the methods or attributes in the interface, and the method does not have a method body.

4. Differences between abstract classes and interfaces

A. abstract classes can be implemented by some Members. Interfaces do not include member implementations. Abstract members of abstract classes can be partially implemented by quilt classes. Interface members must implement full classes, A class can inherit only one abstract class, but can implement multiple interfaces.

B. Class is the abstraction of objects; abstract class is the abstraction of classes; interface is the abstraction of behavior

C. If the behavior spans objects of different classes, you can use interfaces. For some similar class objects, inherit abstract classes. That is, the Implementation interface does not conflict with the inherited abstract class.

D. From the design point of view, abstract classes discover common things from sub-classes, generalize the parent class, and inherit the parent class from the sub-classes. This improves the design of existing code by refactoring. However, the interface does not know the existence of a subclass at all, and the implementation of the method is still unknown and pre-defined.

 

Set

1. array advantages. For example, the array is stored continuously in the memory. Therefore, the elements can be quickly and easily traversed from start to end, and the elements can be modified quickly. The disadvantage is that the size of the array variable must be specified during creation, and it is also difficult to add elements between two elements.

2.. NET Framework provides specialized classes for data storage and retrieval. These classes are collectively referred to as collections. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interface.

3. arraylist is a part of the system. Collections space. It uses an array that can be dynamically increased as needed to implement the ilist interface.

4. The arraylist capacity is the number of elements that can be saved in arraylist. The default error capacity of arraylist is 0. As the element is added to the arraylist, the capacity will automatically increase as needed through reallocation. You can use an integer index to access the elements in this set. The index in this set starts from scratch. However, arraylist is not of type security.

5. Packing is to package the value type into an instance of the object reference type. The so-called unpacking is to extract the value type from the object. Compared with simple assignment, the packing and unpacking processes require a lot of calculations. When you bind a value type, you must create a brand new object with a higher ratio. Second, the forced conversion required for unpacking also requires a lot of calculations.

 

Generic

1. generics are classes, interfaces, structures, and methods with placeholders (type parameters, these Placeholders are one or more types of placeholders stored or used by classes, structures, interfaces, and methods. The generic wedding class can use the type parameter as the placeholder of the type of the object it stores. The type parameter is present as the type of its field and the parameter type of its method.

2. Generally, we recommend that you use a generic set, because this gives you the direct advantages of type security without deriving from the base set type and implementing type-specific members. In addition, if the set element is of the value type, the performance of the generic set type is generally better than that of the corresponding non-generic set type (and better than the type derived from the non-generic base set type ), because you do not need to pack elements when using generics.

 

Delegation and events

1. Delegation is the encapsulation of functions and can be used to specify a name for features of a method. An event is a special form of delegation. When a meaningful event occurs, the event object handles the notification process.

2. delegate is a type of reference method. Once a method is assigned to the Delegate, the delegate has the same behavior as the delegate. Events mean that when other classes or objects are concerned, classes or objects can notify them through events.

3. eventargs is the base class of the class containing event data.

There is no end to learning. continuous practice is required to truly become a good software engineer. Looking forward to this day!

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.