In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
In this series, we mainly focus on the OOP series of blogs which are popular on CodeProject, and present OOP in a profound and simple way.
No matter as a master of software design or as a rookie, architecture design needs to be refactored and chosen many times in order to facilitate the healthy construction of the whole software project. Some experiences are summed up by our predecessors and we can use them. Some of them are precipitated by team knowledge. In short, reusing the good ideas of our predecessors is conducive to reducing rework. Of course, in the interview, if you can talk about OOP, it will naturally add a lot of points.
Start reading the preliminary knowledge of this series of blogs, polymorphism, encapsulation, object-oriented programming, etc., please learn through MSDN. The terms in the following figure should be familiar to you. This series of articles uses C # as the only scripting language.
What is OOP1, OOP, and what are the advantages of OOP?
OOP stands for object-oriented programming (Object-Oriented Programming), which is based on the object as a whole and replaces the programming idea based on process function. The specific implementation is to encapsulate data and functions around objects, rather than based on logical relations. Objects in OOP go directly to a specific type, or an instance object of a certain type, and more often a class. Each class object is structurally 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 and reduce potential bug. With the passage of time, the software system will become larger and larger, and the OOP programming idea can effectively improve the readability and management of the system code.
2 what is the concept of OOP?
Here are five terms to describe the specific concept of OOP:
Data abstraction (Data Abstraction): data abstraction is the starting point for modeling objects that need to be manipulated. It abstracts the objects used and hides the internal details (for the end users). Users can easily use class methods and data without worrying about the complex process behind the data creation and running logic. Let's take the real world as an example, when you ride a bike, you don't have to think about how the transmission gear drives the chain and the wheels.
Inheritance: inheritance is the most popular concept in the OOP concept. Inheritance gives programmers the advantage of reusable code. The base class defines the function logic, and the subclass can be accessed directly through inheritance-just as convenient as the methods of the subclass itself.
Data encapsulation (Data Encapsulation): wrapping member variables and functions of class through access control characters is called data encapsulation. There are four types of access controllers: public, Protected, Private and Internal.
Polymorphism: objects can achieve the same action by passing different parameters, which is called polymorphism. We take the real world as an example, "driving" 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): message communication means that class functions are called and executed through messages.
3 polymorphisms (Polymorphism)
In this section, we use code snippets to explain the polymorphic types of their respective types: function overloading, early binding, and compiler polymorphism.
First create a console project and name it InheritanceAndPolymorphism, 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);}}
Add the following code to 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 and the result is as follows:
DisplayOverload 100
DisplayOverload method overloading
DisplayOverload method overloading100
The DisplayOverload in the Overload class provides three different types of overloaded functions: the same method name, different parameter types and numbers. This way in C # becomes overloading, since we do not need to define functions with different names for each type of function, we only need to change the type and number of function parameters, which also becomes a function signature.
Is it okay to use different return values? Let's try the following code:
Public void DisplayOverload () {} public int DisplayOverload () {}
The positive result is that Visual Studio will give the following error message:
Error: Type 'InheritanceAndPolymorphism.Overload' already defines a member called' DisplayOverload' with the same parameter types
From the above results, we can see that the returned 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 parameter types
Conclusion: the visible function modifier of static is not used as an overloaded signature.
Run the following code to see if out and ref can be used as overload signatures.
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 on ref and out
Conclusion: the passing parameter modifiers of ref and out can not be used as overloaded signatures.
The role of Params parameters in 4 polymorphisms
A function can contain the following four types of parameter passes:
Value passing (pass by value)
Reference passing (Pass by reference)
As an output parameter (As an output parameter)
Use 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', which is already used in a' parent or current' scope to denote something else
Parameter names must be unique in the same scope.
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 Program.cs: class Program {static void Main (string [] args) {Overload overload = new Overload (); overload.Display (); Console.ReadKey () }} the running result is as follows: Akhil
Akhil 1
Akhil 2
Akhil3
Conclusion: we pass the memory address of name through ref reference, so changing the value of x and y is equivalent to directly modifying the value of name, so the result runs as above.
The following code demonstrates the function 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 (300) } private void DisplayOverload (int a, params string [] parameterArray) {foreach (string strin 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 result is as follows: Akhil 100
Mittal 100
OOP 100
Akhil 200
C # provides params dynamic parameter array mechanism, which is very convenient to dynamically transfer different numbers of parameters of the same type at run time.
Note: the params keyword can only be used as the last parameter of a function.
Let's try the priority order of params keyword function signature and non-params keyword function signature:
Public class Overload {public void Display () {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 running results are as follows:
ParameterArray
The two integers 200 300
ParameterArray
From the running results, C# skillfully gives priority to precise matching of non-params functions, such as 1 int type\ 3 int types, params type matching and 2 int types matching with clearly defined functions.
5 conclusion
In this section, we do the first article in the OOP series, which focuses on the polymorphism of the compiler, 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 paper are summarized as follows:
The signature rule of C # function overloading is judged by the type and number of parameters, not the name of the function.
The value returned by the function is not used as an overloaded signature.
Modifiers are not part of the signature, such as static
In the same function, multiple parameter names should be unique.
Ref and out are passed by reference, passing the memory address of the parameter
Params, as a parameter keyword, 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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.