ASP. NET web application development Quick Start series 3-C # Object-Oriented Programming outline-class

Source: Internet
Author: User

 

Note: Some data sources are from the Internet.

 

1. What is an object?

In the real world, everything is an object. A car, an apple, a table, and a person are all objects.

Objects in C # Represent the structure of data and behavior.

 

2. What is object-oriented

All operations are based on objects.

Four principles of object-oriented:

1) Abstract action ):

It is to find the essence of things, extract common and essential features from numerous things, and discard their non-essential features. For example, vehicles, trains, and airplanes, their common features are transportation tools. The process of getting the concept of transport is an abstract process. Therefore, to be abstract, a comparison is required, and a common part cannot be found without comparison.

Common features are the features that distinguish a class of things from other things. These differentiated features are also called essential features. Therefore, the common feature of extracting things is to extract essential features of things and discard different features. Therefore, the abstract process is also a process of cropping. All the different and non-essential features are cropped out.

The so-called common features are relative. They refer to the common characteristics in a certain aspect. For example, for cars and rice, from the perspective of sales, both products have prices. This is their common feature, while in other aspects, they are different. Therefore, in abstraction, the same and different are determined from what perspectives. The abstract angle depends on the purpose of analyzing the problem.

From the programming point of view, it is to use OOP to describe the abstraction to enable the computer to identify and run stably on the computer. Of course, abstraction should come from specific needs. In the process of software development, identifying stable requirements, identifying core requirements, identifying conceptual requirements, designing the system architecture, and defining the interface relationships between components in the system are all abstract processes, it is a process that reflects the essential features of the system. Abstract is stable and permanent.

Abstract antonyms are specific. People often say, "You are too abstract. Can you tell me something specific ?" In the development language, there are abstract classes and specific classes. Specific classes can inherit from abstract classes and can be instantiated. Abstract classes can be derived from many different classes. The stability of the system is embodied in the abstract class, and the changes of the system are embodied in the specific class. The level of an abstract class is higher than that of a specific class. Therefore, we can say that the system is stable and vivid due to abstraction.

 

2) Encapsulation)

In OOP technology, encapsulation is to hide content that is not allowed to be accessed externally or that is not concerned externally, and only expose (publish) content that is allowed to be accessed externally.

 

3) modularity)

Modularization is to divide the project into several parts by function. Each part uses its own public interface for communication. In addition, high cohesion and low coupling are required. The smaller the dependencies between modules, the better.

 

4) Hierarchy)

"Divide and conquer ". Layering is an important idea in software design, especially for developing large software systems. Divide and conquer is a commonly used method in computers. For example, the TCP/IP layer-7 protocol is a typical hierarchical application. Any qualified software developer must master the hierarchical thinking.

. Net has the concept of "Three-tier architecture". The so-called three-tier architecture refers to the presentation layer, business logic layer, and data access layer. The presentation layer is a user interface, such as a web form or a Windows form, used to display information and process user requests. The business logic layer is the core application part of the system to implement actual business activities. The data layer is the layer that operates data (tables in the database or data sources in other text forms. The perfect three-tier structure requires that you modify the presentation layer instead of the logic layer, instead of the data layer.

 

Three elements of object-oriented:

1) Encapsulation)

2) Inheritance (inherits)

There is a word called "Zi Chenggong". Simply put, the son inherits the father's career. So, after the son inherits the father's career, the son can also "Zi Chenggong, he can further develop his career, so his son will inherit from his father's and his grandfather's career. In C #, inheritance also contains such a layer of meaning. Inheritance refers to the ability to use all the functions of the existing parent class and extend these functions without re-writing the parent class.

3) polymorphism)

 

Polymorphism (polymorphism) is a Greek term that refers to "multiple forms ". In C #, we can understand that the same operation acts on different objects and can have different interpretations to produce different execution results. Polymorphism is implemented by overwriting the virtual function method in the base class in the derived class.

There are two types of polymorphism: one is the polymorphism at compilation and the other is the polymorphism at runtime.

Polymorphism during compilation: The polymorphism during compilation is implemented through overload. For non-Virtual members, during compilation, the system determines the operation based on the passed parameters, returned types, and other information.

Runtime polymorphism: The Runtime polymorphism refers to the operation that is performed only when the system is running. C # The Running polymorphism is implemented by override virtual members.

 

3. Class and Class Members

 

Class is the most powerful type in C #, and is a kind of structure, used inProgramDescribes the real thing and the relationship between each other. A class is like a blueprint, which defines the type of data and behavior:

Definition Syntax:

[Access modifier] [ Static ] Class Class Name
{
// Class subject
}

 

Definition example:

///   <Summary>
/// Create a car
///   </Summary>
Public   Class Car
{
// Description of vehicle attributes and Behaviors
String Facecolor =   Null ;
}
 
///   <Summary>
/// Create an animal
///   </Summary>
Public   Class Animal
{
// Description of animal attributes and Behaviors
}

 

AboveCodeIt means creating a public class car, creating a public class animal (animal ); "Class" indicates that a class object is created. When creating a class, the "class" keyword is always used and must appear in this position.

Usage class:

Instantiation class: Instance name of the class name = New Class Name ();

Member: Instance name of the class. member name;

//Instantiate the car class and animal class

Car instancecar =   New Car ();
Animal instanceanimal =   New Animal ();

//Member
Instancecar. facecolor="Red ";
Console. writeline ("the color of this car is :{0} ", Instancecar. facecolor );

 

Static class:

[Access modifier] Static   Class Class Name
{
// Static Member
}

 

Static keywords are critical.

 /// <Summary>

/// Create a car
///   </Summary>
Public   Static   Class Staticcar
{
// Description of vehicle attributes and Behaviors
Public   Static   String Facecolor =   Null ;
}

 

Static features:

1) Static classes cannot be instantiated.

2) directly use "class name. member name" to declare static members of the class.

 

4. Access Modifier

 

You can see that the keyword "public" is used to define the class. This is an access modifier, which limits the accessible range of the class (object.

Public

Any other code in the same assembly or other assembly that references the Assembly can access this type or member. Public access is the highest allowed access level. There are no restrictions on accessing public members.

Private

Only the code in the same class or structure can access this type or member. Private access is the minimum allowed access level. Private Members are accessible only when their classes and struct are declared.

Protected

Only code in the same class or structure or derived class can access this type or member. A protected member can be accessed by a derived class instance in its class. The protected member of the base class is accessible only when the base class type is accessed.

Internal

Any code in the same assembly can access this type or member, but the code in other assembly cannot.

Protected internal

Any code in the same assembly or any derived class in other assembly can access this type or member.

 

Note:

One member or type can only have one access modifier, except when using the protected internal combination.

Access modifiers are not allowed in the namespace. The namespace has no access restrictions.

The next section describes class members.

 

Related Article

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.