Comprehensible oop (i): Polymorphism and Inheritance (early bound/compile-time polymorphism)

Source: Internet
Author: User
Tags modifiers

In this series, we use the CodeProject on the more fire of the OOP series blog mainly for OOP in a very easy to show.

Whether as a master of software design, or rookie, for the architecture design, need to re-construct, trade-offs, in order to facilitate the health of the entire software project construction, some experience is summarized by predecessors, we can use it, some of the team knowledge precipitated, in short, the reuse of good ideas to reduce rework. Of course, in the interview, if you can talk about OOP, it will naturally add a lot of points.

Start reading this series of blog prerequisites, polymorphism, encapsulation, object-oriented programming, and more, and learn from MSDN. As a term, you should be familiar with it. This series of articles uses C # speech as a unique scripting language.

Oop1 What is OOP and what is the advantage of OOP?

OOP represents object-oriented programming (Object-oriented programming), which is programmed based on the whole of the object and replaces the programming idea based on the process function. The implementation is based around the object for data, function encapsulation, rather than a logical relationship. Objects in OOP are directly connected to a particular type, to an instance object of a certain type, and more often to a class. The structure of each class object is basically similar, but has its own unique properties and data values. objects can be accessed through external interfaces: methods, properties, and so on. Based on these advantages of OOP, independent objects can be modified without affecting other objects, which makes it easier to upgrade software to reduce potential bugs. The software system becomes bigger and larger over time, and the OOP programming idea effectively improves the readability and management of the system code.

2 What is the concept of OOP?

Here are 5 terms to illustrate the specific concept of OOP:

    • Data Abstraction: Data abstraction is the starting point for modeling objects that need to be manipulated, both abstracting the use of objects and hiding internal details (for the end user to use). Users can easily use class methods, data, and not care about the complex process behind data creation and running logic. We take the real world as an example, when you ride a bike, do not consider how the principle of variable speed gear to drive the chain, wheel bar.

    • Inheritance (inheritance): Inheritance is one of the most popular concepts in the OOP concept. Inheritance provides the programmer with the advantage of reusable code. The base class defines the function logic, which can be accessed directly by inheriting the subclass--just as easily as a subclass's own method.

    • Data Encapsulation: A member variable of class, a member function is wrapped by an access control character, is called a data encapsulation. There are 4 types of access controls, public, Protected, Private, Internal.

    • Polymorphic (polymorphism): objects can implement the same action by passing different parameters, which we call polymorphic. We take the real world as an example, "drive" This method, for different types of users to provide different parameters to achieve polymorphism, such as car.drive (man), Car.drive (Woman) and so on.

    • Message Communication: Message communication means the invocation, execution of a class function through a message.

3 Polymorphism (polymorphism)

In this section, we use code snippets to illustrate the polymorphic types of the respective types: function overloading, early binding, and compiler polymorphism.

First create a console project and name itInheritanceAndPolymorphism,然后添加类Overload.cs,再添加DisplayOverload函数。

Displayoverload (+ displayoverload (+ displayoverload (A, + A +

Program.cs添加如下代码:

Main (=,

Run the program with the following results:

displayoverload
Displayoverload Method Overloading
Displayoverload method overloading100

Overload类中的DisplayOverload提供了3类不同的重载函数:方法名相同,参数类型和个数不同。C#中的这种方式成为重载,既我们不需要为每类函数定义不同名字的函数,仅需要改变函数参数类型和个数即可实现,这个也成为函数签名。

用不同的返回值可以否? 我们试试下面的代码:

Displayoverload () {}

The positive result is that Visual studio will give you the following error message:

Error:type ' Inheritanceandpolymorphism.overload ' already defines a member called ' Displayoverload ' with the same paramete R types

From the above results, the return value is not signed as a polymorphic function.

Let's run the following code again:

Displayoverload (Displayoverload (Displayoverload (a) {}

The result is still an error:

Error:type ' Inheritanceandpolymorphism.overload ' already defines a member called ' Displayoverload ' with the same paramete R types

Conclusion: The visible function modifier of static is not used as an overloaded signature.

Run the following code and try out, ref can be used as an overloaded signature.

Displayoverload (Displayoverload (= Displayoverload (a) {}

The result is the following error:

Error:cannot define overloaded method ' displayoverload ' because it differs from another method only in ref and out

Conclusion: REF and out pass parameter modifiers also cannot be overloaded signatures.

4 Effects of params parameters in polymorphic

A function can contain the following 4 types of parameter passing:

    • Value Pass (pass by value)

    • Reference Pass (pass by reference)

    • As output parameter (as an output parameter)

    • Using a parameter array (using parameter arrays)

We run the following code:

Displayoverload (A, Display (

No accident, get the following error message:

Error1:the parameter name ' A ' is a duplicate

ERROR2:A local variable named ' A ' cannot be declared in this scope because it would give A different meaning to ' A ', whic H is already used in a ' parent or current ' scope to denote something else

In the same scope, the parameter name must be unique.

In the Overload.cs file, add the following code:

Name = name, Display2 (x, = = =
Add the following code to the Program.cs:
Main (=
The results of the operation are as follows:
Akhil
Akhil 1
Akhil 2
Akhil3

Conclusion: We pass the memory address of name through ref reference, so modifying the value of x and Y is equivalent to modifying the value of name directly, so the result runs as above.

The following code demonstrates the role of the params keyword:

Add the following code to the Overload.cs file:

,,,, Displayoverload (A, str + +

Add the following code to the Program.cs file:

Main (=
The results of the operation are as follows:
Akhil 100
Mittal
OOP
Akhil

C # provides the params dynamic parameter array mechanism, which is very convenient to dynamically pass different quantities of the same type parameters at run time.

Note: The params keyword can only be applied as the last parameter of the function.

Let's try again the function signature of the params keyword and the order of precedence of the non-params keyword function signature:

,,,, Displayoverload (x, + x + + displayoverload (

Add the following code to the Program.cs file:

Main (=

The results of the operation are as follows:

Parameterarray
The integers
Parameterarray

From the running results, C # is very ingenious to perform the exact matching of non-params functions first, such as 1 int type \ 3 int type, then match with params type, 2 int type, match with well-defined function.

5 Conclusion

In this section, we take the first article in the OOP series, which mainly describes the polymorphism of the compiler, which is also known as early binding or method overloading. At the same time, we also learn the powerful params keyword in C # and use it to implement polymorphism.

The main points of this article are summarized as follows:

    • The signature rules for C # function overloading are judged by the type and number of arguments, not by the name of the function.

    • The function return value is not signed as an overload.

    • Modifiers are not part of a signature, such as static

    • In the same function, multiple parameter names are unique

    • Ref, Out is a reference pass, the memory address of the parameter is passed

    • Params as a parameter key, can only be used for the last parameter of a function

Original address: Http://www.codeproject.com/Articles/771455/Diving-in-OOP-Day-Polymorphism-and-Inheritance-Ear

Comprehensible oop (i): Polymorphism and Inheritance (early bound/compile-time polymorphism)

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.