Object-oriented Programming

发布时间 2023-04-30 10:51:12作者: Nocturne2282282

Object-oriented Programming

Source: What Is Object-Oriented Programming (OOP)? A Complete Guide

What is OOP

Object-oriented programming is a programming paradigm[1], or classification, that organizes a group of data attributes with functions or methods into a unit, known as an object.

第一句:面向对象编程是一种编程范式或分类。这句定义更像是描述了面向对象的封装特点。

面向对象编程把数据属性(大概是类中的私有数据成员)和方法(函数)打包成一个单元,即对象

Structure of OOP

  • Class: A class is a data type that provides a framework for creating objects.
  • Object: An object represents an instance, or creation, of a class.
  • **Method: ** A method is a function that performs a task or action.
  • **Attribute: **This structure stores information about an object and defines its state.

Read more: 12 Key Object-Oriented Programming Terms (With Definitions)[2]

OOP

4 principles of OOP

Four basic concepts: encapsulation, abstraction, inheritance and polymorphism.

Encapsulation

Encapsulation forms a barrier around data to protect it from the rest of the code. Binding the data and its functions into a class will conceals the private details of a class and only exposes the functionality essential for interfacing with it (API).

跟我一起念:通过将数据及其函数绑定到类中来执行封装,可以隐藏类的具体实现(私有)细节并只公开与之交互所必须的功能......

Related: 50 Object-Oriented Programming Interview Questions

Abstraction

Abstraction refers to using simplified classes, rather than complex implementation code, to access objects.

Abstraction helps isolate the impact of changes made to the code so if an error occurs, the change only affects the implementation details of a class and not the outside code.[3]

抽象既简化设计和编程,也保证了程序的安全性:其实现(Implementation)的改变并不会影响整个类。

Read more: What Is Abstraction in Computer Science? With Types and FAQs

我们只知道这里有一个板子,上面有几个不同功能的按钮,而按钮下面是看不到的。只需要按下这几个按钮就可以实现相应的功能,看不到内部发生了什么。

So comes the question: What’s the difference between Encapsulation and Abstraction.

Read more: 6 Examples of Data Abstraction (With Definition and Benefits)

Inheritance

Inheritance means a new class automatically inhabits the same properties and functionalities as its parent class.

继承使程序员可以重复使用代码且不必在子类中再重新定义函数

Related: 44 Coding Terms To Know (With Definitions)

Polymorphism

Polymorphism refers to creating objects with shared behaviors. In OOP, polymorphism allows for the uniform treatment of classes in a hierarchy. When you write code for objects at the root of the hierarchy, any objects created by a child class within the hierarchy have the same functions. Depending on the type of object, it may execute different behaviors.

比如,鼠标的left_click动作,点击关闭按钮是关闭页面,点击文件是选中(Selected), 这就是左键单击函数的多态。

Benefits of OOP

  • Reusable code
  • Increased productivity
  • Enhanced security

7 Principles of OOP

Contents come from 面向对象编程七大编程原则概述详解.

  1. Single Responsibility Principle, SRP
  2. Open-Closed Principle, OCP
  3. Dependence Inversion Principle,DIP
  4. Liskov Substitution Principle, LSP
  5. interface segregation principle, ISP
  6. Composite Reuse Principle,CRP/Composition/Aggregate Reuse Principle,CARP[4]
  7. Law of Demeter, LoD, or Least Knowledge Principle,LKP
GithubLogo

  1. A paradigm is a model for something which explains it or shows how it can be produced. ↩︎

  2. The 12 terms are: Objects, Classes, Attributes, Behaviors, Methods, Constructor, Abstraction, Encapsulation, Inheritance, Polymorphism, Instance. ↩︎

  3. Maybe abstraction is not always right. Abstraction Can Make Your Code Worse ↩︎

  4. Notice: The earliest expression of “Prefer Composition Over Inheritance” may be Design Mode, 1994, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. See: The Flaws of Inheritance and in Chinese: 【熟】代码美学:组合为何优于继承?. ↩︎