Lots of interesting points already mentioned here.
One way to think about it is that in OO, you have the idea of 'objects' which are things that have characteristics and behaviors inherent to them. They typically have some sort of public 'interface' which provides a mechanism to retrieve some information about them, but the object itself, or rather its 'class', limits what information is publicly available. The innards of the object are not exposed to the public because there's typically no need to know the dirty details 'under the hood' of the object. So object oriented programs utilize this construct, as well as other things.
Procedural programming doesn't typically utilize such a coupling of data and behavior into an 'object'. I've seen it done in C before but it wasn't pretty and involved way too much monkey business to approximate what one could do with, say, C++.
One of the ideas behind object oriented development is that I ought not be able to muck with your data through any means other than the ones that you've provided. If you provide me only a well-thought out interface, you can keep me honest. Now, if you are using a procedural approach and you send me a structure that has no built-in protections, well then I can do as I please and if I'm dumb or evil, I can change things you might not want me to change.
Granted, you can circumvent the object if you are clever but you've got to go out of the way to do this.
在 C++ 中,对象的方法只是作用于对象的过程。 但在真正的面向对象范式中,您应该将方法视为对象可以接收的潜在消息(即字母)。 对象接收消息(参数代表消息的有效负载,即信件的内容)并根据消息更改其状态。
The way C++ is implemented just makes OO programming look a lot like procedural programming. You need to shift your thinking slightly.
In C++ objects have methods that are just procedures that act on the object. But in a real OO paradiam you should think of the methods as potential messages that the object can recieve (ie letters). The object recieves a message (the parameters represent the payload of the message ie the content of the letter) and changes its state based on the message.
有关过程式和面向对象之间差异的一个相当直观的示例,请尝试学习 Smalltalk。 在 Smalltalk 中,一切,我的意思是一切都是对象。 没有 if 语句或 while 循环。 您可以通过向其他对象发送消息(也称为调用其方法)来实现该功能。 一开始它确实会让您头晕,但我想您很快就会明白 OO 应该是什么。
For a fairly in-your-face example of the difference between procedural and OO, try learning Smalltalk. In Smalltalk, everything, and I mean everything is an object. There are no if-statements or while-loops. You achieve that functionality by sending messages to (a.k.a. invoking methods on) other objects. It really makes your head spin at first, but I think you'll quickly grok what OO is supposed to be.
In a procedural program, the code is king and the data is subordinate. In other words, you have programs which act on data and they're not usually tightly bound.
In the OO world, objects are the primary thing of interest. An object consists of data and the code that is allowed to act on that data, and they are very tightly bound. It is the concept of encapsulation, the hiding of information.
An example, let's say you have a number and you want to double it. A procedural way of doing this is:
n = n * 2
The code here quite explicitly multiplies n by 2 and stores the result back into n.
The OO way of doing this is to send a "message" to the number object telling it to double itself:
n.double();
The advantage of this is called polymorphism. What happens when you decide you want to be able to double a string like "bob". In the procedural world, you'd have to provide more code to do the doubling but you'd also have to call that code differently.
With OO, you create a string object which can also take the 'double' message. The code to double a string belongs to the string object so it knows it has to act differently to the number object. If it decided that "bob" * 2 was "bobbob", the code would look something like:
class number: class string:
int n char array s
procedure double: procedure double:
n = n * 2 s = string_join(s,s)
Then you could call x.double() no matter what actual type x was (number or string) and it would know which code to run - this greatly simplifies your code. You can double integer, strings, matrices, complex numbers, reals, window sizes on your monitor and all sorts of different things.
And you're right, a C library can be made to look a little bit like objects. The classic example is stdio.h - you don't ever care what a FILE* actually points to, just the fact that it will behave in a certain way. The FILE*, fopen(), fclose() and other functions are an class of sorts representing the I/O capabilities of C.
You can program procedurally in most OO languages, but the power of OO comes from the ability to inherit, encapsulate and abstract that procedural logic. I think you are correct, a library should look a lot like a class. It should have its own scope and encapsulate logic behind functions with meaningful names.
You are correct in your observation that object-oriented programs are based in many ways on the procedural paradigm. You are also correct in that syntactically all that really happens is that you invoke functions. In fact, you could implement many features of object oriented languages using procedural mechanisms (e.g., function pointers in C++). You could thus do an object-oriented design and still implement it in a procedural language (e.g., like old C++ compilers did).
The importance of the object oriented paradigm is not as much in the language mechanism as it is in the thinking and design process. In procedural programming the thinking is about operations and breaking those operations down using other operations, grouping them into modules, etc. This means that the data or state falls into a secondary importance. It is like thinking of mathematical operations.
The object oriented paradigm, on the other hand, says that you need to think of state and operations together as an entity, and then design your program as interactions between entities that exchange state and activate operations.
IMHO, object oriented programming is a concept that exists at a higher level of abstraction than procedural programming. The two are not mutually exclusive in that individual methods in an OO program look pretty much the same as individual functions in a procedural program. This contrasts with, for example, functional programming, which requires a completely different mindset. Furthermore, you can write procedurally in an OO language by making everything static, etc. You can be a human compiler and write effectively OO code in C by using lots of function pointers and struct pointer casting.
OO, then, is more of a design philosophy and world view than something w/ a rigorous definition. It requires that inheritance, polymorphism, etc. be used as major patterns in structuring your code, and that syntax be provided to make these expressible without resorting to low-level tricks. It requires that you think of code that acts on the state of a collection of data as being a property of the data, not a procedure that exists by itself. It is not black and white. Your code can be "more" or "less" OO depending on how heavily you rely on inheritance, polymorphism, classes and the "methods as a property of data" worldview as a means of structuring and explaining/understanding your code.
OO is mostly a mind set. You can program OO in C (if you really want to ... ), and you can perfectly have procedural code in C++/Java; what I mean is, even if you use classes on the surface, it could still be procedural.
The idea behind OO is abstraction of state. Instead of "thinking" in terms of "groupings of data", you "think" in terms of "objects", where an object is an "interface" for "grouping of data and ways to manipulate this data".
It all sounds philosophical, because it is.
There's a lot to say here, and it can't be all said in a small SO post, so I'll leave it here.
UPDATE As mentioned in Flanagan's answer, OO languages implement constructs that utilize this abstraction.
I mean, you could technically "hack" classes and polymorphism in terms of structs, functions, and function pointers.
[pardon the primer style, it's late and i'm tired]
procedures process data - data in, apply some processing, get data out
sometimes, some of the data elements are related to some of the other data elements, and it's convenient to group them together into a data structure, which can then be manipulated and addressed as a single unit.
now our procedure can take a data structure as input and alter it and/or produce another data structure as output
occasionally we notice that some procedures are only concerned with a certain kind of data structure; it is convenient to group these procedures together with their data structure, and call it an object.
a template for creating objects is called a class; an object is said to be an instance of a class
we may notice that one class is very much like another, so instead of copying and pasting code we let one class inherit from another: the subclass inherits from the superclass or "base class". In this way the subclass has access to all of the data structures and procedures of the superclass, and can augment or override them in certain ways
if we politely request an object to do something for us instead of brutally calling its procedures directly, this is called message passing, even if no actual 'message' is transmitted. The joy here is that many different kinds of objects may understand the same message, which leads to the notion of polymorphism. For example, we can ask many different kinds of documents to Print themselves, and they each respond appropriately.
a language that supports objects (via classes or not) with message passing and inheritance is called object-oriented. If there is no inheritance, the language is merely object-based.
The difference is that objects have procedures and related data in the same place - procedural languages use 'structs' (things that hold related data together) which keeps the data separate from the procedures. Effectively, anything you do in an OO language should be possible in a procedural language with a combination of structs and procedures.
The main difference is the mind set that an OO languages puts programmers in.
Procedural is part of the procedural/functional/logical (or logic oriented) distinction (compare c, lisp, and prolog) between different ways of describing what a program should do.
Object orientation is orthogonal to this other idea, and describes a means of grouping sub-programs with data. C++ and java are procedural languages with object oriented features; fortran77 is a procedural languages without object oriented features. Common lisp supports object orientation; some older lisps do not. Plain vanilla prolog does not support objects, and I can't name a logic oriented language that does (I don't do logic oriented programming, it is on my list of things to do when I have some copious spare time. I barely do functional programming).
As others have noted, however, proper object oriented thinking changes how you do your programming as much as a switch from procedural to functional.
BTW-- I see "procedural" used a lot to distinguish non-object-oriented procedural languages from their object-oriented brethren, but I think this is a poor usage driven by the lack of a clean adjective for "not object oriented". YMMV.
Its easier to understand in context, look at other abstractions introduced between languages.
A key difference between assembly language and a procedural language like C or Pascal is the introduction of the "procedure" abstraction. People writing assembly code create procedures, but its hard and error prone, a procedural language gives you tools to make it easier.
The difference between a procedural language and an OO language like C++ is the "object" abstraction. People who write "c" often create conceptual objects but its difficult and error prone, an OO language gives you tools to make it easier.
Things like Sing# from Microsoft (or Erlang) add the Message/Process abstraction into the language. Sure, you can do message passing and process creation in assembly, C or C++ but Sing# makes it easier.
It all comes down to the same machine code, these abstractions are purely for the benfit of our brains, not the computer.
In a procedural program, you divide a big problem into small problems, and abstract each of these small problems as a procedure. This is called procedural abstraction.
In object oriented programs, you analyse a problem as some objects, and the interaction between objects. This is called object abstraction.
Procedure oriented programming - Gives importance to algorithm rather than data.This way of programming concentrates on procedures i.e methods to perform specific task and share their data structure. It follows Top-down structures.
example : Pascal and C
Object oriented programming - Gives importance to data rather than algorithm. It follows Bottom-up structures.Every thing is viewed as an object. Each object has its own data structure and procedure. It includes features like data hiding, polymorphism,encapsulation and message passing. Users really dont have to bother what inside these objects , while using them in their programs.
C 没有对象,而 C++ 是支持对象的语言。 另一方面,Java 一切都是对象(除了原语)。 一切都是打字的。
线性进展发生在对象内部,但对象本身只是代码和数据的集合。
This is a simplified answer.
In a true OO language, the only procedural coding is done inside of an object.
C has no objects and C++ is a language that supports objects. Java on the other hand everything is an object(except the primitives). Everything is typed.
Linear progression happens inside of the objects but the objects themselves are just collections of code and data.
事实上,库,或者更确切地说是在结构上运行的一组函数,与 C++ 中的对象相同。 事实上C++就是这样实现的。
It depends how you define OOP. In terms of Java-like OOP where you call methods on objects, procedural programming is pretty much the same. As far as I can tell you can emulate all OOP principles (encapsulation, abstraction, polymorphism, inheritance) in a procedural language like C. Proof of this is GObject, to some extend Objective-C, and many other OOP language implementations using C, like cPython. This is done by using structures and operating on those structures using functions:
The interface is very OOP like. It doesn't really allow for polymorphism, but it's also possible. It is very similar to a Python interface, except that the attributes are separate from the "methods":
I chose Python for the example because "self" is explicit, as in the C example. Many OOP languages, like Java, abstract this.
There are also the Smalltalk-like OOP where messages are sent to objects, rather than calling methods on objects. The difference is subtle at first glance, but it provides a lot of power and flexibility. This can also be implemented in procedural-languages, as proven by Objective-C.
Object-oriented programming is not necessarily a type of language, but rather a paradigm. Object-oriented languages such as Java, Python, Ruby, etc, provide syntactic sugar to easily manipulate objects, and this is the main difference between "procedural languages" and "object-oriented languages".
Indeed, a library, or rather a set of functions operating on a structure, is the same as an object in C++. In fact C++ is implemented in just that way.
发布评论
评论(17)
这里已经提到了很多有趣的点。
一种思考方式是,在面向对象中,您有“对象”的概念,“对象”是具有固有特征和行为的事物。 它们通常具有某种公共“接口”,提供一种检索有关它们的一些信息的机制,但对象本身,或者更确切地说是它的“类”,限制了公开可用的信息。 对象的内部结构不会向公众公开,因为通常不需要知道对象“幕后”的肮脏细节。 因此,面向对象的程序利用了这个结构以及其他东西。
过程式编程通常不会将数据和行为耦合到“对象”中。 我以前见过用 C 语言实现的,但它并不漂亮,而且涉及太多的琐事,无法近似使用 C++ 等语言可以实现的功能。
面向对象开发背后的想法之一是,除了您提供的方法之外,我不应该通过任何其他方法来处理您的数据。 如果你只为我提供一个经过深思熟虑的界面,你就可以让我保持诚实。 现在,如果您使用程序方法并向我发送一个没有内置保护的结构,那么我可以随心所欲,如果我愚蠢或邪恶,我可以更改您可能不希望我做的事情改变。
当然,如果你聪明的话,你可以绕过这个物体,但你必须不遗余力地做到这一点。
这并不完整,但它是一方面。
Lots of interesting points already mentioned here.
One way to think about it is that in OO, you have the idea of 'objects' which are things that have characteristics and behaviors inherent to them. They typically have some sort of public 'interface' which provides a mechanism to retrieve some information about them, but the object itself, or rather its 'class', limits what information is publicly available. The innards of the object are not exposed to the public because there's typically no need to know the dirty details 'under the hood' of the object. So object oriented programs utilize this construct, as well as other things.
Procedural programming doesn't typically utilize such a coupling of data and behavior into an 'object'. I've seen it done in C before but it wasn't pretty and involved way too much monkey business to approximate what one could do with, say, C++.
One of the ideas behind object oriented development is that I ought not be able to muck with your data through any means other than the ones that you've provided. If you provide me only a well-thought out interface, you can keep me honest. Now, if you are using a procedural approach and you send me a structure that has no built-in protections, well then I can do as I please and if I'm dumb or evil, I can change things you might not want me to change.
Granted, you can circumvent the object if you are clever but you've got to go out of the way to do this.
This isn't complete, but it is one aspect.
C++ 的实现方式使得面向对象编程看起来很像过程式编程。 你需要稍微改变一下你的想法。
在 C++ 中,对象的方法只是作用于对象的过程。 但在真正的面向对象范式中,您应该将方法视为对象可以接收的潜在消息(即字母)。 对象接收消息(参数代表消息的有效负载,即信件的内容)并根据消息更改其状态。
The way C++ is implemented just makes OO programming look a lot like procedural programming. You need to shift your thinking slightly.
In C++ objects have methods that are just procedures that act on the object. But in a real OO paradiam you should think of the methods as potential messages that the object can recieve (ie letters). The object recieves a message (the parameters represent the payload of the message ie the content of the letter) and changes its state based on the message.
有关过程式和面向对象之间差异的一个相当直观的示例,请尝试学习 Smalltalk。 在 Smalltalk 中,一切,我的意思是一切都是对象。 没有 if 语句或 while 循环。 您可以通过向其他对象发送消息(也称为调用其方法)来实现该功能。 一开始它确实会让您头晕,但我想您很快就会明白 OO 应该是什么。
For a fairly in-your-face example of the difference between procedural and OO, try learning Smalltalk. In Smalltalk, everything, and I mean everything is an object. There are no if-statements or while-loops. You achieve that functionality by sending messages to (a.k.a. invoking methods on) other objects. It really makes your head spin at first, but I think you'll quickly grok what OO is supposed to be.
在过程化程序中,代码为王,数据为次要。 换句话说,您拥有作用于数据的程序,并且它们通常不是紧密绑定的。
在面向对象的世界中,对象是最重要的。 对象由数据和允许对该数据进行操作的代码组成,并且它们之间的联系非常紧密。 这是封装的概念,即信息的隐藏。
举个例子,假设您有一个数字,并且想要将其加倍。 执行此操作的一种程序方法是:
此处的代码非常明确地将 n 乘以 2,并将结果存储回 n。
执行此操作的 OO 方法是向数字对象发送一条“消息”,告诉它将自身加倍:
这样做的优点称为多态性。 当您决定希望能够将“bob”这样的字符串加倍时会发生什么。 在程序世界中,您必须提供更多代码来进行加倍,但您还必须以不同的方式调用该代码。
使用 OO,您可以创建一个字符串对象,它也可以接受“double”消息。 将字符串加倍的代码属于字符串对象,因此它知道它的行为必须与数字对象不同。 如果它确定“bob” * 2 是“bobbob”,则代码将类似于:
然后,无论 x 的实际类型是什么(数字或字符串),您都可以调用 x.double(),并且它会知道要运行哪个代码- 这极大地简化了您的代码。 您可以将整数、字符串、矩阵、复数、实数、显示器上的窗口大小以及各种不同的东西加倍。
你是对的,C 库可以变得有点像对象。 典型的例子是
stdio.h
- 你从来不关心FILE*
实际指向什么,只关心它的行为以某种方式。FILE*
、fopen()
、fclose()
和其他函数是代表 C 语言 I/O 功能的一类。In a procedural program, the code is king and the data is subordinate. In other words, you have programs which act on data and they're not usually tightly bound.
In the OO world, objects are the primary thing of interest. An object consists of data and the code that is allowed to act on that data, and they are very tightly bound. It is the concept of encapsulation, the hiding of information.
An example, let's say you have a number and you want to double it. A procedural way of doing this is:
The code here quite explicitly multiplies n by 2 and stores the result back into n.
The OO way of doing this is to send a "message" to the number object telling it to double itself:
The advantage of this is called polymorphism. What happens when you decide you want to be able to double a string like "bob". In the procedural world, you'd have to provide more code to do the doubling but you'd also have to call that code differently.
With OO, you create a string object which can also take the 'double' message. The code to double a string belongs to the string object so it knows it has to act differently to the number object. If it decided that "bob" * 2 was "bobbob", the code would look something like:
Then you could call x.double() no matter what actual type x was (number or string) and it would know which code to run - this greatly simplifies your code. You can double integer, strings, matrices, complex numbers, reals, window sizes on your monitor and all sorts of different things.
And you're right, a C library can be made to look a little bit like objects. The classic example is
stdio.h
- you don't ever care what aFILE*
actually points to, just the fact that it will behave in a certain way. TheFILE*
,fopen()
,fclose()
and other functions are an class of sorts representing the I/O capabilities of C.您可以使用大多数 OO 语言进行过程式编程,但 OO 的强大之处在于继承、封装和抽象过程逻辑的能力。
我认为你是对的,图书馆应该看起来很像一个类。 它应该有自己的作用域,并用有意义的名称封装函数后面的逻辑。
You can program procedurally in most OO languages, but the power of OO comes from the ability to inherit, encapsulate and abstract that procedural logic.
I think you are correct, a library should look a lot like a class. It should have its own scope and encapsulate logic behind functions with meaningful names.
您的观察是正确的,即面向对象的程序在很多方面都基于过程范例。 从语法上讲,您也是正确的,真正发生的只是您调用函数。 事实上,您可以使用过程机制(例如,C++ 中的函数指针)来实现面向对象语言的许多功能。 因此,您可以进行面向对象的设计,并且仍然以过程语言实现它(例如,像旧的 C++ 编译器那样)。
面向对象范式的重要性不在于语言机制,而在于思维和设计过程。 在过程编程中,思维是关于操作并使用其他操作分解这些操作,将它们分组为模块等。这意味着数据或状态处于次要地位。 这就像思考数学运算。
另一方面,面向对象范例表示,您需要将状态和操作视为一个实体,然后将程序设计为交换状态和激活操作的实体之间的交互。
You are correct in your observation that object-oriented programs are based in many ways on the procedural paradigm. You are also correct in that syntactically all that really happens is that you invoke functions. In fact, you could implement many features of object oriented languages using procedural mechanisms (e.g., function pointers in C++). You could thus do an object-oriented design and still implement it in a procedural language (e.g., like old C++ compilers did).
The importance of the object oriented paradigm is not as much in the language mechanism as it is in the thinking and design process. In procedural programming the thinking is about operations and breaking those operations down using other operations, grouping them into modules, etc. This means that the data or state falls into a secondary importance. It is like thinking of mathematical operations.
The object oriented paradigm, on the other hand, says that you need to think of state and operations together as an entity, and then design your program as interactions between entities that exchange state and activate operations.
两者之间的差异虽然微妙,但意义重大。
在过程程序中,模块通过读取和写入存储在共享数据结构中的状态进行交互。
在面向对象的程序中,对象形式的模块通过向其他对象发送消息来进行交互。
The difference between the two is subtle but significant.
In a procedural program, modules interact by reading and writing state that is stored in shared data structures.
In an object oriented program, modules in the form of objects interact by sending messages to other objects.
恕我直言,面向对象编程是一个比过程编程更高抽象层次的概念。 两者并不相互排斥,因为面向对象程序中的各个方法看起来与过程程序中的各个函数几乎相同。 这与函数式编程形成鲜明对比,函数式编程需要完全不同的思维方式。 此外,您可以通过将所有内容设为静态等,以面向对象语言进行程序性编写。您可以成为一名人工编译器,并通过使用大量函数指针和结构指针转换,以 C 语言编写有效的面向对象代码。
那么,面向对象更多的是一种设计哲学和世界观,而不是有严格定义的东西。 它要求将继承、多态性等用作构建代码的主要模式,并提供语法以使这些模式无需诉诸低级技巧即可表达。 它要求您将作用于数据集合状态的代码视为数据的属性,而不是本身存在的过程。 它不是黑白的。 您的代码可以“更多”或“更少”面向对象,具体取决于您对继承、多态性、类以及“方法作为数据属性”世界观作为构建和解释/理解代码的手段的依赖程度。
IMHO, object oriented programming is a concept that exists at a higher level of abstraction than procedural programming. The two are not mutually exclusive in that individual methods in an OO program look pretty much the same as individual functions in a procedural program. This contrasts with, for example, functional programming, which requires a completely different mindset. Furthermore, you can write procedurally in an OO language by making everything static, etc. You can be a human compiler and write effectively OO code in C by using lots of function pointers and struct pointer casting.
OO, then, is more of a design philosophy and world view than something w/ a rigorous definition. It requires that inheritance, polymorphism, etc. be used as major patterns in structuring your code, and that syntax be provided to make these expressible without resorting to low-level tricks. It requires that you think of code that acts on the state of a collection of data as being a property of the data, not a procedure that exists by itself. It is not black and white. Your code can be "more" or "less" OO depending on how heavily you rely on inheritance, polymorphism, classes and the "methods as a property of data" worldview as a means of structuring and explaining/understanding your code.
OO 主要是一种心态。 你可以用C语言编写面向对象的程序(如果你真的想……),并且你可以完美地用C++/Java编写程序代码; 我的意思是,即使表面上使用类,它仍然可能是过程性的。
OO 背后的思想是状态抽象。 您不是根据“数据分组”来“思考”,而是根据“对象”来“思考”,其中对象是“数据分组以及操作该数据的方法”的“接口”。
这一切听起来都很有哲理,因为确实如此。
这里有很多话要说,不可能在一篇小小的 SO 帖子中全部说完,所以我将其留在这里。
更新
正如弗拉纳根的回答,OO 语言实现了利用这种抽象的结构。
我的意思是,从技术上讲,您可以在结构、函数和函数指针方面“破解”类和多态性。
下面是 C 中的 OO 的示例
OO is mostly a mind set. You can program OO in C (if you really want to ... ), and you can perfectly have procedural code in C++/Java; what I mean is, even if you use classes on the surface, it could still be procedural.
The idea behind OO is abstraction of state. Instead of "thinking" in terms of "groupings of data", you "think" in terms of "objects", where an object is an "interface" for "grouping of data and ways to manipulate this data".
It all sounds philosophical, because it is.
There's a lot to say here, and it can't be all said in a small SO post, so I'll leave it here.
UPDATE
As mentioned in Flanagan's answer, OO languages implement constructs that utilize this abstraction.
I mean, you could technically "hack" classes and polymorphism in terms of structs, functions, and function pointers.
Here's an example of OO in C
[请原谅初级风格,已经晚了,我累了]
程序处理数据 - 数据输入,应用一些处理,有时取出数据
,一些数据元素与其他一些数据元素相关,并且很方便分组它们一起形成一个数据结构,然后可以将其作为一个单元进行操作和寻址。
现在,我们的程序可以将数据结构作为输入并更改它和/或生成另一个数据结构作为输出,
有时我们会注意到某些程序只涉及某种数据结构; 将这些过程及其数据结构组合在一起,并将其称为对象是很方便的。
创建对象的模板称为类; 对象被称为类的实例,
我们可能会注意到一个类与另一个类非常相似,因此我们让一个类继承,而不是复制和粘贴代码另一种:子类继承自超类或“基类”。 通过这种方式,子类可以访问超类的所有数据结构和过程,并且可以以某些方式增强或重写它们,
如果我们礼貌地请求一个对象为我们做某事,而不是粗暴地直接调用其过程,这称为消息传递,即使没有传输实际的“消息”。 这里的乐趣在于许多不同类型的对象可以理解相同的消息,这导致了多态性的概念。 例如,我们可以要求许多不同类型的文档自行打印,并且它们各自做出适当的响应。
通过消息传递和继承支持对象(通过类或不通过类)的语言称为面向对象的语言。 如果没有继承,则该语言仅仅是基于对象的。
祝你学习顺利!
[pardon the primer style, it's late and i'm tired]
procedures process data - data in, apply some processing, get data out
sometimes, some of the data elements are related to some of the other data elements, and it's convenient to group them together into a data structure, which can then be manipulated and addressed as a single unit.
now our procedure can take a data structure as input and alter it and/or produce another data structure as output
occasionally we notice that some procedures are only concerned with a certain kind of data structure; it is convenient to group these procedures together with their data structure, and call it an object.
a template for creating objects is called a class; an object is said to be an instance of a class
we may notice that one class is very much like another, so instead of copying and pasting code we let one class inherit from another: the subclass inherits from the superclass or "base class". In this way the subclass has access to all of the data structures and procedures of the superclass, and can augment or override them in certain ways
if we politely request an object to do something for us instead of brutally calling its procedures directly, this is called message passing, even if no actual 'message' is transmitted. The joy here is that many different kinds of objects may understand the same message, which leads to the notion of polymorphism. For example, we can ask many different kinds of documents to Print themselves, and they each respond appropriately.
a language that supports objects (via classes or not) with message passing and inheritance is called object-oriented. If there is no inheritance, the language is merely object-based.
good luck with your studies!
不同之处在于,对象在同一位置具有过程和相关数据 - 过程语言使用“结构”(将相关数据保存在一起的东西),将数据与过程分开。 实际上,您在面向对象语言中所做的任何事情都应该可以在具有结构和过程组合的过程语言中实现。
主要区别在于 OO 语言给程序员带来的思维定势。
The difference is that objects have procedures and related data in the same place - procedural languages use 'structs' (things that hold related data together) which keeps the data separate from the procedures. Effectively, anything you do in an OO language should be possible in a procedural language with a combination of structs and procedures.
The main difference is the mind set that an OO languages puts programmers in.
过程是描述程序应该做什么的不同方式之间的过程/功能/逻辑(或面向逻辑)区别(比较 c、lisp 和 prolog)的一部分。
面向对象与另一个想法正交,它描述了一种将子程序与数据分组的方法。 C++和java是具有面向对象特性的过程语言; fortran77 是一种没有面向对象功能的过程语言。 Common Lisp 支持面向对象; 一些较旧的 lisp 则没有。 普通的 prolog 不支持对象,而且我无法命名一种支持对象的面向逻辑的语言(我不进行面向逻辑的编程,当我有一些东西时,它就在我要做的事情列表中)我几乎没有时间做函数式编程)。
然而,正如其他人所指出的,正确的面向对象思维会改变您的编程方式,就像从过程式到函数式的转变一样。
顺便说一句,我看到“过程”经常被用来区分非面向对象的过程语言和面向对象的兄弟,但我认为这是一种糟糕的用法,因为缺乏“非面向对象”的干净形容词。 YMMV。
Procedural is part of the procedural/functional/logical (or logic oriented) distinction (compare c, lisp, and prolog) between different ways of describing what a program should do.
Object orientation is orthogonal to this other idea, and describes a means of grouping sub-programs with data. C++ and java are procedural languages with object oriented features; fortran77 is a procedural languages without object oriented features. Common lisp supports object orientation; some older lisps do not. Plain vanilla prolog does not support objects, and I can't name a logic oriented language that does (I don't do logic oriented programming, it is on my list of things to do when I have some copious spare time. I barely do functional programming).
As others have noted, however, proper object oriented thinking changes how you do your programming as much as a switch from procedural to functional.
BTW-- I see "procedural" used a lot to distinguish non-object-oriented procedural languages from their object-oriented brethren, but I think this is a poor usage driven by the lack of a clean adjective for "not object oriented". YMMV.
在上下文中更容易理解,看看语言之间引入的其他抽象。
汇编语言与 C 或 Pascal 等过程语言之间的一个关键区别是“过程”抽象的引入。 编写汇编代码的人创建过程,但它很困难且容易出错,过程语言为您提供了使其变得更容易的工具。
过程语言和面向对象语言(如 C++)之间的区别在于“对象”抽象。 写“c”的人经常创建概念对象但它很困难并且容易出错,面向对象语言为您提供了使事情变得更容易的工具。
像 Microsoft(或 Erlang)的 Sing# 这样的东西将消息/进程抽象添加到语言。 当然,您可以在汇编、C 或 C++ 中进行消息传递和进程创建,但 Sing# 让它变得更容易。
这一切都归结为相同的机器代码,这些抽象纯粹是为了我们的大脑而不是计算机的利益。
Its easier to understand in context, look at other abstractions introduced between languages.
A key difference between assembly language and a procedural language like C or Pascal is the introduction of the "procedure" abstraction. People writing assembly code create procedures, but its hard and error prone, a procedural language gives you tools to make it easier.
The difference between a procedural language and an OO language like C++ is the "object" abstraction. People who write "c" often create conceptual objects but its difficult and error prone, an OO language gives you tools to make it easier.
Things like Sing# from Microsoft (or Erlang) add the Message/Process abstraction into the language. Sure, you can do message passing and process creation in assembly, C or C++ but Sing# makes it easier.
It all comes down to the same machine code, these abstractions are purely for the benfit of our brains, not the computer.
在过程式程序中,您将一个大问题分成几个小问题,并将每个小问题抽象为一个过程。 这称为过程抽象。
在面向对象的程序中,您将问题分析为一些对象以及对象之间的交互。 这称为对象抽象。
In a procedural program, you divide a big problem into small problems, and abstract each of these small problems as a procedure. This is called procedural abstraction.
In object oriented programs, you analyse a problem as some objects, and the interaction between objects. This is called object abstraction.
不同之处
在于面向过程的编程 - 重视算法而不是数据。这种编程方式集中于过程,即执行特定任务并共享其数据结构的方法。 它遵循自上而下的结构。
示例:Pascal 和 C
面向对象编程 - 重视数据而不是算法。 它遵循自下而上的结构。每件事物都被视为一个对象。 每个对象都有自己的数据结构和过程。 它包括数据隐藏、多态性、封装和消息传递等功能。 用户在程序中使用这些对象时实际上不必关心这些对象的内部内容。
示例:C++ 和 Java
The difference is
Procedure oriented programming - Gives importance to algorithm rather than data.This way of programming concentrates on procedures i.e methods to perform specific task and share their data structure. It follows Top-down structures.
example : Pascal and C
Object oriented programming - Gives importance to data rather than algorithm. It follows Bottom-up structures.Every thing is viewed as an object. Each object has its own data structure and procedure. It includes features like data hiding, polymorphism,encapsulation and message passing. Users really dont have to bother what inside these objects , while using them in their programs.
example : C++ and Java
这是一个简化的答案。
在真正的面向对象语言中,唯一的过程编码是在对象内部完成的。
C 没有对象,而 C++ 是支持对象的语言。 另一方面,Java 一切都是对象(除了原语)。 一切都是打字的。
This is a simplified answer.
In a true OO language, the only procedural coding is done inside of an object.
C has no objects and C++ is a language that supports objects. Java on the other hand everything is an object(except the primitives). Everything is typed.
这取决于你如何定义 OOP。 就类似于 Java 的 OOP(在对象上调用方法)而言,过程式编程几乎是相同的。 据我所知,你可以用像 C 这样的过程语言来模拟所有 OOP 原则(封装、抽象、多态性、继承)。证明是 GObject,在某种程度上是 Objective-C,以及许多其他使用 C 的 OOP 语言实现,例如 cPython。 这是通过使用结构并使用函数对这些结构进行操作来完成的:
该接口非常像 OOP。 它实际上不允许多态性,但也是可能的。 它与 Python 接口非常相似,只是属性与“方法”是分开的:
我选择 Python 作为示例,因为“self”是显式的,如 C 示例中所示。 许多 OOP 语言(例如 Java)对此进行了抽象。
还有类似 Smalltalk 的 OOP,其中消息被发送到对象,而不是调用对象上的方法。 乍一看,差异很微妙,但它提供了很大的功能和灵活性。 正如 Objective-C 所证明的那样,这也可以用过程语言来实现。
面向对象编程不一定是一种语言,而是一种范式。 Java、Python、Ruby等面向对象语言提供了语法糖来方便地操作对象,这是“过程语言”和“面向对象语言”之间的主要区别。
事实上,库,或者更确切地说是在结构上运行的一组函数,与 C++ 中的对象相同。 事实上C++就是这样实现的。
It depends how you define OOP. In terms of Java-like OOP where you call methods on objects, procedural programming is pretty much the same. As far as I can tell you can emulate all OOP principles (encapsulation, abstraction, polymorphism, inheritance) in a procedural language like C. Proof of this is GObject, to some extend Objective-C, and many other OOP language implementations using C, like cPython. This is done by using structures and operating on those structures using functions:
The interface is very OOP like. It doesn't really allow for polymorphism, but it's also possible. It is very similar to a Python interface, except that the attributes are separate from the "methods":
I chose Python for the example because "self" is explicit, as in the C example. Many OOP languages, like Java, abstract this.
There are also the Smalltalk-like OOP where messages are sent to objects, rather than calling methods on objects. The difference is subtle at first glance, but it provides a lot of power and flexibility. This can also be implemented in procedural-languages, as proven by Objective-C.
Object-oriented programming is not necessarily a type of language, but rather a paradigm. Object-oriented languages such as Java, Python, Ruby, etc, provide syntactic sugar to easily manipulate objects, and this is the main difference between "procedural languages" and "object-oriented languages".
Indeed, a library, or rather a set of functions operating on a structure, is the same as an object in C++. In fact C++ is implemented in just that way.