10以内加减法编程
Object Oriented Programming (OOP) is all too often viewed by those unfamiliar to it as a science to be mastered. OOP however, is an art...a methodology, a simple (but powerful) way of defining what a program is. After going through many tutorials on the subject some of us still scratched our heads and lamented, "Interesting philosophy, but I still don't know how to do OOP." We wanted to master a science and we couldn't find the telltale step-by-step instructions we were looking for. So, I've written this article to save you that frustration.
不熟悉面向对象编程(OOP)的人经常将其视为一门要掌握的科学。 但是,OOP是一门艺术……一种方法论,是一种定义程序是什么的简单(但功能强大)的方法。 通过很多教程去一些关于这个问题我们还是划伤我们的头和感叹之后,“有趣的理念,但我还是不知道该怎么办 OOP。” 我们想精通一门科学,但找不到所需的分步说明。 所以,我写这篇文章是为了让您免于沮丧。
Placing objects inside a program does not, by itself, make it Object Oriented. For that to happen we need to consider how the parts of a program relate to each other. Let's think of a pet tracking program as just a bunch of instructions thrown together...now OOP sees that same program rearranged as several self-contained groups of related functions and variables (these groupings are determined by your common-sense). Don't make this more complicated..that's THE concept!
仅将对象放置在程序内部并不能使其成为面向对象的 。 为此,我们需要考虑程序各部分之间的相互关系。 让我们把宠物跟踪程序想象成一堆指令,一起把它们放在一起……现在,OOP看到同一程序重新排列为几个独立的相关功能和变量组(这些分组由您的常识决定)。 不要把这个变得更复杂..那就是概念!
These groups are called Classes (which is just a blueprint for an object). For example, a dog class may have a "doTrick" subroutine and a "breed" variable, but no "sweater-size" (that's absolutely ridiculous, but I digress). Dog tricks and breeds are both related to 'dog', that's the common-sense part. 'Class' is a blueprint, just as 'integer' is an existing blueprint when you declare "i" as 'integer.' And with OOP, a variable like "i" can be declared from a 'dog' blueprint. The difference here is that you, the programmer, write the code that defines 'dog'. After making that dog-blueprint you can declare a new variable (like "i") as an instance of 'dog.' That can then be filled with unique data and passed around to other objects in the program.
这些组称为类 ( Classs) (仅是对象的蓝图)。 例如,狗类可能具有“ doTrick”子例程和“ breed”变量,但没有“ sweater-size”(这绝对是荒谬的,但我离题了)。 狗的花样和品种都与“狗”有关,这是常识部分。 当您将“ i”声明为“整数”时,“类”是一个蓝图,就像“整数”是现有的蓝图。 使用OOP时,可以从“狗”蓝图中声明类似“ i”的变量。 这里的区别就是你 ,程序员,编写代码,定义“狗”。 绘制完该狗的蓝图之后,您可以声明一个新变量(例如“ i”)作为“狗”的实例。 然后可以用唯一的数据填充该数据,并将其传递给程序中的其他对象。
Rules must be used to govern what parts of an object are seen (and/or altered) by other objects in the program (aka “Encapsulation"). So let's make a rule. Well, 'color' is seen by people (who, incidentally, would probably be a class). They are allowed to get your dog's color but shouldn't be allowed to set it. "Purple poodle" I hear you say? Well, if you insist. But to do that, let's declare a new 'fancy dog' class (one with sweater-size and settable color). Here OOP gives us something wonderful..we can reuse ALL the existing class code in 'dog' (and no, it's not cut and paste)! In OOP we just create an empty class named 'fancy_dog', and tell it to inherit from the existing 'dog' class. We then just add sweater-size and color as new variables inside it (remembering to make color settable) ….and now we're doing serious OOP!
必须使用规则来控制程序中其他对象(或称为“ 封装 ”)看到(和/或更改)对象的哪些部分。让我们制定一个规则。好吧,“颜色”被人(谁,顺便说一句,可能会是一个类),它们允许你的狗的颜色,但不应该被允许设置它。“紫狮子狗”我听到你说什么?好吧,如果你坚持,但是要做到这一点,让我们声明新的“花哨的狗”类(一种具有毛衣大小和可设置颜色的类),在这里,OOP给我们带来了奇妙的效果..我们可以重用“狗”中的所有现有类代码(不,它不能剪切粘贴)!我们只是创建一个名为'fancy_dog'的空类,并告诉它从现有的'dog'类继承 ,然后在其中添加毛衣大小和颜色作为新变量(记住要使颜色可设置)……现在正在做严重的OOP!
Other concepts such as “Loose Coupling,” "Composition," "Aggregation," and UML are useful and aren't too hard to tackle once you’ve master the ideas discussed above.
其他概念(例如“松散耦合”, “组成”,“聚合”和UML)很有用,一旦您掌握了上面讨论的思想,就不会太难解决。
**If you found this article helpful, please click the "Thumbs-Up" (below and to the left).
** 如果您认为这篇文章对您有所帮助,请单击“竖起大拇指”(在下方和左侧) 。
10以内加减法编程