Object-oriented Program Design (Object-Oriented Programming, short as OOP) is a very powerful Programming method designed to create software to reuse code and think about Programming issues based on classes. Class is the core component of OOP, usually Use Class ). In the classic book "code Daquan", we define: "The first step is probably the most important step to create high-quality classes. It is to create a good interface. This also includes creating a reasonable abstraction that can be presented through interfaces, and ensuring that the details are still hidden behind the abstraction .", In fact, the interface is also a class, a more abstract class.
In my previous article, I mentioned: "The more classes, the better. The classification of classes is for encapsulation, but the foundation of classification is abstraction, an abstract set of objects with the same attributes and functions is a class. ---- Select "big talk design mode".
It can be seen how important a class is in OOP. This article will focus on class as a discussion point and analyze and introduce various knowledge points of the class in all aspects.
1. Objects in daily life
In most people's eyes, objects are abstract nouns, while classes are more abstract. In daily life, objects can be understood as an entity for identification. Such as automobiles, ships, airplanes, Computer . If you associate an object with a program, you can think of it as an entity represented in the program.
Only by starting from reality can the compiled code better reflect the real life. For example, a computer includes components such as the motherboard, hard disk, video card, and memory. However, the actual programming is not so simple, but we still have to analyze it from the reality. Taking program design into consideration in this way will bring many benefits: Easier program design, more intuitive and easier to understand the program architecture, it clearly reflects the abstract meaning of the program from the real life, and makes it easier for multiple developers to work together, because they can process different objects in the code, you only need to know what other people's objects do, and do not need to worry about the work details of other people's code.
2. Objects in programming
What objects have we used in programming? I think there are two main categories: System Objects and user-defined objects. Okay. Let's start with "Hello World" to better illustrate this. Take a look at the following code definition:
Console.WriteLine("Hello Wrold");
The above code is too familiar for you who are reading this article. its function is to output the "Hello World" string on the console. however, in addition to this, this code also implies other related knowledge. so what is it? In fact, when we compile this code Use The. Console is an object, a system object provided by. NET Framework. Method The output is implemented because the system object method is called. The Console is a typical system object.
Next, let's look at the custom objects. First, let's analyze the objects in real life. Computer A computer may consist of a motherboard, hard disk, video card, memory, and many other elements. through abstraction, we can think that the computer is an object, and the components of the motherboard and video card are the attributes of the computer object. the following is the definition of computer:
1/** // <summary>
2 // computer
3 /// </summary>
4 public class Computer
5 {
6 private string name;
7 public string Name
8 {
9 get {return name ;}
10 set {name = value ;}
11}
12
13 public string motherboard = "elite motherboard ";
14 public string hard drive = "Seagate hard drive ";
15 public string memory = "Kingston ";
16 public string graphics card = "colorful red ";
17 // other components
18
19 public Computer ()
20 {}
21
22 public Computer (string name)
23 {
24 this. Name = name;
25}
26}
What is the above Code? We have defined a Computer class, which defines several members and structures. Method Etc. For a clearer description of this purpose, we can first perform a test, create two objects, and then determine whether the attribute of the other door is the same. The Code is as follows:
1 class Program
2 {
3 static void Main (string [] args)
4 {
5 Computer zhangsan = new Computer ("James ");
6 Computer lisi = new Computer ("Li Si ");
7
8 Console. WriteLine (zhangsan. motherboard = lisi. motherboard );
9}
10}
By running the above program segment, the result is: true. What does this mean? Does it mean that Michael's motherboard and Lee's motherboard are the same? No, we should say that both Zhang and Li are using the same class object. What is the Computer class doing here? Encapsulate attributes and behaviors? The easiest way to understand is to regard this class as a new data type. that is to say, Computer is a data type, while zhangsan and lisi are the new data type variables. in OOP, zhangsan and lisi are the instances or objects of the Computer object.
Iii. class-related features
The class has many features. Here I will briefly introduce several of the most commonly used features.
1. Class Name
The class name is the only feature used to distinguish other classes, such as the same name. Of course, in the same Project In an assembly or framework, classes with the same name will appear, just as the world is large, and people with the same name are everywhere. In real life, the same celebrity can be distinguished based on his country, province, district, country, and family. For classes with the same name, they are distinguished by namespaces.
Above Computer Class Computer, which is the class name of the Computer class.
2. Attributes
Attribute is an important part of a class. For more information, see Computer. Name, motherboard, and video card are Computer attributes. We can access them through class objects (instance objects). The schematic code is as follows:
1 class Computer
2 {
3 public string Name = "zhangsan ";
4}
5 class TestComputer
6 {
7 void Method ()
8 {
9 Computer computer = new Computer ();
10 Console. WriteLine (computer. Name );
11}
12}
In actual development, we usually set the attribute to private by providing get; set; Attribute accessors for external calls. This is not detailed here.
3. Constructor
Structure Method It is one of the most commonly used features. Unlike common methods, constructor has no return value modifier. Every time you create an object, you actually call the constructor of the object. In actual development, we can reload the constructor multiple times and initialize the attribute value through the constructor. The schematic code is as follows:
1 class Computer
2 {
3 public Computer () {}// No parameter Constructor
4 public Computer (string name) // overload constructor with a parameter
5 {
6 this. Name = name;
7}
8
9 private string name;
10 public string Name
11 {
12 get {return name ;}
13 set {name = value ;}
14}
15}
16 class TestComputer
17 {
18 void Method ()
19 {
20 Computer computer = new Computer (); // call the construction method without Parameters
21 computer. Name = "James"; // set the attribute value through the property accessors
22 Console. WriteLine (computer. Name); // call the overloaded construction method with Parameters
23 computer = new Computer ("James"); // set the attribute value through the constructor
24 Console. WriteLine (computer. Name );
25}
26}
4. behavior ( Method )
This is easy to understand. The class behavior is the class Method. The program code in the above class constructor has a Method in the TestComputer class, we can call it a behavior or method of the TestComputer class.
5. Class Object (Instance)
It is easy to understand here. We have Use Has been used for multiple class objects. For example, in the Method of the TestComputer class, a Computer-type class object is created. With this object, we can easily access the public attributes and methods in the computer class, the sample code is as follows:
1 public void Method ()
2 {
3 // create a Computer-type object computer
4 Computer computer = new Computer ("James ");
5 string name = computer. Name; // call the computer class attribute Name through the Computer object
6}
6. class inheritance
Since C # is a single inheritance Language Therefore, only a single inheritance is supported in class inheritance. That is to say, a class can only have one parent class, but can inherit multiple interfaces.
1 class
2 {
3 public string Name {get; set ;};
4}
5 class B: A // class B inherits from class
6 {
7
8}
1 class
2 {
3 public string Name {get; set ;};
4}
5 interface Ia
6 {
7 void MethodA ();
8}
9 class B: A, Ia // class B inherits from class A and the Ia Interface
10 {
11 public void MethodA ()
12 {
13 //
14}
15}
1 class
2 {
3 public string Name {get; set ;};
4}
5 interface Ia
6 {
7 void MethodA ();
8}
9 interface Ib
10 {
11 void MethodB ();
12}
13 class B: A, Ia, Ib // class B inherits from class A and inherits the Ia and Ib interfaces.
14 {
15 public void MethodA ()
16 {
17 //
18}
19
20 public void MethodB ()
21 {
22 //..
23}
24}
7. Class Modification
I will not describe the class modification in detail here, but simply put forward several concepts. For more information, see related documents. Below are several common class modifiers:
1public class ClassName { }
2public static class ClassName { }
3public sealed class ClassName { }
4public partial class ClassName { }
This article introduces common class-related knowledge points. Of course, there are still many class-related knowledge points which have not been mentioned in this Article. For more details about the class, please refer to the relevant books and materials, length constraints, this article introduces this and I hope this article will be helpful to beginners. My personal abilities are limited, and errors will inevitably occur in this article. I hope you can kindly advise. Thank you.
Source: http://beniao.cnblogs.com/