preface

Object oriented is a very important concept for programmers, do not understand the theoretical knowledge of object oriented, when learning object oriented language will be confused

1. Object-oriented meaning

  • Origin:
  • Early programming was process-oriented, and the complexity of the problem gradually increased over time. This becomes increasingly complex for using process-oriented design
  • But face to object falls from the sky at this moment, brought a new solution to the problem, and gradually developed into the mainstream
  • Contrast:

In ancient times, when there were not many articles, it was ok to print them in a rigid way, but as there were more articles, it was difficult to re-print them each time. This is when movable type was introduced. The difference between object oriented and process oriented is something like this:

Object-oriented problem solving is more flexible and simple, the use of encapsulated technical methods can be a programming, multiple calls, and the use of attributes such a way, reduce the coupling, can be more simple to use, to understand

2. Object-oriented characteristics

Packaging:

Encapsulation is the packaging of a series of program logic into a class, only to display the attributes of the class, without paying attention to any of its internal structure, directly use the external interface can be:

Inheritance:

Inheritance is easy to understand. DNA from a father to a son is inheritance. In object-oriented programming, an inherited class is called a parent or base class, and an inherited class is called a subclass or derived class. Inheritance needs to follow certain rules:

In OOP (object-oriented programming), inheritance can be implemented using methods that follow the class name with a colon followed by the class being inherited

Inheritance rules:

  • C++ can implement multiple inheritance, while c# only supports single inheritance. To implement multiple inheritance, you need to implement it through the interface
  • Static classes cannot be inherited. Because static classes contain only static methods and static properties, they cannot be instantiated,

Polymorphism:

A Polymorphism literally means “many states.” In object-oriented languages, interfaces can be implemented in many different ways, called polymorphisms.

There are several ways to implement polymorphism:

  • Virtual functions
  • An abstract class
  • overloading
  • cover
  • Templates (overloaded and polymorphic independent)

3. Class characteristics

An important concept of object orientation is the class. A deeper understanding of the class can help you better learn and understand the concept of object orientation (this blog is based on C#).

3.1 Classification of classes

Static and instance classes:

Static classes use the modifier static. All property methods in a static class must be static members. Creating a static class can be interpreted as creating a directly instantiated class, but the details are different from instantiation

An instance class can be defined as a static member or an instance member without the static modifier

Using static modifiers to create properties or methods within a class is called a static member. A static member belongs to the class and, when called, is called directly by the class name. Static member call

About static member features:

  • When a class is accessed for the first time, all of its static members are created in memory
  • Static members are not on the stack or heap. Created in static storage
  • Static members are created only once, the first time the class is accessed
  • Static members are created in static storage and are not reclaimed until the end of the program

Instance members belong to objects that need to be instantiated and then accessed.

Use code to understand static and instance members and how they are accessed:

class Program
    {
        static void Main(string[] args)
        {
            Exam exam=new Exam{};
            Console.WriteLine(exam.i);   // Instance members need to be instantiated before being accessed

            Console.WriteLine(Exam.j);   // Static members are accessed directly by the class name}}public class Exam  / / instance
    {
        public int i=1;      
        public static int j=2;
    }
Copy the code

An abstract class:

Abstract classes are simply inherited as base classes, not instantiated

You can use abstract to modify abstract classes, which are different from static classes. Abstract classes can contain two types of abstract members: abstract method declarations and abstract properties. They can also contain other non-abstract members.

public abstract class Abst  / / abstract classes
    {
        public int i=1;
        public abstract void print(); // Define an abstract member
        
    }
    public class Demo:Abst  // Inherit the abstract class
    {
        public override void print()   // To override the abstract member, use the modifier override
        {
            Console.WriteLine("yes ok!"); }}class Program
    {
        static void Main(string[] args)
        {
            Demo demo=newDemo{}; Console.WriteLine(demo.i); demo.print(); }}Copy the code

To be continued ——–