是什么让语言成为面向对象的?

发布于 2024-07-04 14:14:47 字数 240 浏览 11 评论 0原文

由于没有有意义的术语的辩论是毫无意义,我想我应该指出房间并问:到底是什么使语言“面向对象”? 我在这里并不是在寻找教科书上的答案,而是根据您使用在您的领域(无论是什么领域)运行良好的 OO 语言的经验得出的答案。

可能有助于首先回答的一个相关问题是:面向对象语言的原型是什么?为什么?

Since debate without meaningful terms is meaningless, I figured I would point at the elephant in the room and ask: What exactly makes a language "object-oriented"? I'm not looking for a textbook answer here, but one based on your experiences with OO languages that work well in your domain, whatever it may be.

A related question that might help to answer first is: What is the archetype of object-oriented languages and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

愁以何悠 2024-07-11 14:14:48

真正的面向对象并不是语言,而是代码。

可以编写面向对象的 C 代码(如果您愿意,可以使用结构体甚至函数指针成员),并且我已经看到了一些非常好的示例。 (我想到了 Quake 2/3 SDK。)用 C++ 编写过程(即非 OO)代码也绝对是可能的。

鉴于此,我想说正是该语言对编写良好的 OO 代码的支持使其成为“面向对象的语言”。 例如,对于普通的成员函数,我永远不会在 C 的结构中使用函数指针成员; 因此我会说C不是OO语言。

(扩展这一点,人们可以说 Python 也不是面向对象的,每一步都强制使用“self”引用,并且构造函数称为 init 等等;但这是一种宗教讨论。)

It's not really the languages that are OO, it's the code.

It is possible to write object-oriented C code (with structs and even function pointer members, if you wish) and I have seen some pretty good examples of it. (Quake 2/3 SDK comes to mind.) It is also definitely possible to write procedural (i.e. non-OO) code in C++.

Given that, I'd say it's the language's support for writing good OO code that makes it an "Object Oriented Language." I would never bother with using function pointer members in structs in C, for example, for what would be ordinary member functions; therefore I will say that C is not an OO language.

(Expanding on this, one could say that Python is not object oriented, either, with the mandatory "self" reference on every step and constructors called init, whatnot; but that's a Religious Discussion.)

醉南桥 2024-07-11 14:14:48

原型

用代码表达现实世界场景的能力。

foreach(House house in location.Houses)
{
 foreach(Deliverable mail in new Mailbag(new Deliverable[]
              {
              GetLetters(), 
              GetPackages(), 
              GetAdvertisingJunk()
              })
 {
    if(mail.AddressedTo(house))
    {
        house.Deliver(mail);
    }
 }
}

-

foreach(Deliverable myMail in GetMail())
{
    IReadable readable = myMail as IReadable;
    if ( readable != null )
    {
        Console.WriteLine(readable.Text);
    }
}

为什么?

帮助我们更容易地理解这一点。 它在我们的头脑中更有意义,如果正确实现,会使代码更加高效、可重用并减少重复。

要实现这一点,您需要:

  • 指针/引用以确保 this == this 且 this != that。
  • 指向(例如Arm),存储数据(int hairyness)和操作(Throw(IThrowable))
  • 多态性(继承和/或接口)以处理特定对象一种通用的方式,这样你就可以看书,也可以在墙上涂鸦(两者都实现了 IReadable)
  • 封装,因为苹果不会公开 Atoms[] 属性

Archetype

The ability to express real-world scenarios in code.

foreach(House house in location.Houses)
{
 foreach(Deliverable mail in new Mailbag(new Deliverable[]
              {
              GetLetters(), 
              GetPackages(), 
              GetAdvertisingJunk()
              })
 {
    if(mail.AddressedTo(house))
    {
        house.Deliver(mail);
    }
 }
}

-

foreach(Deliverable myMail in GetMail())
{
    IReadable readable = myMail as IReadable;
    if ( readable != null )
    {
        Console.WriteLine(readable.Text);
    }
}

Why?

To help us understand this more easily. It makes better sense in our heads and if implemented correctly makes the code more efficient, re-usable and reduces repetition.

To achieve this you need:

  • Pointers/References to ensure that this == this and this != that.
  • Classes to point to (e.g. Arm) that store data (int hairyness) and operations (Throw(IThrowable))
  • Polymorphism (Inheritance and/or Interfaces) to treat specific objects in a generic fashion so you can read books as well as graffiti on the wall (both implement IReadable)
  • Encapsulation because an apple doesn't expose an Atoms[] property
聽兲甴掵 2024-07-11 14:14:48

如果一种语言的设计具有专门支持面向对象编程的功能(4 个功能),那么它就是一种面向对象的编程语言。

  • 您可以在更多情况下以面向对象的风格进行编程或更少的任何语言。面向对象的是代码而不是语言。

  • 真正的面向对象语言的例子有 Java、c#、Python、Ruby、C++。
    此外,还可以通过扩展来提供面向对象的功能,例如 PHP、Perl 等。

  • 您可以使用 C 编写面向对象的代码,但它不是面向对象的编程。 郎。 它不是为此而设计的(这就是 c++ 的全部意义)

If a language is designed with the facilities specifically to support object-oriented programming(4 features) then it is an Object-oriented programming language.

  • You can program in an object-orientated style in more or less any language.It’s the code that is object-oriented not the language.

  • Examples of real object-oriented languages are Java, c#, Python, Ruby, C++.
    Also, it's possible to have extensions to provide Object-Oriented features like PHP, Perl etc.

  • You can write an object-oriented code with C but it is not object-oriented prog. lang. It is not designed for that (that was the whole point of c++)

醉态萌生 2024-07-11 14:14:48

对象:对象是数据的存储库。 例如,如果 MyList 是 ShoppingList 对象,则 MyList 可能会记录您的购物清单。

类:类是对象的一种类型。 同一类的多个对象可能存在; 例如,MyList 和 YourList 可能都是 ShoppingList 对象。

方法:对对象或类进行操作的过程或函数。 方法与特定的类相关联。 例如,addItem 可能是将项目添加到任何 ShoppingList 对象的方法。 有时,一个方法与一系列类相关联。 例如,addItem 可以对任何列表进行操作,而 ShoppingList 只是其中的一种类型。

继承:类可以从更通用的类继承属性。 例如,ShoppingList 类从 List 类继承存储项目序列的属性。

多态性:使一个方法调用适用于多个不同类的对象的能力,即使这些类需要方法调用的不同实现。 例如,一行代码可能能够在每种列表上调用“addItem”方法,即使将项目添加到 ShoppingList 与将项目添加到 ShoppingCart 完全不同。

面向对象:每个对象都知道自己的类以及操作该类中对象的方法。 每个 ShoppingList 和每个 ShoppingCart 都知道 addItem 的哪个实现适用于它。

在此列表中,真正区分面向对象语言与过程语言(C、Fortran、Basic、Pascal)的一件事是多态性。

来源:https://www.youtube.com/观看?v=mFPmKGIrQs4&list=PL-XXv-cvA_iAlnI-BQr9hjqADPBtujFJd

Object: An object is a repository of data. For example, if MyList is a ShoppingList object, MyList might record your shopping list.

Class: A class is a type of object. Many objects of the same class might exist; for instance, MyList and YourList may both be ShoppingList objects.

Method: A procedure or function that operates on an object or a class. A method is associated with a particular class. For instance, addItem might be a method that adds an item to any ShoppingList object. Sometimes a method is associated with a family of classes. For instance, addItem might operate on any List, of which a ShoppingList is just one type.

Inheritance: A class may inherit properties from a more general class. For example, the ShoppingList class inherits from the List class the property of storing a sequence of items.

Polymorphism: The ability to have one method call work on several different classes of objects, even if those classes need different implementations of the method call. For example, one line of code might be able to call the "addItem" method on every kind of List, even though adding an item to a ShoppingList is completely different from adding an item to a ShoppingCart.

Object-Oriented: Each object knows its own class and which methods manipulate objects in that class. Each ShoppingList and each ShoppingCart knows which implementation of addItem applies to it.

In this list, the one thing that truly distinguishes object-oriented languages from procedural languages (C, Fortran, Basic, Pascal) is polymorphism.

Source: https://www.youtube.com/watch?v=mFPmKGIrQs4&list=PL-XXv-cvA_iAlnI-BQr9hjqADPBtujFJd

软糖 2024-07-11 14:14:48

简单来说:(比较保险性)

1-多态性
2-继承
3-封装
4-重复使用。
:)

Simples:(compare insurance character)

1-Polymorphism
2-Inheritance
3-Encapsulation
4-Re-use.
:)

无边思念无边月 2024-07-11 14:14:48

当你可以创建类时,它就是面向对象的
例如:java是面向对象的,javascript不是,而c++看起来像是某种“好奇对象”语言

when you can make classes, it is object-oriented
for example : java is object-oriented, javascript is not, and c++ looks like some kind of "object-curious" language

月牙弯弯 2024-07-11 14:14:48

根据我的经验,语言不是面向对象的,代码是。

几年前,当我开始理解面向对象时,我正在用 AppleScript 编写一套程序,它并没有真正强制执行任何面向对象的功能。 用 AppleScript 编写对象是很笨拙的,尽管如果您花时间弄清楚如何创建类、构造函数等是可以的。

该语言是该领域的正确语言:让 Macintosh 上的不同程序协同工作,根据输入文件完成一些自动任务。 不厌其烦地自我实施面向对象的风格是正确的编程选择,因为它会使代码更容易排除故障、测试和理解。

在将代码从过程式更改为面向对象的过程中,我最注意到的功能是封装:属性和方法调用。

In my experience, languages are not object-oriented, code is.

A few years ago I was writing a suite of programs in AppleScript, which doesn't really enforce any object-oriented features, when I started to grok OO. It's clumsy to write Objects in AppleScript, although it is possible to create classes, constructors, and so forth if you take the time to figure out how.

The language was the correct language for the domain: getting different programs on the Macintosh to work together to accomplish some automatic tasks based on input files. Taking the trouble to self-enforce an object-oriented style was the correct programming choice because it resulted in code that was easier to trouble-shoot, test, and understand.

The feature that I noticed the most in changing that code over from procedural to OO was encapsulation: both of properties and method calls.

野味少女 2024-07-11 14:14:48

为了进一步说明 aib 所说的,我想说一种语言并不是真正面向对象的,除非可用的标准库是面向对象的。 最大的例子是 PHP。 尽管它支持所有标准的面向对象概念,但事实上,很大一部分标准库不是面向对象的,这意味着几乎不可能以面向对象的方式编写代码。

如果所有标准库仍然要求您在所有函数调用前加上 mysql_ 和 pgsql_ 之类的前缀,那么它们引入命名空间并不重要,而在实际 API 中支持命名空间的语言中,您可以使用以下命令摆脱函数: mysql_ 并在文件顶部有一个简单的“include system.db.mysql.*”,以便它知道这些东西来自哪里。

To further what aib said, I would say that a language isn't really object oriented unless the standard libraries that are available are object oriented. The biggest example of this is PHP. Although it supports all the standard object oriented concepts, the fact that such a large percentage of the standard libraries aren't object oriented means that it's almost impossible to write your code in an object oriented way.

It doesn't matter that they are introducing namespaces if all the standard libraries still require you to prefix all your function calls with stuff like mysql_ and pgsql_, when in a language that supported namespaces in the actual API, you could get rid of functions with mysql_ and have just a simple "include system.db.mysql.*" at the top of your file so that it would know where those things came from.

温柔嚣张 2024-07-11 14:14:48

不考虑理论含义,它似乎是

“任何具有名为‘class’的关键字的语言”:-P

Disregarding the theoretical implications, it seems to be

"Any language that has a keyword called 'class'" :-P

猥︴琐丶欲为 2024-07-11 14:14:48

支持类、方法、属性、封装、数据隐藏、继承、多态、抽象……?

Supports classes, methods, attributes, encapsulation, data hiding, inheritance, polymorphism, abstraction...?

抠脚大汉 2024-07-11 14:14:48

据我所知,使语言成为“面向对象”的主要观点是支持数据分组的思想以及处理该数据的方法,这通常是通过类、模块、继承、多态性等来实现的。

请参阅此讨论,了解人们对面向对象的看法(认为?)的概述。

至于“原型”OO 语言——正如 Kristopher 指出的那样,这确实是 Smalltalk。

As far as I can tell, the main view of what makes a language "Object Oriented" is supporting the idea of grouping data, and methods that work on that data, which is generally achieved through classes, modules, inheritance, polymorphism, etc.

See this discussion for an overview of what people think (thought?) Object-Orientation means.

As for the "archetypal" OO language - that is indeed Smalltalk, as Kristopher pointed out.

层林尽染 2024-07-11 14:14:48

我很高兴与大家分享这个,这对我来说非常有趣并且很有帮助。 这是 1994 年滚石杂志采访的摘录,其中 Steve(不是程序员)用简单的术语解释了 OOP。

Jeff Goodell:您能否简单地解释一下什么是面向对象软件?

史蒂夫·乔布斯:物体就像人一样。 它们是活生生的、会呼吸的生物,它们内部有关于如何做事情的知识,并且内部有记忆,因此它们可以记住事情。 您不是在非常低的级别与它们交互,而是在非常高的抽象级别与它们交互,就像我们在这里所做的那样。

举个例子:如果我是你的洗衣对象,你可以把你的脏衣服给我,然后给我发一条消息,说:“请你帮我洗一下衣服好吗?” 我碰巧知道旧金山最好的洗衣店在哪里。 我会说英语,而且我口袋里有美元。 于是我出去叫了一辆出租车,告诉司机带我去旧金山的这个地方。 我去把你的衣服洗了,我跳回出租车,我回到这里。 我给你干净的衣服并说:“这是你的干净的衣服。”

你不知道我是怎么做到的。 你对洗衣房一无所知。 也许你会说法语,但你甚至无法叫到出租车。 你付不起钱,你口袋里没有钱。 然而,我知道如何做到这一切。 而且你不必知道其中任何事情。 所有这些复杂性都隐藏在我的内心,我们能够在非常高的抽象级别上进行交互。 这就是对象。 它们封装了复杂性,并且该复杂性的接口是高级别的。

I am happy to share this with you guys, it was quite interesting and helpful to me. This is an extract from a 1994 Rolling Stone interview where Steve (not a programmer) explains OOP in simple terms.

Jeff Goodell: Would you explain, in simple terms, exactly what object-oriented software is?

Steve Jobs: Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.

Here’s an example: If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please.” I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”

You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket. Yet, I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.

惯饮孤独 2024-07-11 14:14:48

Smalltalk 通常被认为是原型的 OO 语言,尽管 Simula 经常被认为是第一个 OO 语言。

当前的 OO 语言可以根据借用最多概念的语言进行粗略分类:

  • 类 Smalltalk:Ruby、类 Objective-C
  • Simula:C++、Object Pascal、Java、C#

Smalltalk is usually considered the archetypal OO language, although Simula is often cited as the first OO language.

Current OO languages can be loosely categorized by which language they borrow the most concepts from:

  • Smalltalk-like: Ruby, Objective-C
  • Simula-like: C++, Object Pascal, Java, C#
左岸枫 2024-07-11 14:14:48

基本上,面向对象实际上可以归结为“消息传递”。

在过程语言中,我调用这样的函数:

  f(x)

并且名称 f 可能在编译时绑定到特定的代码块。 (除非这是一种具有高阶函数或函数指针的过程语言,但让我们暂时忽略这种可能性。)因此,这行代码只能表示一个明确的事情。

在面向对象的语言中,我将消息传递给对象,也许像这样:

 o.m(x) 

在这种情况下。 m 不是代码块的名称,而是一个“方法选择器”,调用哪个代码块实际上在某种程度上取决于对象 o。 这行代码比较模糊或笼统,因为它在不同情况下可能意味着不同的东西,具体取决于 o。

在大多数OO语言中,对象o有一个“类”,类决定调用哪个代码块。 在一些面向对象语言(最著名的是 Javascript)中,o 没有类,但有在运行时直接附加到它的方法,或者从原型继承它们。

我的划分是,类和继承都不是面向对象语言所必需的。 但这种消息的多态处理是必不可少的。

尽管您可以使用 C 语言中的函数指针来伪造这一点,但这不足以将 C 称为面向对象语言,因为您必须实现自己的基础结构。 你可以做到这一点,并且 OO 风格是可能的,但语言还没有给你它。

Basically Object Oriented really boils down to "message passing"

In a procedural language, I call a function like this :

  f(x)

And the name f is probably bound to a particular block of code at compile time. (Unless this is a procedural language with higher order functions or pointers to functions, but lets ignore that possibility for a second.) So this line of code can only mean one unambiguous thing.

In an object oriented language I pass a message to an object, perhaps like this :

 o.m(x) 

In this case. m is not the name of a block of code, but a "method selector" and which block of code gets called actually depends on the object o in some way. This line of code is more ambiguous or general because it can mean different things in different situations, depending on o.

In the majority of OO languages, the object o has a "class", and the class determines which block of code is called. In a couple of OO languages (most famously, Javascript) o doesn't have a class, but has methods directly attached to it at runtime, or has inherited them from a prototype.

My demarcation is that neither classes nor inheritance are necessary for a language to be OO. But this polymorphic handling of messages is essential.

Although you can fake this with function pointers in say C, that's not sufficient for C to be called an OO language, because you're going to have to implement your own infrastructure. You can do that, and a OO style is possible, but the language hasn't given it to you.

风铃鹿 2024-07-11 14:14:48

根据 Booch 的说法,以下要素:
主要:

  • 抽象
  • 封装
  • 模块化
  • 层次结构(继承)

次要:

  • 类型
  • 并发持久

According to Booch, the following elements:
Major:

  • Abstraction
  • Encapsulation
  • Modularity
  • Hierarchy (Inheritance)

Minor:

  • Typing
  • Concurrency
  • Persistence
苏别ゝ 2024-07-11 14:14:48

面向对象的定义 当然是 大量蠕虫,但这是我的 2 美分

:对我来说,面向对象就是通过发送消息进行协作的对象。 对我来说,这是面向对象语言最重要的一个特征。

如果我必须列出面向对象语言必须具备的所有功能的有序列表,它将如下所示:

  1. 对象向其他对象发送消息
  2. 一切都是对象
  3. 后期绑定
  4. 子类型多态
  5. 继承或类似的表达方式,例如 委托
  6. 封装
  7. 信息隐藏
  8. 抽象

显然,这个列表是非常有争议的,因为它排除了广泛使用的多种语言。视为面向对象,如 JavaC#C++,所有这些都违反了第 1、2 和 3 点。但是,毫无疑问,这些语言允许面向对象编程(但 < a href="http://Open-Std.Org/jtc1/sc22/wg14/" rel="noreferrer" title="C 编程语言">C),甚至促进它(C 没有)。 因此,我将满足这些要求的语言称为“纯粹面向对象”。

作为典型的面向对象语言,我将命名为 SelfNewspeak

两者均满足上述要求。 两者都受到 Smalltalk 的启发,并且都是其继承者,并且两者实际上都设法“更加面向对象”在某种意义上。 我喜欢 Self 和新话的一点是,它们都将消息发送范式发挥到了极致(新话甚至比 Self 更重要)。

在新话中,一切都是消息发送。 没有实例变量,没有字段,没有属性,没有常量,没有类名。 它们都是通过使用 getter 和 setter 来模拟的。

在 Self 中,没有类,只有对象。 这强调了 OO 的真正含义:对象,而不是类。

Definitions for Object-Orientation are of course a huge can of worms, but here are my 2 cents:

To me, Object-Orientation is all about objects that collaborate by sending messages. That is, to me, the single most important trait of an object-oriented language.

If I had to put up an ordered list of all the features that an object-oriented language must have, it would look like this:

  1. Objects sending messages to other objects
  2. Everything is an Object
  3. Late Binding
  4. Subtype Polymorphism
  5. Inheritance or something similarly expressive, like Delegation
  6. Encapsulation
  7. Information Hiding
  8. Abstraction

Obviously, this list is very controversial, since it excludes a great variety of languages that are widely regarded as object-oriented, such as Java, C# and C++, all of which violate points 1, 2 and 3. However, there is no doubt that those languages allow for object-oriented programming (but so does C) and even facilitate it (which C doesn't). So, I have come to call languages that satisfy those requirements "purely object-oriented".

As archetypical object-oriented languages I would name Self and Newspeak.

Both satisfy the above-mentioned requirements. Both are inspired by and successors to Smalltalk, and both actually manage to be "more OO" in some sense. The things that I like about Self and Newspeak are that both take the message sending paradigm to the extreme (Newspeak even more so than Self).

In Newspeak, everything is a message send. There are no instance variables, no fields, no attributes, no constants, no class names. They are all emulated by using getters and setters.

In Self, there are no classes, only objects. This emphasizes, what OO is really about: objects, not classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文