面向对象编程概念

by Alexander Petkov

通过亚历山大·佩特科夫(Alexander Petkov)

Have you noticed how the same cliche questions always get asked at job interviews — over and over again?

您是否注意到在求职面试中总是一遍又一遍地问同样的陈词滥调问题?

I’m sure you know what I mean.

我确定你知道我的意思。

For example:

例如:

Where do you see yourself in five years?

五年后您在哪里看到自己?

or, even worse:

或更糟糕的是:

What do you consider to be your greatest weakness?

您认为什么是您最大的弱点?

Ugh…give me a break. I consider answering this question a great weakness! Anyway, not my point.

gh…给我休息一下。 我认为回答这个问题是一个很大的弱点! 无论如何,这不是我的意思。

As trivial as questions like these may be, they are important because they give clues about you. Your current state of mind, your attitude, your perspective.

尽管像这些问题一样琐碎,但它们很重要,因为它们为您提供了线索。 您当前的心态,态度和观点。

When answering, you should be careful, as you may reveal something you later regret.

在回答时,您应该小心,因为您可能会透露一些您以后会后悔的东西。

Today I want to talk about a similar type of question in the programming world:

今天,我想谈一谈编程世界中类似的问题:

What are the main principles of Object-Oriented Programming?

面向对象编程的主要原理是什么?

I’ve been on both sides of this question. It’s one of those topics that gets asked so often that you can’t allow yourself to not know.

我一直在这个问题的两面。 这是经常被问到的一些主题之一,以至于您无法让自己不知道。

Junior and entry-level developers usually have to answer it. Because it’s an easy way for the interviewer to tell three things:

初级和入门级开发人员通常必须回答。 因为这是面试官说出三件事的简单方法:

  1. Did the candidate prepare for this interview?

    候选人是否准备了这次面试?

    Bonus points if you hear an answer immediately — it shows a serious approach.

    如果您立即听到答案,将获得加分-这表明您是认真的方法。

  2. Is the candidate past the tutorial phase?

    候选人是否已超过辅导阶段?

    Understanding the principles of Object-Oriented Programming (OOP) shows you’ve gone beyond copy and pasting from tutorials — you already see things from a higher perspective.

    了解面向对象编程(OOP)的原理表明,您已经超出了教程中的复制和粘贴范围-您已经从更高的角度看到了事情。

  3. Is the candidate’s understanding deep or shallow?

    候选人的理解是深还是浅?

    The level of competence on this question often equals the level of competence on

    在这个问题上的能力水平通常等于在这个问题上的能力水平

    most other subjects. Trust me.

    其他大多数科目 。 相信我。

The four principles of object-oriented programming are encapsulation, abstraction, inheritance, and polymorphism.

面向对象程序设计的四个原则是封装抽象继承 , 和多态性

These words may sound scary for a junior developer. And the complex, excessively long explanations in Wikipedia sometimes double the confusion.

对于初级开发人员来说,这些话听起来很吓人。 维基百科中复杂而冗长的解释有时会使混乱加倍。

That’s why I want to give a simple, short, and clear explanation for each of these concepts. It may sound like something you explain to a child, but I would actually love to hear these answers when I conduct an interview.

这就是为什么我要对每个概念进行简单,简短和清晰的解释。 听起来像是您向孩子解释的事情,但是当我进行面试时,我实际上很想听听这些答案。

封装形式 (Encapsulation)

Say we have a program. It has a few logically different objects which communicate with each other — according to the rules defined in the program.

说我们有一个程序。 根据程序中定义的规则,它具有几个逻辑上不同的对象,可以彼此通信。

Encapsulation is achieved when each object keeps its state private, inside a class. Other objects don’t have direct access to this state. Instead, they can only call a list of public functions — called methods.

当每个对象在类中保持其状态为private时 ,就可以实现封装。 其他对象没有直接访问此状态的权限。 取而代之的是,它们只能调用公共函数列表(称为方法)。

So, the object manages its own state via methods — and no other class can touch it unless explicitly allowed. If you want to communicate with the object, you should use the methods provided. But (by default), you can’t change the state.

因此,对象通过方法管理自己的状态-除非明确允许,否则其他任何类都不能接触它。 如果要与对象通信,则应使用提供的方法。 但是(默认情况下),您无法更改状态。

Let’s say we’re building a tiny Sims game. There are people and there is a cat. They communicate with each other. We want to apply encapsulation, so we encapsulate all “cat” logic into a Cat class. It may look like this:

假设我们正在构建一个小型的《模拟人生》游戏。 有人,有一只猫。 他们彼此交流。 我们要应用封装,因此我们将所有“ cat”逻辑封装到Cat 类。 它可能看起来像这样:

Here the “state” of the cat is the private variables mood, hungry and energy. It also has a private method meow(). It can call it whenever it wants, the other classes can’t tell the cat when to meow.

在这里,猫的“状态”是moodhungryenergy私人变量 。 它还有一个私有方法meow() 它可以随时调用它,其他类则不能告诉猫何时叫声。

What they can do is defined in the public methods sleep(), play() and feed(). Each of them modifies the internal state somehow and may invoke meow(). Thus, the binding between the private state and public methods is made.

它们可以做什么在公共方法 sleep()play()feed() 它们每个都以某种方式修改内部状态,并且可以调用meow() 因此,在私有国家和公共方法之间建立了约束。

This is encapsulation.

这是封装。

抽象化 (Abstraction)

Abstraction can be thought of as a natural extension of encapsulation.

抽象可以被认为是封装的自然扩展。

In object-oriented design, programs are often extremely large. And separate objects communicate with each other a lot. So maintaining a large codebase like this for years — with changes along the way — is difficult.

在面向对象的设计中,程序通常非常大。 并且单独的对象彼此之间经常进行通信。 因此,要像这样多年维护大型代码库(并不断进行更改)是很困难的。

Abstraction is a concept aiming to ease this problem.

抽象是旨在缓解此问题的概念。

Applying abstraction means that each object should only expose a high-level mechanism for using it.

应用抽象意味着每个对象应公开使用它的高级机制。

This mechanism should hide internal implementation details. It should only reveal operations relevant for the other objects.

此机制应隐藏内部实现细节。 它仅应显示与其他对象相关的操作。

Think — a coffee machine. It does a lot of stuff and makes quirky noises under the hood. But all you have to do is put in coffee and press a button.

想想-咖啡机。 它做很多事情,在引擎盖下发出古怪的声音。 但是,您所要做的只是喝咖啡并按下一个按钮。

Preferably, this mechanism should be easy to use and should rarely change over time. Think of it as a small set of public methods which any other class can call without “knowing” how they work.

优选地,该机制应该易于使用并且应该很少随时间变化。 可以将其视为少量的公共方法,任何其他类都可以在不“知道”它们如何工作的情况下调用它们。

Another real-life example of abstraction? Think about how you use your phone:

另一个现实生活中的抽象示例? 考虑一下如何使用手机:

You interact with your phone by using only a few buttons. What’s going on under the hood? You don’t have to know — implementation details are hidden. You only need to know a short set of actions.

您只需使用几个按钮即可与手机互动。 到底发生了什么事? 您不必知道-实施细节被隐藏。 您只需要知道一些简短的操作即可。

Implementation changes — for example, a software update — rarely affect the abstraction you use.

实现更改(例如,软件更新)很少影响您使用的抽象。

遗产 (Inheritance)

OK, we saw how encapsulation and abstraction can help us develop and maintain a big codebase.

好的,我们看到了封装和抽象如何帮助我们开发和维护大型代码库。

But do you know what is another common problem in OOP design?

但是您知道OOP设计中的另一个常见问题是什么吗?

Objects are often very similar. They share common logic. But they’re not entirely the same. Ugh…

对象通常非常相似。 他们有着共同的逻辑。 但他们不完全一样。 啊…

So how do we reuse the common logic and extract the unique logic into a separate class? One way to achieve this is inheritance.

那么,我们如何重用通用逻辑并将独特逻辑提取到单独的类中呢? 实现此目的的一种方法是继承。

It means that you create a (child) class by deriving from another (parent) class. This way, we form a hierarchy.

这意味着您通过派生另一个(父)类来创建(子)类。 这样,我们形成了一个层次结构。

The child class reuses all fields and methods of the parent class (common part) and can implement its own (unique part).

子类重用父类的所有字段和方法(公共部分),并可以实现其自己的(唯一部分)。

For example:

例如:

If our program needs to manage public and private teachers, but also other types of people like students, we can implement this class hierarchy.

如果我们的计划需要管理公共和私人教师,还需要管理其他类型的人(如学生),我们可以实施此类班级结构。

This way, each class adds only what is necessary for it while reusing common logic with the parent classes.

这样,每个类在重复使用父类的通用逻辑的同时,仅添加必要的内容。

多态性 (Polymorphism)

We’re down to the most complex word! Polymorphism means “many shapes” in Greek.

我们只能说最复杂的词了! 多态性在希腊语中意为“许多形状”。

So we already know the power of inheritance and happily use it. But there comes this problem.

因此,我们已经知道继承的力量并乐于使用它。 但是出现了这个问题。

Say we have a parent class and a few child classes which inherit from it. Sometimes we want to use a collection — for example a list — which contains a mix of all these classes. Or we have a method implemented for the parent class — but we’d like to use it for the children, too.

假设我们有一个父类和一些继承自它的子类。 有时我们想使用一个集合(例如一个列表),其中包含所有这些类的混合。 或者我们为父类实现了一个方法-但我们也想将其用于子类。

This can be solved by using polymorphism.

这可以通过使用多态来解决。

Simply put, polymorphism gives a way to use a class exactly like its parent so there’s no confusion with mixing types. But each child class keeps its own methods as they are.

简而言之,多态为使用类提供了一种完全类似于其父类的方法,因此不会与混合类型混淆。 但是每个子类都按原样保留自己的方法。

This typically happens by defining a (parent) interface to be reused. It outlines a bunch of common methods. Then, each child class implements its own version of these methods.

这通常是通过定义要重用的(父)接口来发生的。 它概述了一堆常用方法。 然后,每个子类实现这些方法的自己的版本。

Any time a collection (such as a list) or a method expects an instance of the parent (where common methods are outlined), the language takes care of evaluating the right implementation of the common method — regardless of which child is passed.

每当集合(例如列表)或方法需要父级实例(概述了通用方法)时,该语言都会负责评估通用方法的正确实现-不管传递哪个子级。

Take a look at a sketch of geometric figures implementation. They reuse a common interface for calculating surface area and perimeter:

看一下几何图形实现的草图。 它们重用了一个公共接口来计算表面积和周长:

Having these three figures inheriting the parent Figure Interface lets you create a list of mixed triangles, circles, and rectangles. And treat them like the same type of object.

通过使这三个图形继承父Figure Interface ,可以创建混合trianglescirclesrectangles 。 并将它们视为相同类型的对象。

Then, if this list attempts to calculate the surface for an element, the correct method is found and executed. If the element is a triangle, triangle’s CalculateSurface() is called. If it’s a circle — then cirlce’s CalculateSurface() is called. And so on.

然后,如果此列表尝试计算元素的曲面,则会找到并执行正确的方法。 如果元素是三角形,则三角形的CalculateSurface() 叫做。 如果是圆,则为cirlce的CalculateSurface() 叫做。 等等。

If you have a function which operates with a figure by using its parameter, you don’t have to define it three times — once for a triangle, a circle, and a rectangle.

如果您有一个通过使用图形的参数来处理图形的函数,则不必定义3次-一次定义一个三角形,一个圆形和一个矩形。

You can define it once and accept a Figure as an argument. Whether you pass a triangle, circle or a rectangle — as long as they implement CalculateParamter(), their type doesn’t matter.

您可以定义一次并接受一个Figure 作为争论。 传递三角形,圆形还是矩形-只要它们实现CalculateParamter() ,它们的类型就无关紧要。

I hope this helped. You can directly use these exact same explanations at job interviews.

希望对您有所帮助。 您可以在面试中直接使用这些完全相同的解释。

If you find something still difficult to understand — don’t hesitate to ask in the comments below.

如果您发现仍然难以理解的内容,请随时在下面的评论中提问。

下一步是什么? (What’s next?)

Being prepared to answer one of the all-time interview question classics is great — but sometimes you never get called for an interview.

准备回答一个经典的面试问题真是太好了,但是有时候您从没有被要求面试。

Next, I’ll focus on what employers want to see in a junior developer and how to stand out from the crowd when job hunting.

接下来,我将重点介绍雇主希望在初级开发人员中看到的东西,以及在求职时如何脱颖而出。

Stay tuned.

敬请关注。

翻译自: https://www.freecodecamp.org/news/object-oriented-programming-concepts-21bb035f7260/

面向对象编程概念

面向对象编程概念_如何向6岁的孩子解释面向对象的编程概念相关推荐

  1. 小孩学编程课程_我在17岁时学到的关于编程的7个重要课程

    小孩学编程课程 by Alec Jones 通过亚历克琼斯 我在17岁时学到的关于编程的7个重要课程 (7 important lessons about programming that I've ...

  2. python中面向对象的缺点_最简单的方法搞懂Python面向对象

    1.面向对象介绍 2.类和对象 3.面向对象基本语法 面向对象与面向过程面向过程:根据业务逻辑从上到下写代码. 面向对象:将变量与函数绑定到一起,分类进行封装,每个程序只要负责分配给自己的分类,这样能 ...

  3. 与孩子一起学编程代码_这周与您的孩子一起做一个代码小时

    与孩子一起学编程代码 The Hour of Code started in 2013 with the goal of getting kids excited about programming ...

  4. java机器人编程简介_机器人十大流行编程语言的介绍及机器人编程系统以及操作方法...

    "如果您问"机器人的最佳编程语言是什么?计算机视觉程序员会给出不同于认知机器人的答案.每个人都不同意什么是"最好的编程语言",语言首先学习,即使这是最现实的答案 ...

  5. python支持函数式编程吗_利用Fn.py库在Python中进行函数式编程

    尽管Python事实上并不是一门纯函数式编程语言,但它本身是一门多范型语言,并给了你足够的自由利用函数式编程的便利.函数式风格有着各种理论与实际上的好处(你可以在Python的文档中找到这个列表): ...

  6. fanuc机器人码垛编程实例_两个很简单的FANUC系统CNC加工中心编程实例

    CNC加工中心的编程实例有很多,针对不同的加工工序有不一样的程序编制方法.操机人员通过编程可以使机器达到不同的加工效果. 在这里,我们将会带给大家几种关于法兰克系统的CNC加工中心的编程实例: 1:如 ...

  7. 米兔机器人 编程案例_米兔积木机器人改装双层立体车库,通过编程变得更智能...

    上次,我们分享了双层车库的搭建步骤[没车不要紧,先拼个智能车库吧],但只有外观仍然不能实现车库实现自动化运行,为了让小小停车库变得更智能,我们采用了米兔积木机器人拓展包颜色传感器,并通过米兔积木机器人 ...

  8. 宏定义编程软件_什么是计算机编程? 定义软件开发。

    宏定义编程软件 My five year old son, Ramy, approached me one day while I was working from home and asked, & ...

  9. 一年级的孩子可以学习机器人编程

    一年级的孩子可以学习机器人编程?现在越来越多的家长在培养孩子的学习的时候,会给孩子选择一些能够提升孩子能力的课程.就拿现在很多的家长想要孩子去学习机器人编程的课程来说,他们对于一年级的孩子学习机器人编 ...

最新文章

  1. c# 中Stopwatch 类的运用
  2. Android 屏幕适配攻略(三)单位dp与sp
  3. 解决 jupyter-lab 安装插件后无法使用的问题(重新安装插件无法使用)
  4. ModelCoder中的代数环问题
  5. 天才小毒妃 第878章 你喜欢孩子
  6. 大二Git-Branching学习
  7. 机甲大师机器人控制(一):概念与流程
  8. vue自动滚动组件 可以支持鼠标滚轮操作
  9. 关于m3u8格式的视频文件ts转mp4下载和key加密问题
  10. Python——线性回归模型
  11. 32位系统装8g内存条?能用吗
  12. ICCV 2021审稿结果出炉,有人已总结出了一份Rebuttal写作指南
  13. 装饰者(Decorator)模式
  14. chinese input
  15. 最小生成树--还是畅通工程
  16. 百度竞价关键词质量度提升的方法你知道多少?
  17. pythonhtml生成word_html转word
  18. java 求水仙花数
  19. 跨域的五种解决方案详解
  20. POP / IMAP 服务器的构建( Dovecot )

热门文章

  1. 数据库表(字段类型、约束、截断表、修改表字段、重命名表)
  2. php ile_get_contents无法请求https连接的解决方法
  3. iOS 关闭页面侧滑手势
  4. 利用Injection插件加快Xcode编译速度
  5. RubyGems 库发现了后门版本的网站开发工具 bootstrap-sass
  6. javascript推荐书籍
  7. Centos7.x系统配置上的变化【转】
  8. MyBatis 中的 set 元素用法(MyBatis 3.1.1)
  9. 程序员:提高编程效率的技巧
  10. 如何用Swift实现一个好玩的弹性动画