Abstract: Although there are many friends who are very familiar with the concept of object-oriented, what is object-oriented programming? What are the characteristics of object-oriented? What convenience can object-oriented programming bring us? What are the shortcomings of object-oriented?

This article is shared from the Huawei Cloud Community " [Cloud-based co-creation] Is object-oriented programming that has been touted for many years really so perfect? ", author: Glacier.

Speaking of object-oriented, I believe that many small partners are already familiar with it. After all, programming languages like Java and C++ have long been in the forefront of the programming language list, and almost every university’s software engineering and computer majors will open Java and C++ related courses. When in school, the teacher will give us what the object is and what kind. For example: In Java, everything is an object.

Speaking of this, although many friends are already familiar with the concept of object-oriented, what is object-oriented programming? What are the characteristics of object-oriented? What convenience can object-oriented programming bring us? What are the shortcomings of object-oriented?

Few people have delved into these issues. I believe that many of our friends are half-knowledgeable. Today, let’s talk about the topic of object-oriented.

What is object-oriented programming?

First, let's talk about what object-oriented programming is. Object-oriented programming in English is Object Oriented Programming, or OOP for short. There are two most important and basic concepts in object-oriented: class and object.

These two concepts first appeared in the Simula programming language. The real first use of the concept of object-oriented programming was in the Smalltalk language. The Smalltalk language is also recognized as the world's first true object-oriented programming language.

Today, there are more and more object-oriented programming languages, such as C++, Java, Go, Python, C#, Object-C, Scala, etc. are all object-oriented programming languages.

In fact, there are essential differences between object-oriented programming and object-oriented programming languages.

object-oriented programming: object-oriented programming refers more to a programming constraint, with the two concepts of class and object as the carrier of output, and provides encapsulation, inheritance, abstraction, and polymorphism .

object-oriented programming language: object-oriented programming language is essentially a tool for outputting and expressing object-oriented. It can convert the programmer's thoughts into binary codes that the computer can recognize, and finally can run on the computer.

What are the characteristics of object-oriented?

Earlier we have briefly mentioned the four major characteristics of object-oriented: encapsulation, inheritance, abstraction and polymorphism.

Encapsulation

Encapsulation, as the name implies, is to protect the data to be accessed, and prevent the outside world from directly accessing the attributes and fields of the class, but to provide a limited access interface to the outside, and to authorize the outside to access only through the interface provided by the class. For example, we take the Java language as an example to write a User class that only contains the user name userName and the user age userSex.

public class User{
    private String userName;
    private Integer userSex;
 
    public void setUserName(String userName){
        this.userName = userName;
    }
 
    public String getUserName(){
        return this.userName;
    }
 
    public void setUserSex(Integer userSex){
        this.userSex = userSex;
    }
 
    public Integer getUserSex(){
        return this.userSex;
    }
}

In this User class, there are a total of two attributes, also called class member variables, userName and userSex. userName represents the name of the user, and userSex represents the name of the user.

We cannot directly access the userName and userSex attributes in the User class because they are all modified by the privaite modifier. But we can access two attributes through the following four methods.

• void setUserName(String userName)
• String getUserName()
• setUserSex(Integer userSex)
• Integer getUserSex()

can improve the ease of use of the class and reduce the risk of code errors during the programming process.

inherit

The most direct manifestation of inheritance in a programming language is the relationship between parent and child classes. In Java, the extends keyword is used to implement inheritance, and the colon (:) is used in C++ to indicate the inheritance relationship.

For example, we take the Java language as an example to create a Student class and inherit the User class, as shown below.

public class Student extends User{
    private String studentNo;
 
    public void setStudentNo(String studentNo){
        this.studentNo = studentNo;
    }
 
    public String getStudentNo{
        return this.studentNo;
    }
}

At this point, the Student class object can access the properties of the User class object through the public methods provided by the User class object.

biggest advantage of 1615d113ab75f0 inheritance is the ability to reuse code.

abstract

Abstraction mainly refers to the specific implementation of hidden methods, so that the caller of the method does not need to care about the specific implementation of the method, but only needs to care about what functions the class provides.

For example, in the Java programming language, the most intuitive realization of abstraction is the interface and the abstract class . The interface is decorated with the interface keyword, and the abstract class is decorated with the abstract keyword.

For example, taking the Java language as an example, the business logic interface StudentService of the Student class is as follows.

public interface StudentService{
    public abstract Student getStudentByNO(String studentNo);
}

The AbstractStudent abstract class that implements the StudentService interface.

public abstract class AbstractStudent{
 
    @Override
    public Student getStudentByNO(String studentNo){
        //省略无数代码
        return student;
    } 
 
    public abstract void visitStudent(String studentNo);
}

abstraction is more able to separate the design and implementation of the program.

Polymorphism

Polymorphism means that the subclass can replace the parent class. When the code of the program is actually run, the method of the subclass is called. For example, the User class and Student class below.

User class:

public class User{
    private String userName;
    private Integer userSex;
 
    public void setUserName(String userName){
        System.out.println("调用User类的setUserName()方法...");
        this.userName = userName;
    }
 
    public String getUserName(){
        return this.userName;
    }
 
    public void setUserSex(Integer userSex){
        this.userSex = userSex;
    }
 
    public Integer getUserSex(){
        return this.userSex;
    }
}

Student class:

public class Student extends User{
    private String studentNo;
 
    public void setUserName(String userName){
        System.out.println("调用Student类的setUserName()方法...");
        this.userName = userName;
    }
 
    public void setStudentNo(String studentNo){
        this.studentNo = studentNo;
    }
 
    public String getStudentNo{
        return this.studentNo;
    }
}

We run the following code.

public static void main(String[] args){
    User user = new Student();
    user.setUserName("冰河");
}

At this time, the following statement will be output on the command line:

calls the setUserName() method of the Student class...
biggest advantage of 1615d113ab780b polymorphism is to improve the scalability of the program.

What are the benefits of object-oriented programming?

Object-oriented programming has many advantages, such as the following advantages introduced earlier in the article:

• The encapsulation feature can improve the ease of use of the class and reduce the risk of code errors during the programming process.
• The biggest advantage of inheritance is the ability to reuse code.
• Abstraction is more about separating the design and implementation of the program.
• The biggest benefit of polymorphism is to improve the scalability of the program.

In addition, object-oriented programming also has obvious advantages in the field of concurrency. In the field of concurrent programming, we can use object-oriented encapsulation features to encapsulate shared variables into a class, and this shared variable is decorated with private, which means that this shared variable cannot be directly accessed by the outside world. It can only be accessed through a limited number of methods provided by this class, and these methods are all locked to ensure thread safety. This avoids the thread safety problem caused by the shared variable of multiple threads at the same time.

What are the shortcomings of object-oriented programming?

Although object-oriented programming has many benefits, it also has disadvantages.

• Everything is an object, creating a large number of objects will take up a lot of memory space.
• A certain software support environment is required.
• Not suitable for the development of some low-level computer applications, such as chip development, microcontroller development, etc.
• Not suitable for the development of some large-scale MIS systems.
• Performance is not as high as process-oriented programming.
• Not suitable for the development of a command transmission system with particularly high real-time requirements.

Click to follow and learn about Huawei Cloud's fresh technology for the first time~


华为云开发者联盟
1.4k 声望1.8k 粉丝

生于云,长于云,让开发者成为决定性力量