首页 > 其他分享 >Object-oriented Programming

Object-oriented Programming

时间:2023-04-30 10:55:09浏览次数:48  
标签:Abstraction Object Programming oriented code Principle class OOP

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: 【熟】代码美学:组合为何优于继承?. ↩︎

标签:Abstraction,Object,Programming,oriented,code,Principle,class,OOP
From: https://www.cnblogs.com/Nocturne-Xiaoma/p/17365021.html

相关文章

  • Java的Object类
    Object类Object类是Java中所有类的始祖,在Java中每个类都是由它扩展而来的。但是并不需要这样写:publicclassEmployeeextendsObject如果没有明确地指出父类,Object就被认为是这个类的父类。所有的数组类型,不管是对象数组还是基本类型的数组都扩展了Object类。Employe......
  • Java的Object类
    Object类Object类是Java中所有类的始祖,在Java中每个类都是由它扩展而来的。但是并不需要这样写:publicclassEmployeeextendsObject如果没有明确地指出父类,Object就被认为是这个类的父类。所有的数组类型,不管是对象数组还是基本类型的数组都扩展了Object类。Employ......
  • apollo启动报错java.lang.NoClassDefFoundError PemObjectGenerator
    启动apollo时,报错。java.lang.NoClassDefFoundError:org/bouncycastle/util/io/pem/PemObjectGenerator很明显类没找到,根据经验判断,很可能是maven依赖问题。解决方案apollo版本1.1.4。目前相关的是:org.bouncycastle:bcpkix-jdk15on:jar:1.55org.bouncycastle:bcprov-jdk15on:jar......
  • SWEN20003 Object Oriented软件开发
    SWEN20003ObjectOrientedSoftwareDevelopmentProject2,2023TheUniversityofMelbourneSchoolofComputingandInformationSystemsSWEN20003ObjectOrientedSoftwareDevelopmentShadowPacProject2,Semester1,2023Released:Friday,21stApril2023at4:30......
  • 【git基础问题】LFS upload missing objects
    前言 修改文件权限;还是不行;修改文件权限之后,使用gitpush就可以了;gitpush-uorigintda4dev#好像ok了gitlfspush--allorigintda4dev#notok   参考1. 完......
  • access 查询报“提供程序无法确定 Object 值.例如,该行刚刚创建,未提供 Object 列的默
    access查询报“提供程序无法确定Object值.例如,该行刚刚创建,未提供Object列的默认值,并且”System.InvalidOperationException:TheprovidercouldnotdeterminetheObjectvalue.Forexample,therowwasjustcreated,thedefaultfortheObjectcolumnwasnota......
  • 【git基础问题】LFS upload missing objects
    前言 修改文件权限;还是不行;修改文件权限之后,使用gitpush就可以了;gitpush-uorigintda4dev#好像ok了gitlfspush--allorigintda4dev#notok   参考1. 完......
  • 类型体操-Tuple to Object
    刷类型体操时,问题的记录及收获TupletoObjectconsttuple=['tesla','model3','modelX','modelY']asconsttyperesult=TupleToObject<typeoftuple>//expected{'tesla':'tesla','model3�......
  • AttributeError: 'numpy.ndarray' object has no attribute 'imresize'
    问题描述:AttributeError:'numpy.ndarray'objecthasnoattribute'imresize' 分析原因:`imresize`函数已经在新版的`scipy`中被弃用,导致报错。解决办法:使用`skimage`库中的`resize`函数来代替。需要先安装`skimage`库 pipinstallscikit-image#安装skimage库fro......
  • objection使用
    Fridaobjection启动objection保证Fridaserver开启命令行输出objection-gpackageNameexploreobjection的缺陷缺少对Native层的支持对象类型的数据打印有问题,比如Bytearray和Json,会显示成[object,object]对Spawn方式启动的支持不够流畅退出exit视频演示:https://asciinema.or......