What's the difference between a procedural pro

2019-01-30 02:31发布

I'm fairly new to programming but I've been reading some interesting discussions on StackOverflow about various programming approaches. I'm still not 100% clear on what the difference is between procedural programming and object oriented programming. It sounds like object oriented programming still uses procedures (methods) but everything is organized differently because the object is the star of the show. But it seems to me that procedures still allow you to do all of the same things. Like in C, you can put all of your similar procedures into a library. So couldn't you really say that a library in C is similar to an object in C++?

17条回答
虎瘦雄心在
2楼-- · 2019-01-30 02:59

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.
查看更多
看我几分像从前
3楼-- · 2019-01-30 02:59

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:

typedef struct {
    Object *isa;
    String *name;
    Date *birthday;
} Person;

Person *Person_new();
String *Person_name(Person *self);
void Person_setName(Person *self, String *newName);
// ...

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":

class Person(object):
    def __init__(self):
        self._name = ""
        self._age = datetime.datetime.now()

    @property
    def name(self):
        return self._name

    @property
    def age(self):
        return self._age

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.

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-30 03:00

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.

查看更多
Deceive 欺骗
5楼-- · 2019-01-30 03:03

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.

查看更多
老娘就宠你
6楼-- · 2019-01-30 03:06

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.

查看更多
够拽才男人
7楼-- · 2019-01-30 03:08

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.

查看更多
登录 后发表回答