C # object-oriented Basics

Source: Internet
Author: User
Tags modifiers

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

Private indicates that only members of the same class are allowed to access the database. Other classes, including its sub-classes, cannot be accessed, which is commonly known as private.

Protected indicates that the subclass can have full access to the base class during inheritance. That is to say, the class members modified by protected are public to the subclass, but not to other classes.

If the member in the class does not have a modifier, it is considered private.

Generally, the field is private, that is, the private variable, and the attribute is public, that is, the public variable.

Attribute names are generally capitalized, and fields are generally lowercase or prefixed with "_".

 

The constructor, also called constructor, is actually initializing 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.

All classes have constructor methods. If no encoding is performed, the system generates an empty constructor by default. If a constructor is defined, the default constructor fails.

 

Method overloading provides the ability to create multiple methods with the same name, but these methods require different parameter types or numbers.

 

Attribute is a method or a pair of methods, but in the code that calls it, it is a field, and the attribute is suitable for the use of method calls in a field. A field is the data that the storage class needs to meet its design requirements. A field is a class-related variable.

 

Three features of object-oriented: encapsulation, inheritance, and Polymorphism

Advantages of encapsulation: first, good encapsulation can reduce coupling.

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

Third, the class has clear external interfaces.

 

The inheritance of an inherited object represents a relationship of 'is-a'. If two objects A and B can be described as 'B is A', B can inherit.

If the subclass inherits from the parent class: 1. The subclass has non-private attributes and functions of the parent class.

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

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

The sub-class inherits methods, fields, attributes, events, and indexers from its parent class. However, a constructor cannot be inherited but can only be called. You can use the base keyword to call Members of the parent class.

 

Polymorphism indicates that different objects can perform the same operation, but they must be executed through their own code.

First, the Child class appears as the parent class.

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.

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 choose to 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.

Note: Generally, the virtual method is used, but the attribute and event indexer can be virtual except for the field. Although the method can be virtual, there is still a method body that can actually do something.

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

For example, animal = new dog ();

Or

Dog dog = new dog ();

Animal animal = dog ();

 

Notes for abstract classes:

First, the abstract class cannot be instantiated.

Second, the abstract method must be overwritten by the quilt class. Abstract methods can be viewed as virtual methods without implementing the body.

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

Abstract Classes usually 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, Tree nodes should be specific classes, and tree branches should be abstract classes.

 

An interface combines implicit public methods and attributes to encapsulate a set of specific functions. Once the class implements the interface, the class can support the 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.

Interfaces cannot be instantiated, constructor methods and fields, modifiers, virtual or static declarations, and classes that implement interfaces must implement all methods and attributes of interfaces.

A class can support multiple interfaces, and multiple classes can also support the same interface.

The method or attribute in the interface cannot have modifiers, and the method does not have a method body.

Interface ichange

{

String changething (string thing );

}

 

Abstract classes can provide member implementations, but interfaces cannot contain Member implementations.

Abstract class abstract members can be partially implemented by the quilt class, and interface members need to fully implement the class

A class can inherit only one abstract class, but multiple interfaces can be implemented.

First, the class is the abstraction of objects, the abstract class is the abstraction of classes, and the interface is the abstraction of behavior.

Second, if the behavior spans objects of different classes, you can use interfaces. For some similar class objects, inherit abstract classes.

Third, from the design point of view, abstract classes discover common things from the subclass, generalize the parent class, and then inherit the parent class from the subclass. The interface does not know the existence of the subclass, the implementation of the method is unknown and pre-defined.

 

Array advantages: continuous storage in the memory, so you can quickly and easily traverse elements from start to end, you can quickly modify elements and so on.

Disadvantages of array: The size of array variables must be specified during creation, and it is also difficult to add elements between two elements.

 

Set

Arraylist is a part of the namespace system. Collections. It uses an array that can be dynamically increased as needed to implement the ilist interface. The size of arraylist is the metadata that can be saved by arraylist. The default initial capacity of arraylist is 0. As the element is added to 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.

Advantages of arraylist: The arraylist can be dynamically increased based on the used size, without the need to set its size in advance.

You can add, insert, or remove elements in a range at will, which is more convenient than arrays.

Disadvantages of arraylist: arraylist accepts all objects and treats all elements as objects. arraylist is not of type security.

Arraylist packs a value type into an object. When using a set element, You need to unpack it, resulting in high performance loss.

Generics are classes, structures, interfaces, and methods with placeholders (type parameters, these Placeholders are one or more types of placeholders stored or used by classes, structures, interfaces, and methods. A generic collection class can use a type parameter as a placeholder for the type of the object it stores: The type parameter appears as the type of its field and the parameter type of its method.

 

Delegation and events

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

A delegate is a type of reference method. Once a method is assigned to the Delegate, the Delegate will have the same behavior as the method, and the delegate object will be declared with the keyword delegate. When other classes or objects are concerned, classes or objects can be notified through events, and event objects are declared with the event keyword.

 

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.