Object Oriented Programming OOP

This section talks about what is Object Oriented Programming. Before talking about object-oriented, what we have to mention is Process Oriented Programming. C language is a process-oriented language. What is the difference between the two? We can imagine a scenario-cooking in the kitchen:

To explain in a process-oriented form, the first step: prepare materials, the second step: start a fire, the third step: stir-fry, the fourth step: serve; process-oriented is to write a function, each function performs a part of the operation, Finally, according to this set of functions, the purpose is to implement an overall requirement.

For object-oriented, we explained cooking in the same way, the first step: there must be a chef, a stove, and a waiter, the second step: the chef prepares the materials, the third step: the stove is on fire, the fourth step: the chef Stir-frying, the fifth step: the waiter serves the dishes. The object-oriented programming method organizes the original independent functions with the objects they belong to and encapsulates them into methods (the "function" in object-oriented has a new method called Method). Although the amount of code will actually increase, this kind of programming thinking is reasonable, realistic, and easier to understand, because the responsibilities of each object are clear, so later maintenance will become more convenient.

Below at the code level, demonstrate the difference between object-oriented and process-oriented:

Proceduralization:

#include "stdio.h"
​
void Prepare(){
  printf("准备食材。\n");
}
void Fire(){
  printf("起火\n");
}
void Cooking(){
  printf("炒菜,\n");
  printf("炒完了\n");
}
void Serve(){
  printf("请享用。");
}
​
main(){
  Prepare();
  Fire();
  Cooking();
  Serve(); 
}

Object Oriented Programming OOP

Objectification:

//创建三个对象
//厨师
class Cook
{
    //准备食材的方法
    public void Prepare()
    {
        Console.WriteLine("厨师准备食材。");
    }
    //做饭的方法
    public void Cooking()
    {
        Console.WriteLine("厨师正在做饭...");
        Console.WriteLine("厨师做好了。");
    }
}
//灶台工具类
static class CookingBench
{
    //静态工具方法:起火
    public static void Fire()
    {
        Console.WriteLine("灶台生火。");
    }
}
//服务员
class Waiter
{
    //上菜方法
    public void Serve()
    {
        Console.WriteLine("请享用。");
    }
}

Called in the main method:

Cook cook=new Cook();
Waiter waiter=new Waiter();
​
cook.Prepare();
CookingBench.Fire();
cook.Cooking();
waiter.Serve();

Object Oriented Programming OOP

Object-oriented has three characteristics: encapsulation, inheritance, and polymorphism. Let's talk about it in detail:

Package:

Everyone has their own secrets. The same is true in object-oriented code. Among the objects, there are objects that can be viewed by the outside world, and some are not viewed by the outside world. This idea of ​​hiding some members is encapsulation. To achieve encapsulation, you need to first Learn about the four access modifiers: public, private, protect, internal

Access modifiers can modify classes, properties, and methods. Use modifiers to modify classes or properties and methods, with different access levels. The access modifier should be written first when declaring:

public class publicClass{}//声明一个类
private bool isPublic;//声明一个属性

public: public, this access level is the lowest.

private: private, as the name implies, this access level is the highest and can only be accessed within the scope of the declaration.
protect: Protected and can only be accessed on the inheritance chain. To put it bluntly, only a class inherited can access the protected members of this class.

internal: internal, only accessible in the same assembly. It can be interpreted narrowly as being accessible under the same namespace.

There is also a combination punch: protect internal, which is to meet the same assembly, but also have to inherit the relationship to access.

Through these keywords, we can achieve encapsulation. When developing, you only need to specify what kind of access permissions are assigned to the classes or properties, methods, etc. that you write.

inherit:

The concept of inheritance is also easy to understand. It is like in real life, a child inherits the parent’s property, then the parent’s things become the child’s. In C#, inheritance between classes is achieved through ":" of.

public class Father{}
public class Chlid:Father{}//Child类继承了Father

Note that C# is a single inheritance language, which means that a class can only inherit one parent class.

Subclasses can inherit non-private properties or methods in the parent class. If private properties or methods can be accessed, there will be no protect keyword. Through inheritance, we can extract the repetitive code shared by the subclasses into the parent class, so that all subclasses do not need to declare these members, which reduces a lot of code. In the inheritance structure of C#, the object class is the parent class of all classes, and any class inherits object by default. The object class provides us with some of the most basic members of the class, such as our commonly used tostring() method.

There is a principle in object-oriented called the open-close principle. This principle stipulates that it is closed to modification and open to extension. That is to say, after writing a class and using it for a period of time, we need to modify this class because of project upgrades or other reasons. (Add some new things) At this time, according to the principle of opening and closing, we cannot modify it directly. Instead, we have to write another class to inherit it and add new business logic to the subclass. This is also a purpose of inheritance.

In inheritance, there is another concept called method overriding, that is, a method in the subclass has the same name as the method of the inherited parent class, so that the subclass method overwrites the parent class method. This process is rewriting. This concept will be expanded in detail in the section that specifically introduces classes and methods.

Polymorphism:

Polymorphism depends on inheritance, only inheritance can achieve polymorphism. The same type, with different forms is polymorphism. For example, dogs have different forms: huskies, pastoral dogs, corgis, etc. The manifestation in the code is that the parent class can receive the subclass to assign values ​​to it. Take the above example again, the following code is an example of polymorphism:

Father f=new Chlid();

The basis of polymorphism is the Richter's transformation principle: the child class inherits the parent class, then, the scene that originally applied to the parent class must apply to the child class, because the child class inherits all the explicit functions of the parent class, and the parent class can do Yes, subclasses can also do it. This principle is to define the existence of this theory. Subclasses can directly replace the parent class and convert all parent classes to subclasses. There is no difference in program behavior.

Polymorphism is also a very important cornerstone of object-oriented programming. We generally use interfaces as much as possible in programming, oriented to abstraction, and reduce coupling. Because of polymorphism, we can implement instance operations through interfaces or some abstract data structures. .

Finally, an example is used to demonstrate polymorphism (some knowledge related to classes and methods will be explained in detail in the next section of classes and methods):

     public class Dog
    {
    public string name { get; set; }

    public Dog(string name)
    {
        this.name = name;
    }

         public void introduce()
    {
        Console.WriteLine("这是一只:" + name);
    }
    }
    ​
    public class Husky : Dog
    {
    //调用父类的构造方法,为name赋值
    public Husky():base("Husky"){}
    }
    ​
    public class Koji : Dog
    {
      public Koji() : base("Koji"){}
    }
    ​
    class DogStore
    {
    public Dog dog { get; set; }
    ​
    public DogStore(Dog dog)
    {
        this.dog = dog;
    }
    ​
    public void wantBuy()
    {
        Console.WriteLine("Do u want this "+dog.name+"?");
    }
    }

There is a common Dog class in the above code, and there are two Husky classes respectively, which Corgi inherited. There is also a pet dog shop, which requires the Dog attribute.

Let's take a look at the code in the main method:

DogStore dogStore=new DogStore(new Husky());
dogStore.wantBuy();
dogStore=new DogStore(new Koji());
dogStore.wantBuy();

Object Oriented Programming OOP
We receive more specific subclasses through the parent class. This is a very good embodiment of polymorphism, which is also a very elegant and efficient programming method.

 Personal public account, love to share, knowledge is priceless.

Object Oriented Programming OOP

Guess you like

Origin blog.51cto.com/14960461/2542693