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 # as a unique scripting language.
650) this.width=650; "Width=" 265 "height=" 207 "title=" image "style=" border-width:0px;padding-top:0px;padding-right : 0px;padding-left:0px;background-image:none; "alt=" image "src=" http://image.mamicode.com/info/201504/ 20181021212149310814.png "border=" 0 "/>
What is OOP1 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.
650) this.width=650; "width=" 251 "height=" 168 "title=" image "style=" border-width:0px;padding-top:0px;padding-right : 0px;padding-left:0px;background-image:none; "alt=" image "src=" http://image.mamicode.com/info/201504/ 20181021212149353784.png "border=" 0 "/>
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.
Create a console project first, name it inheritanceandpolymorphism, and then add the class Overload.cs , and then add the displayoverload
function.
public class overload { Public void displayoverload (Int a) { system.console.writeline ("displayoverload " + a); } public void displayoverload ( String a) { System.Console.WriteLine ("displayoverload " + a); } public void displayoverload (String a,  INT B) { System.Console.WriteLine ("displayoverload " + a + b); } }
在Program.cs添加如下代码:
Class Program {static void Main (string[] args) {overload overload = new Overload (); Overload. Displayoverload (100); Overload. Displayoverload ("Method overloading"); Overload. Displayoverload ("Method overloading", 100); Console.readkey ();
Run the program with the following results:
Displayoverload 100
Displayoverload Method Overloading
Displayoverload method overloading100
Overload类中的DisplayOverload提供了3类不同的重载函数:方法名相同,参数类型和个数不同。C#中的这种方式成为重载,既我们不需要为每类函数定义不同名字的函数,仅需要改变函数参数类型和个数即可实现,这个也成为函数签名。
用不同的返回值可以否? 我们试试下面的代码:
public void Displayoverload () {}public int 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:
static void Displayoverload (int a) {}public void displayoverload (int a) {}public void Displayoverload (String 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.
private void Displayoverload (int a) {}private void displayoverload (out int a) {a = 100; }private void displayoverload (ref int 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:
public void Displayoverload (int A, string a) {} public void Display (int a) {string A; }
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:
public class overload { private string name = "Akhil"; public Void display () { display2 (Ref name, ref name); system.console.writeline (name); } private void display2 (Ref string x, ref string y) { system.console.writeline (name); x = "Akhil 1"; system.console.writeline (name); y = "Akhil 2"; System.Console.WriteLine (name); name = "akhil 3"; } }
Add the following code to the Program.cs:
Class Program {static void Main (string[] args) {overload overload = new Overload (); Overload. Display (); Console.readkey (); } }
The results of the operation are as follows:
Akhil
Akhil 1
Akhil 2
Akhil3
650) this.width=650; "Width=" 118 "height=" 119 "title=" image "style=" border-width:0px;padding-top:0px;padding-right : 0px;padding-left:0px;background-image:none; "alt=" image "src=" http://image.mamicode.com/info/201504/ 20181021212149408474.png "border=" 0 "/>
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:
Public class overload { public void display () { displayoverload (100, "Akhil", "Mittal", "OOP"); displayoverload (200, "Akhil"); displayoverload (; ) } private void Displayoverload (Int a, params string[] parameterarray) { foreach (String str in parameterarray) console.writeline (Str + " " + a); } }
Add the following code to the Program.cs file:
Class Program {static void Main (string[] args) {overload overload = new Overload (); Overload. Display (); Console.readkey (); } }
The results of the operation are as follows:
Akhil
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:
Public class overload { public void display () { displayoverload (; ) displayoverload (200, 300); displayoverload (200, 300, 500, 600); } private void displayoverload (int x , int y) { console.writeline ("the two integers " + x + " " + y); } private Void displayoverload (Params int[] parameterarray) { console.writeline (" Parameterarray "); } }
Add the following code to the Program.cs file:
Class Program {static void Main (string[] args) {overload overload = new Overload (); Overload. Display (); Console.readkey (); } }
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
650) this.width=650; "width=" 181 "height=" 215 "title=" image "style=" border-width:0px;padding-top:0px;padding-right : 0px;padding-left:0px;background-image:none; "alt=" image "src=" http://image.mamicode.com/info/201504/ 20181021212149446561.png "border=" 0 "/>
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
This article is from the "Grape City Control Technology Team Blog" blog, be sure to keep this source http://powertoolsteam.blog.51cto.com/2369428/1627540
Comprehensible oop (i): Polymorphism and Inheritance (early bound/compile-time polymorphism)