OOP features such as C # supplements

Source: Internet
Author: User

OOP Overview

OOP is not only a specific software development technology, but also a set of software methodologies on how to view the relationship between software systems and the real world, how to study problems and how to solve them, and how to construct systems.

The emergence of the OOP method compensates for the shortcomings of the traditional method, first focusing on the objects involved in the actual problem, including various objective entities and events, to solve the problem design needs, various objects and object properties, the necessary operations and the relationship between the various objects, This builds the structure of the object and the sequence of events (commonly known as scripts) that need to be executed to solve the problem. Establish the required functionality through a message connection between the individual objects.

The definition of class in OOP method fully embodies the idea of abstract data type, and the class-based architecture can localize the program modification. In particular, once the system functions need to be modified, the main modification of some operations in the class, and the object represented by the class is basically the same, keeping the whole system still stable.

advantages of the OOP approach:

(1) Consistent with the way of thinking of human habits. The process of human cognition is the process of gradual thinking from general to special. The method of OOP conforms to this law, from looking for the object that asks for solution "What is it?" At first, the subjective arbitrariness is restricted by the cognition of the attributes of things and their essential laws. And the traditional way is from "How to do?" "At first, subjective randomness is greater.

(2) Good stability. The traditional method is mainly process-oriented, and is based entirely on the function and performance analysis of the system. Changes in the overall structure of the software are caused when functional requirements change. The Oop method is centered on "object". In the process of analyzing and studying the object and its attribute, the solving model is established according to its inherent law. The software system based on this method, no matter how the external environment or functional requirements change, but the inherent law of the object is not changed, so it does not cause the overall change of software structure, so the system is stable.

(3) reusability is good. Inheritance is an important mechanism of OOP methods. The basic object classes of a system designed with OOP can be reused by other new systems. Typically this is done through a class library that contains classes and subclass hierarchies.

(4) maintainability is good. Because the stability is good, even if the local modification, also does not affect the global: Because the object has the encapsulation and information hiding mechanism, makes the program easy to modify: the expansion of the system function, often by the original class based on the derivation of some new subclasses to achieve, easy to digest: Similarly, because the derived class inherits the characteristics of the original class, The system is also easy to test and debug, so that the system has good maintainability.

(5) Indicates the consistency of the method. There are different representations in the traditional structural analysis and design methods. For example, the data flow diagram is used in the analysis phase, and the representation of the structure diagram is used in the design phase. The Oop method, which is used throughout the development of the system, from OOA to Ood, until OOP, uses an always-on representation, which strengthens the internal consistency between analysis, design and programming, and improves the communication of information between users, analysts, designers, and programmers.

OOP characteristics of a class

Encapsulation of Classes

If the user wants to make sure that a class is not used as a base class, use the sealed keyword when defining the class. The only limitation is that abstract classes cannot be used as enclosing classes because the nature of the abstract classes determines that they must be used as base classes. The purpose of the enclosing class is to prevent accidental derivation operations. Specifically, because the compiler determines that the class does not have any derived classes, you can convert virtual function member calls on the enclosing instance to non-virtual function calls.

example, using the keyword sealed modifier class for a series of program debugging like the result

Inheritance of Classes

In the C # programming language, most classes can inherit except for sealed classes. In the OOP programming system, inheritance is an important concept, and for a class, inheritance is a subclass that contains the data structure and behavior of the parent class, including fields, properties, methods, and events. Although these definitions are not included in the definition of the subclass itself, these parent class members can still be used.

In the class's inheritance, the inherited class is called the base class or parent class, and the inherited class is called a derived class or subclass.

When a class is derived from another, the derived class naturally has base class data members, attribute members, method members, and so on, and the code for those members in the base class definition is not required to be overridden in a derived class definition, and in the definition of a derived class, you simply write code that is not in the base class definition. In this way, both the importance of the code and the efficiency of the program are improved, and the free space of writing code is provided for the special needs of the program design, thus improving the extensibility of the existing programming results.

in C #, the inheritance of classes follows these rules:

(1) A derived class can inherit only one base class

(2) Derived classes naturally inherit the members of the base class, but cannot inherit the constructor members of the class

(3) The basis of a class can be passed, assuming that Class C inherits Class B, Class B inherits Class A, then Class C has members of Class B and Class A, in C #, the object class is the base class for all classes, that is, all classes have members of the object class.

(4) A derived class is an extension to a base class that can declare a new member in a derived class definition, but cannot eliminate an inherited base class member.

(5) When declaring a member in a base class, no matter what access control it is, it is always inherited by a derived class, and the difference in access control only determines whether the members of the derived class can access the base class members.

(6) If a member with the same name as the base class is declared in the derived class definition, the member with the same name as the base class is overwritten, so that the derived class cannot directly access members of the base class with the same name.

(7) A base class can define virtual method members, so that derived classes can overload these members to represent the polymorphism of the class.

Derived classes are typically defined in the following format:

Access control modifier class derived class Name: base class name

{

Class Body

}

The access control modifiers can be public,protected and private. Public is usually used to guarantee the openness of the class, and public can be omitted because the access control defined by the class defaults to public. ": base class Name", which represents the inherited class.

When the base class and derived class definitions are complete, the objects declared with the derived class will contain the members of the base class (in addition to the constructors), so the derived class object can access the base class members directly. When you create a derived class object, the order in which you call the constructor is to call the base class constructor before calling the constructor of the derived class. To complete the task of allocating memory space for data members and initializing it.

The basis function class constructor passes a parameter, which must be implemented by the constructor of the derived class, in the form:

Public derived class constructor name (formal parameter list) base (the argument list passed by the basis function class constructor)

{   }

"Base" is a C # keyword that represents a parameterized constructor that calls the base class, and the argument list passed to the base class constructor is typically contained in the parameter list of the derived class constructor.

In the above examples of the program to remove the sealed keyword, is a class of inherited program code example.

Polymorphism of the class

In the inheritance of classes, C # allows a method with the same name to be declared in a base class and a derived class, and a method of the same name can have different code, that is, there can be different implementations in the same functionality of the base class and the derived class, providing multiple ways to solve the same problem.

Polymorphism means that while a program is running, the execution of a statement that invokes a method can be done differently depending on the type of the derived class object.

There are many ways to achieve polymorphism in C #.

(1) virtual method

In a derived class, declare a method with the same name as the base class with the New keyword in the following format:

Public new method Name (parameter list)

{   }

The declaration format in the base class is as follows:

Public virtual method Name (parameter list)

The declared format in a derived class is:

public override method Name (parameter list)

Declaring a method with the same name as the base class in a derived class is also called a method overload. After a derived class overloads a base class method, you can use the base keyword if you want to call a method of the same name as the base class. If both the base class and the derived class have a parameterless show method.

The declaration format of the show method in the base class: Show ()

The declaration format of the show method in a derived class: Base show ()

(2) abstract classes and abstract methods

In order to achieve polymorphism, virtual methods must be declared in the base class, but sometimes the virtual methods declared in the base class cannot implement the specific functionality. The abstract method needs to be declared in the base class. An abstract class is a method that declares in the definition of a base class that does not include any implementation code, and is actually a method that does not have any specific functionality. The only effect of such a method is to have the derived class rewrite it.

In a base class definition, as long as the class body contains an abstract method, the class is called an abstract class, and a generic virtual method can be declared in an abstract class.

1 declaring abstract classes and abstract methods

Declaring abstract classes and abstract methods requires the use of the keyword abstract, in the following format:

Public abstract class name

{

Public abstract return type method name (parameter list)

}

Abstract method is not a general empty method, abstract method declaration, there is no method body, only after the method followed by a semicolon.

2 Overloaded abstract methods

When you define a derived class of an abstract class, derived classes naturally inherit abstract method members from the abstract class and must override the abstract method of the abstract class, which is different from the virtual method because the derived class can not have to be overridden for the virtual method of the base class. Overriding the abstract class method must use the Override keyword.

The format of the overloaded abstract method is:

public override return type method name (parameter list)

{  }

Where the method name and argument list must be exactly the same as the abstract method in the abstract class.

For this aspect of the procedure examples in the subsequent specific examples to do a full analysis.

OOP features such as C # supplements

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.