为新程序员定义OOP [英] Defining OOP for a new programmer

查看:58
本文介绍了为新程序员定义OOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在向没有编程背景的人教授第一语言时,尽管我偏爱OOP,但我仍然很难定义OOP,如何为编程经验很少(或为零)的人定义OOP?

In teaching a first language to someone with no programming background I am having a hard time defining OOP despite the fact that I prefer OOP, how can I define OOP to someone with little (or zero) programming experience?

推荐答案

您可以尝试以下方法,该方法是我在一段时间前在论坛上发布的内容中进行了一些修改:

You might try something like the following approach, which I modified slightly here from a post on a forum I made a while ago:

用于OOP的最基本的词汇集是类,方法和参数.

The most basic set of vocabulary for OOP is a class, a method, and a parameter.

类是一组可以共同完成一项任务的功能.类的实例被视为对象.

A class is a set of functions that work together to accomplish a task. An instance of a class is considered an object.

方法只是引用封装在类中的函数.

A method simply refers to a function that is encased in a class.

参数是传递给函数的变量,该函数指示其如何执行操作或向其提供要处理的信息.

A parameter is a variable that is passed into a function that instructs it how to act or gives it information to process.

如果您进行一些挖掘,将会发现大量有关设计模式的信息.尽管我会一开始就过多地考虑它们,但其中的一些可能会很有用,尽管我会特别小心,因为它们可能会让人不知所措.尝试使自己进入OOP心态时,请记住两个有用的(但有些被过度使用的)首字母缩写:DRY和KISS.

If you do a bit of digging, you'll find a wealth of information about design patterns. Some of these might be useful to look at, though I'd be careful about getting into them too much at first because they can be overwhelming. There are two helpful (and somewhat overused) acronyms you might keep in mind when trying to get yourself into an OOP mindset: DRY and KISS.

DRY代表不要重复自己",这就是这个意思.如果您编写一些代码,则不必再重复该特定代码.实际上,它意味着更抽象的思维并在一开始就计划得更好.我会尽快举一个例子.

DRY stands for Don't Repeat Yourself and it means just that. If you write some code, you shouldn't have to repeat that particular code ever again. In practical terms, it means thinking more abstractly and planning a little better at the outset. I'll give an example shortly.

KISS代表保持简单,愚蠢",意味着您应该尝试编写以尽可能简单的方式实现其目标的代码.更简单意味着出错的可能性更少,维护也更容易.在OOP的上下文中,这通常意味着确保每个方法或函数仅具有一个任务.如果您发现某个方法可以完成多项工作,则通常意味着可以将其重构为几个较小的方法,每个方法专用于特定任务.

KISS stands for Keep It Simple, Stupid and means that you should try to write code that accomplishes its goal in the simplest manner possible. Simpler means fewer possibilities for errors and easier maintenance. In the context of OOP, this usually means making sure that each method or function has only one task. If you find that a method does more than one thing, it usually means that it can be refactored into several smaller methods, each dedicated to a specific task.

现在举一个简单的例子(也许有人可以提出一个更好的例子,但是现在就和我一起去吧)

Now for a simple example (someone might be able to come up with a better one, but go with me on it for now):

假设您需要编写两种不同的形式,一种形式处理有关汽车的信息,另一种形式对卡车执行相同的操作.

Let's say that you need to program two different forms, one that processes information about cars and one that does the same for trucks.

对于汽车,我们将要记录以下信息:

For cars, we will want to record the following info:

  • 颜色
  • 引擎尺寸
  • 传输类型
  • 门数

对于卡车,我们需要:

  • 颜色
  • 引擎尺寸
  • 传输类型
  • 驾驶室大小
  • 牵引能力

在过程编程中,您将首先编写代码来处理汽车表格,然后再编写卡车表格的代码.

In procedural programming, you would write the code first to process the car form and then the code for the truck form.

使用面向对象的程序设计,您将编写一个称为Vehicle的基类,该基类将记录我们从卡车和轿车中获得的共同特征.在这种情况下,车辆类别将记录:

With object-oriented programming, you would write a base class called vehicle that would record the common characteristics what we need from both trucks and cars. In this case, the vehicle class will record:

  • 颜色
  • 引擎尺寸
  • 传输类型

我们将把这些特征中的每个特征变成一个单独的方法.例如,颜色方法可以将车辆的颜色作为参数并对其进行处理,例如将其存储在数据库中.

We'll make each one of those characteristics into a separate method. The color method, for example, could take the color of the vehicle as a parameter and do something with it, like storing it in a database.

接下来,我们将再创建两个类:卡车和汽车,这两个类都将继承车辆类的所有方法,并使用它们唯一的方法对其进行扩展.

Next, we will create two more classes: truck and car, both of which will inherit all of the methods of the vehicle class and extend it with methods that are unique to them.

汽车类将具有一个名为numberOfDoors的方法,而卡车类将具有cabSize和towingCapacity方法.

The car class will have a method called numberOfDoors and the truck class will have the methods cabSize and towingCapacity.

好的,所以让我们假设我们有一个适用于程序和OO编程的示例.现在,让我们来看一些场景.

Okay, so let's assume that we have a working example for both procedural and OO programming. Now, let's run through a few scenarios.

方案1 :假设我们突然需要添加一个总线表单,其中记录了以下信息:

Scenario 1: Suppose that we suddenly need to add a bus form, that records the following information:

  • 颜色
  • 引擎尺寸
  • 传输类型
  • 乘客人数

程序:我们需要重新创建整个表格,并重复颜色,引擎尺寸和变速器类型的代码.

Procedural: We need to recreate the entire form, repeating the code for Color, Engine Size, and Transmission Type.

OOP :我们只需将车辆类扩展为公交车类,然后添加方法numberOfPassengers.

OOP: We simply extend the vehicle class with a bus class and add the method, numberOfPassengers.

场景2 :由于某种奇怪的原因,我们的客户希望将颜色通过电子邮件发送给他,而不是像以前那样将颜色存储在数据库中.

Scenario 2: Instead of storing color in a database like we previously did, for some strange reason our client wants the color emailed to him.

程序:我们更改了三种不同的形式:汽车,卡车和公共汽车,将颜色通过电子邮件发送给客户端,而不是将其存储在数据库中.

Procedural: We change three different forms: cars, trucks, and buses to email the color to the client rather than storing it in the database.

OOP :我们更改了车辆类别中的颜色方法,因为汽车,卡车和公共汽车类别都扩展(或从其继承而来,换句话说),它们是自动更新.

OOP: We change the color method in the vehicle class and because the car, truck, and bus classes all extend (or inherit from, to put it another way) the vehicle class, they are automatically updated.

方案3 :我们希望从通用汽车转向特定品牌,例如:日产和马自达.

Scenario 3: We want to move from a generic car to specific makes, for example: Nissan and Mazda.

程序:我们为每个品牌创建一个新表格,重复所有通用汽车信息代码,并添加每个品牌特定的代码.

Procedural: We create a new form for each make, repeating all of the code for generic car information and adding the code specific to each make.

OOP :我们将汽车类扩展为nissan类和mazda类,并为该汽车制造商的每组唯一信息添加方法.

OOP: We extend the car class with a nissan class and a mazda class and add methods for each set of unique information for that car make.

方案4 :我们在表单的传输类型区域中发现了一个错误,需要对其进行修复.

Scenario 4: We found a bug in the transmission type area of our form and need to fix it.

程序:我们打开并更新每个表格.

Procedural: We open and update each form.

OOP :我们将transmissionType方法固定在车辆类中,并且更改会在继承自该类的每个类中永久保留.

OOP: We fix the transmissionType method in the vehicle class and the change perpetuates in every class that inherits from it.

从以上场景中可以看到,采用OOP样式比使用过程编程具有明显的优势,尤其是随着规模的增加.如果我们还必须添加用于船,摩托车,飞机,卡丁车,ATV,雪地摩托等的表格,请考虑一下从OOP的代码重复,灵活性和维护方面可以节省的费用.

As you can see from the above scenarios, employing an OOP style has significant advantages over procedural programming, especially as your scale increases. Consider the savings we would receive from OOP in terms of repeated code, flexibility, and maintenance if we also had to add forms for boats, motorcycles, planes, go-karts, ATVs, snowmobiles, etc.

通过使用单元测试来测试结果,对象和方法也比过程编程更容易测试.

Objects and methods are also far easier to test than procedural programming by using unit testing to test results.

这是否意味着您永远不应该使用过程编程?不必要.如果您正在制作模型或概念验证应用程序,则可能没有时间使所有内容都面向对象,因此我认为对原型使用过程编程可能会更好,但最好以OO方式生产产品.

Does this mean that you should never use procedural programming? Not necessarily. If you're doing a mockup or a proof-of-concept app, you might not have the time to make everything object-oriented and so I think it might would be better to use procedural programming for a prototype, but it would be best to make the production product in an OO-manner.

这篇关于为新程序员定义OOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆