This caught my attention last night.
昨晚引起了我的注意。
On the latest ALT.NET Podcast Scott Bellware discusses how as opposed to Ruby, languages like c#, java et al. are not truly object oriented rather opting for the phrase "class-oriented". They talk about this distinction in very vague terms without going into much detail or discussing the pros and cons much.
在最新的ALT.NET播客中,Scott Bellware讨论了与Ruby相反的方式,如c#,java等语言。不是真正的面向对象而是选择“以类为导向”这一短语。他们用非常含糊的术语谈论这种区别,而没有详细讨论或讨论利弊。
What is the real difference here and how much does it matter? What are other languages then are "object-oriented"? It sounded pretty interesting but I don't want to have to learn Ruby just to know what if anything I am missing.
这里有什么真正的区别,它有多重要?那么其他什么语言是“面向对象的”?这听起来很有意思,但我不想学习Ruby只是为了知道如果我缺少什么。
Update: After reading some of the answers below it seems like people generally agree that the reference is to duck-typing. What I'm not sure I understand still though is the claim that this ultimately changes all that much. Especially if you are already doing proper tdd with loose coupling blah blah blah. Can someone show me an example of a wonderous thing I could do with ruby that I cannot do with c# and that exemplifies this different oop approach?
更新:阅读下面的一些答案后,似乎人们普遍认为参考是鸭子打字。我不确定我理解的是,这最终会发生变化。特别是如果你已经做了适当的tdd与松散耦合等等等等等等。有人能告诉我一个我可以用红宝石做的奇妙事情的例子,我不能用c#做这个并且这例证了这种不同的oop方法吗?
14 个解决方案
#1
15
The duck typing comments here are more attributing to the fact that Ruby and Python are more dynamic than C#. It doesn't really have anything to do with it's OO Nature.
这里的鸭子输入注释更多地归因于Ruby和Python比C#更具动态性。它与OO Nature没有任何关系。
What (I think) Bellware meant by that is that in Ruby, everything is an object. Even a class. A class definition is an instance of an object. As such, you can add/change/remove behavior to it at runtime.
我认为Bellware的意思是在Ruby中,一切都是对象。即使是上课。类定义是对象的实例。因此,您可以在运行时向其添加/更改/删除行为。
Another good example is that NULL is an object as well. In ruby, everything is LITERALLY an object. Having such deep OO in it's entire being allows for some fun meta-programming techniques such as method_missing.
另一个很好的例子是NULL也是一个对象。在红宝石中,一切都是一个对象。在它的整个存在中拥有如此深的OO允许一些有趣的元编程技术,例如method_missing。
#2
23
In an object-oriented language, objects are defined by defining objects rather than classes, although classes can provide some useful templates for specific, cookie-cutter definitions of a given abstraction. In a class-oriented language, like C# for example, objects must be defined by classes, and these templates are usually canned and packaged and made immutable before runtime. This arbitrary constraint that objects must be defined before runtime and that the definitions of objects are immutable is not an object-oriented concept; it's class oriented.
在面向对象语言中,对象是通过定义对象而不是类来定义的,尽管类可以为给定抽象的特定cookie切割器定义提供一些有用的模板。在面向类的语言中,例如C#,对象必须由类定义,并且这些模板通常在运行时被封装和打包并变为不可变。必须在运行时之前定义对象并且对象的定义是不可变的这种任意约束不是面向对象的概念;它是面向班级的。
#3
10
IMO, it's really overly defining "object-oriented", but what they are referring to is that Ruby, unlike C#, C++, Java, et al, does not make use of defining a class -- you really only ever work directly with objects. Conversely, in C# for example, you define classes that you then must instantiate into object by way of the new keyword. The key point being you must declare a class in C# or describe it. Additionally, in Ruby, everything -- even numbers, for example -- is an object. In contrast, C# still retains the concept of an object type and a value type. This in fact, I think illustrates the point they make about C# and other similar languages -- object type and value type imply a type system, meaning you have an entire system of describing types as opposed to just working with objects.
IMO,它真的过度定义“面向对象”,但他们所指的是Ruby,不像C#,C ++,Java等,不使用定义类 - 你真的只是直接使用对象。相反,在C#中,您可以定义必须通过new关键字实例化为对象的类。关键是你必须在C#中声明一个类或描述它。另外,在Ruby中,所有东西 - 例如偶数 - 都是一个对象。相比之下,C#仍然保留了对象类型和值类型的概念。事实上,我认为这说明了他们对C#和其他类似语言的观点 - 对象类型和值类型意味着一个类型系统,这意味着你有一个描述类型的整个系统而不是仅仅使用对象。
Conceptually, I think OO design is what provides the abstraction for use to deal complexity in software systems these days. The language is a tool use to implement an OO design -- some make it more natural than others. I would still argue that from a more common and broader definition, C# and the others are still object-oriented languages.
从概念上讲,我认为OO设计提供了用于处理软件系统复杂性的抽象概念。该语言是实现OO设计的工具 - 有些使其比其他设计更自然。我仍然认为,从更常见和更广泛的定义来看,C#和其他仍然是面向对象的语言。
#4
9
There are three pillars of OOP
OOP有三个支柱
- Encapsulation
- Inheritance
- Polymorphism
If a language can do those three things it is a OOP language.
如果一种语言可以做这三件事,那么它就是一种OOP语言。
I am pretty sure the argument of language X does OOP better than language A will go on forever.
我很确定语言X的论点比语言A更好地完成OOP会永远存在。
#5
4
OO is sometimes defined as message oriented. The idea is that a method call (or property access) is really a message sent to another object. How the recieveing object handles the message is completely encapsulated. Often the message corresponds to a method which is then executed, but that is just an implementation detail. You can for example create a catch-all handler which is executed regardless of the method name in the message.
OO有时被定义为面向消息。这个想法是方法调用(或属性访问)实际上是发送给另一个对象的消息。接收对象如何处理消息是完全封装的。通常,消息对应于随后执行的方法,但这只是一个实现细节。例如,您可以创建一个catch-all处理程序,无论消息中的方法名称如何,都会执行该处理程序。
Static OO like in C# does not have this kind of encapsulation. A massage has to correspond to an existing method or property, otherwise the compiler will complain. Dynamic languages like Smalltalk, Ruby or Python does however support "message-based" OO.
像C#中的静态OO没有这种封装。按摩必须与现有方法或属性相对应,否则编译器会抱怨。然而,Smalltalk,Ruby或Python等动态语言支持“基于消息”的OO。
So in this sense C# and other statically typed OO languages are not true OO, sine thay lack "true" encapsulation.
所以从这个意义上来说,C#和其他静态类型的OO语言并不是真正的OO,因为缺乏“真正的”封装。
#6
3
Update: Its the new wave.. which suggest everything that we've been doing till now is passe.. Seems to be propping up quite a bit in podcasts and books.. Maybe this is what you heard.
更新:它的新浪潮......这表明我们到目前为止所做的一切都是过去的..似乎在播客和书籍中支撑了很多..也许这就是你所听到的。
Till now we've been concerned with static classes and not unleashed the power of object oriented development. We've been doing 'class based dev.' Classes are fixed/static templates to create objects. All objects of a class are created equal.
到目前为止,我们一直关注静态类,而不是释放面向对象开发的力量。我们一直在做'基于阶级的开发'。类是固定/静态模板以创建对象。类的所有对象都是相同的。
e.g. Just to illustrate what I've been babbling about... let me borrow a Ruby code snippet from PragProg screencast I just had the privilege of watching. 'Prototype based development' blurs the line between objects and classes.. there is no difference.
例如只是为了说明我一直在讨厌的问题......让我从PragProg的截屏视频中借用一段Ruby代码片段,我有权观看。 “基于原型的开发”模糊了对象和类之间的界限......没有区别。
animal = Object.new # create a new instance of base Object
def animal.number_of_feet=(feet) # adding new methods to an Object instance. What?
@number_of_feet = feet
end
def animal.number_of_feet
@number_of_feet
end
cat = animal.clone #inherits 'number_of_feet' behavior from animal
cat.number_of_feet = 4
felix = cat.clone #inherits state of '4' and behavior from cat
puts felix.number_of_feet # outputs 4
The idea being its a more powerful way to inherit state and behavior than traditional class based inheritance. It gives you more flexibility and control in certain "special" scenarios (that I've yet to fathom). This allows things like Mix-ins (re using behavior without class inheritance)..
这个想法是一种比传统的基于类的继承更强大的继承状态和行为的方法。它在某些“特殊”场景(我还没有想到)中为您提供更多的灵活性和控制力。这允许像Mix-ins这样的东西(重新使用没有类继承的行为)。
By challenging the basic primitives of how we think about problems, 'true OOP' is like 'the Matrix' in a way... You keep going WTF in a loop. Like this one.. where the base class of Container can be either an Array or a Hash based on which side of 0.5 the random number generated is.
通过挑战我们如何思考问题的基本原语,“真正的OOP”在某种程度上就像“矩阵”......你继续循环使用WTF。像这一个..其中Container的基类可以是数组,也可以是基于0.5的生成随机数的哈希值。
class Container < (rand < 0.5 ? Array : Hash)
end
Ruby, javascript and the new brigade seem to be the ones pioneering this. I'm still out on this one... reading up and trying to make sense of this new phenomenon. Seems to be powerful.. too powerful.. Useful? I need my eyes opened a bit more. Interesting times.. these.
Ruby,javascript和新旅似乎是开创这一点的人。我仍然在这一个......阅读并试图理解这一新现象。似乎是强大的...太强大了..有用吗?我需要更多地睁开眼睛。有趣的时代..这些。
#7
2
I've only listened to the first 6-7 minutes of the podcast that sparked your question. If their intent is to say that C# isn't a purely object-oriented language, that's actually correct. Everything in C# isn't an object (at least the primitives aren't, though boxing creates an object containing the same value). In Ruby, everything is an object. Daren and Ben seem to have covered all the bases in their discussion of "duck-typing", so I won't repeat it.
我只是听了引发你问题的播客的前6-7分钟。如果他们的意图是说C#不是纯粹面向对象的语言,那实际上是正确的。 C#中的所有东西都不是对象(至少原语不是,虽然装箱会创建一个包含相同值的对象)。在Ruby中,一切都是对象。达伦和本似乎已经涵盖了他们讨论“鸭子打字”的所有基础,所以我不会重复它。
Whether or not this difference (everything an object versus everything not an object) is material/significant is a question I can't readily answer because I don't have sufficient depth in Ruby to compare it to C#. Those of you who on here who know Smalltalk (I don't, though I wish I did) have probably been looking at the Ruby movement with some amusement since it was the first pure OO language 30 years ago.
这种差异(对象与所有对象都不是一个对象)是否具有实质性/重要性是一个我无法回答的问题,因为我没有足够的深度来将Ruby与C#进行比较。那些在这里知道Smalltalk的人(我不知道,虽然我希望我这样做)可能一直在寻找Ruby运动,因为它是30年前第一个纯粹的OO语言。
#8
2
Maybe they are alluding to the difference between duck typing and class hierarchies?
也许他们暗指鸭子打字和类层次结构之间的区别?
if it walks like a duck and quacks like a duck, just pretend it's a duck and kick it.
如果它像鸭子一样走路,像鸭子一样嘎嘎叫,就假装它是一只鸭子并踢它。
In C#, Java etc. the compiler fusses a lot about: Are you allowed to do this operation on that object?
在C#,Java等中,编译器有很多关于:你是否允许在该对象上执行此操作?
Object Oriented vs. Class Oriented could therefore mean: Does the language worry about objects or classes?
因此,面向对象与面向类可能意味着:语言是否担心对象或类?
For instance: In Python, to implement an iterable object, you only need to supply a method __iter__()
that returns an object that has a method named next()
. That's all there is to it: No interface implementation (there is no such thing). No subclassing. Just talking like a duck / iterator.
例如:在Python中,要实现可迭代对象,您只需提供一个方法__iter __(),该方法返回一个具有名为next()的方法的对象。这就是它的全部内容:没有接口实现(没有这样的东西)。没有子类化。就像鸭子/迭代器一样说话。
EDIT: This post was upvoted while I rewrote everything. Sorry, won't ever do that again. The original content included advice to learn as many languages as possible and to nary worry about what the language doctors think / say about a language.
编辑:当我重写所有内容时,这篇文章被赞成。对不起,再也不会这样了。原始内容包括尽可能多地学习语言的建议,并且不必担心医生对语言的看法/说法。
#9
1
That was an abstract-podcast indeed!
But I see what they're getting at - they just dazzled by Ruby Sparkle. Ruby allows you to do things that C-based and Java programmers wouldn't even think of + combinations of those things let you achieve undreamt of possibilities. Adding new methods to a built-in String class coz you feel like it, passing around unnamed blocks of code for others to execute, mixins... Conventional folks are not used to objects changing too far from the class template. Its a whole new world out there for sure..
那确实是一个抽象播客!但我看到了他们所得到的东西 - 他们只是被Ruby Sparkle迷惑了。 Ruby允许你做一些基于C语言和Java程序员甚至都不会想到的东西+这些东西的组合让你实现不可思议的可能性。将新方法添加到内置的String类中,就像它一样,传递未命名的代码块以供其他人执行,mixins ...传统的人不习惯于更改类模板太远的对象。它肯定是一个全新的世界..
As for the C# guys not being OO enough... dont take it to heart.. Just take it as the stuff you speak when you are flabbergasted for words. Ruby does that to most people.
If I had to recommend one language for people to learn in the current decade.. it would be Ruby. I'm glad I did.. Although some people may claim Python. But its like my opinion.. man! :D
至于C#家伙不够OO ......不要把它放在心上..当你大惊小怪的时候,把它作为你说话的东西。 Ruby为大多数人做到了这一点。如果我不得不推荐一种语言供人们在当前的十年中学习......那就是Ruby。我很高兴我做过..虽然有些人可能会说Python。但它像我的意见..男人! :d
#10
1
I don't think this is specifically about duck typing. For instance C# supports limited duck-typing already - an example would be that you can use foreach on any class that implements MoveNext and Current.
我不认为这是关于鸭子打字的具体内容。例如,C#已经支持有限的鸭子类型 - 例如,你可以在任何实现MoveNext和Current的类上使用foreach。
The concept of duck-typing is compatible with statically typed languages like Java and C#, it's basically an extension of reflection.
duck-typing的概念与Java和C#等静态类型语言兼容,它基本上是反射的扩展。
This is really the case of static vs dynamic typing. Both are proper-OO, in as much as there is such a thing. Outside of academia it's really not worth debating.
这实际上是静态与动态类型的情况。两者都是正确的OO,就像有这样的东西一样。在学术界之外,真的不值得辩论。
Rubbish code can be written in either. Great code can be written in either. There's absolutely nothing functional that one model can do that the other can't.
垃圾代码可以用任何一种编写。伟大的代码可以写入其中。一个模型完全没有功能,而另一个模型无法做到。
The real difference is in the nature of the coding done. Static types reduce freedom, but the advantage is that everyone knows what they're dealing with. The opportunity to change instances on the fly is very powerful, but the cost is that it becomes hard to know what you're deaing with.
真正的区别在于编码的性质。静态类型减少了*,但优点是每个人都知道他们正在处理什么。动态更改实例的机会非常强大,但成本是很难知道你正在使用什么。
For instance for Java or C# intellisense is easy - the IDE can quickly produce a drop list of possibilities. For Javascript or Ruby this becomes a lot harder.
例如,对于Java或C#,intellisense很容易 - IDE可以快速生成一系列可能性。对于Javascript或Ruby来说,这变得更加困难。
For certain things, for instance producing an API that someone else will code with, there is a real advantage in static typing. For others, for instance rapidly producing prototypes, the advantage goes to dynamic.
对于某些事情,例如生成其他人将编码的API,静态类型有一个真正的优势。对于其他人,例如快速生产原型,其优势在于动态。
It's worth having an understanding of both in your skills toolbox, but nowhere near as important as understanding the one you already use in real depth.
值得在你的技能工具箱中理解这两者,但远不如理解你已经在真实深度使用的那个重要。
#11
1
Object Oriented is a concept. This concept is based upon certain ideas. The technical names of these ideas (actually rather principles that evolved over the time and have not been there from the first hour) have already been given above, I'm not going to repeat them. I'm rather explaining this as simple and non-technical as I can.
面向对象是一个概念。这个概念基于某些想法。这些想法的技术名称(实际上是在一段时间内发展并且从第一个小时起就没有进化过的原则)已经在上面给出了,我不打算重复它们。我宁愿把它解释为尽可能简单和非技术性。
The idea of OO programming is that there are objects. Objects are small independent entities. These entities may have embedded information or they may not. If they have such information, only the entity itself can access it or change it. The entities communicate with each other by sending messages between each other. Compare this to human beings. Human beings are independent entities, having internal data stored in their brain and the interact with each other by communicating (e.g. talking to each other). If you need knowledge from someone's else brain, you cannot directly access it, you must ask him a question and he may answer that to you, telling you what you wanted to know.
OO编程的想法是有对象。对象是小的独立实体。这些实体可能嵌入了信息,也可能没有。如果他们有这样的信息,只有实体本身可以访问或更改它。实体通过在彼此之间发送消息来彼此通信。将此与人类进行比较。人类是独立的实体,其内部数据存储在他们的大脑中并且通过交流(例如彼此交谈)彼此交互。如果你需要某人的大脑知识,你不能直接访问它,你必须问他一个问题,他可以回答你,告诉你你想知道什么。
And that's basically it. This is real idea behind OO programming. Writing these entities, define the communication between them and have them interact together to form an application. This concept is not bound to any language. It's just a concept and if you write your code in C#, Java, or Ruby, that is not important. With some extra work this concept can even be done in pure C, even though it is a functional language but it offers everything you need for the concept.
这基本上就是这样。这是OO编程背后的真实想法。编写这些实体,定义它们之间的通信并让它们一起交互以形成应用程序。这个概念不受任何语言的约束。这只是一个概念,如果你用C#,Java或Ruby编写代码,那就不重要了。通过一些额外的工作,这个概念甚至可以在纯C中完成,即使它是一种功能语言,但它提供了概念所需的一切。
Different languages have now adopted this concept of OO programming and of course the concepts are not always equal. Some languages allow what other languages forbid, for example. Now one of the concepts that involved is the concept of classes. Some languages have classes, some don't. A class is a blueprint how an object looks like. It defines the internal data storage of an object, it defines the messages an object can understand and if there is inheritance (which is not mandatory for OO programming!), classes also defines from which other class (or classes if multiple inheritance is allowed) this class inherits (and which properties if selective inheritance exists). Once you created such a blueprint you can now generate an unlimited amount of objects build according to this blueprint.
不同的语言现在采用了OO编程的概念,当然概念并不总是相同的。例如,某些语言允许其他语言禁止。现在涉及的概念之一是类的概念。有些语言有类,有些则没有。类是对象的外观蓝图。它定义了一个对象的内部数据存储,它定义了一个对象可以理解的消息,如果有继承(这对于OO编程来说不是必需的!),类还定义了从哪个其他类(或多个继承允许的类)此类继承(以及存在选择性继承时的属性)。创建这样的蓝图后,您现在可以根据此蓝图生成无限量的对象构建。
There are OO languages that have no classes, though. How are objects then build? Well, usually dynamically. E.g. you can create a new blank object and then dynamically add internal structure like instance variables or methods (messages) to it. Or you can duplicate an already existing object, with all its properties and then modify it. Or possibly merge two objects into a new one. Unlike class based languages these languages are very dynamic, as you can generate objects dynamically during runtime in ways not even you the developer has thought about when starting writing the code.
但是,有些OO语言没有课程。然后如何构建对象?嗯,通常是动态的。例如。您可以创建一个新的空白对象,然后动态添加内部结构,如实例变量或方法(消息)。或者,您可以复制已存在的对象及其所有属性,然后对其进行修改。或者可能将两个对象合并为一个新对象。与基于类的语言不同,这些语言非常动态,因为您可以在运行时动态生成对象,甚至开发人员在开始编写代码时也没有考虑过。
Usually this dynamic has a price: The more dynamic a language is the more memory (RAM) objects will waste and the slower everything gets as program flow is extremely dynamically as well and it's hard for a compiler to generate effective code if it has no chance to predict code or data flow. JIT compilers can optimize some parts of that during runtime, once they know the program flow, however as these languages are so dynamically, program flow can change at any time, forcing the JIT to throw away all compilation results and re-compile the same code over and over again.
通常这种动态有一个代价:语言越动态,内存(RAM)对象就越浪费,一切都变得越慢,因为程序流程也非常动态,如果没有机会,编译器很难生成有效的代码预测代码或数据流。 JIT编译器一旦知道程序流就可以在运行时优化它的某些部分,但是由于这些语言是动态的,程序流可以随时改变,迫使JIT抛弃所有编译结果并重新编译相同的代码一遍又一遍地。
But this is a tiny implementation detail - it has nothing to do with the basic OO principle. It is nowhere said that objects need to be dynamic or must be alterable during runtime. The Wikipedia says it pretty well:
但这是一个很小的实现细节 - 它与基本的OO原则无关。没有人说对象需要是动态的,或者必须在运行时可以改变。*说得很好:
Programming techniques may include features such as information hiding, data abstraction, encapsulation, modularity, polymorphism, and inheritance.
编程技术可以包括诸如信息隐藏,数据抽象,封装,模块化,多态和继承之类的特征。
http://en.wikipedia.org/wiki/Object-oriented_programming
They may or they may not. This is all not mandatory. Mandatory is only the presence of objects and that they must have ways to interact with each other (otherwise objects would be pretty useless if they cannot interact with each other).
他们可能或他们可能没有。这不是强制性的。强制性只是对象的存在,并且它们必须具有相互交互的方式(否则,如果它们不能相互交互,则对象将毫无用处)。
#12
1
You asked: "Can someone show me an example of a wonderous thing I could do with ruby that I cannot do with c# and that exemplifies this different oop approach?"
你问道:“有人能告诉我一个我可以用红宝石做的奇妙事情的例子,我不能用c#来做,这就是这种不同的oop方法的例证吗?”
One good example is active record, the ORM built into rails. The model classes are dynamically built at runtime, based on the database schema.
一个很好的例子是活动记录,ORM内置于rails中。模型类是在运行时根据数据库模式动态构建的。
#13
0
This is really probably getting down to what these people see others doing in c# and java as opposed to c# and java supporting OOP. Most languages cane be used in different programming paradigms. For example, you can write procedural code in c# and scheme, and you can do functional-style programming in java. It is more about what you are trying to do and what the language supports.
这实际上可能是这些人看到别人在c#和java中所做的事情,而不是c#和java支持OOP。大多数语言都可以用于不同的编程范例。例如,您可以用c#和scheme编写过程代码,并且可以在java中进行函数式编程。它更多地是关于您尝试做什么以及语言支持的内容。
#14
0
I'll take a stab at this.
我会捅这个。
Python and Ruby are duck-typed. To generate any maintainable code in these languages, you pretty much have to use test driven development. As such, it is very important for a developer to easily inject dependencies into their code without having to create a giant supporting framework.
Python和Ruby是鸭子类型。要生成这些语言中的任何可维护代码,您几乎必须使用测试驱动开发。因此,开发人员可以轻松地将依赖项注入其代码中,而无需创建巨大的支持框架。
Successful dependency-injection depends upon on having a pretty good object model. The two are sort of two sides of the same coin. If you really understand how to use OOP, then you should by default create designs where dependencies can be easily injected.
成功的依赖注入依赖于具有非常好的对象模型。这两个是同一枚硬币的两面。如果您真的了解如何使用OOP,那么您应该默认创建可以轻松注入依赖项的设计。
Because dependency injection is easier in dynamically typed languages, the Ruby/Python developers feel like their language understands the lessons of OO much better than other statically typed counterparts.
因为依赖注入在动态类型语言中更容易,所以Ruby / Python开发人员觉得他们的语言比其他静态类型的同行更能理解OO的教训。
#1
15
The duck typing comments here are more attributing to the fact that Ruby and Python are more dynamic than C#. It doesn't really have anything to do with it's OO Nature.
这里的鸭子输入注释更多地归因于Ruby和Python比C#更具动态性。它与OO Nature没有任何关系。
What (I think) Bellware meant by that is that in Ruby, everything is an object. Even a class. A class definition is an instance of an object. As such, you can add/change/remove behavior to it at runtime.
我认为Bellware的意思是在Ruby中,一切都是对象。即使是上课。类定义是对象的实例。因此,您可以在运行时向其添加/更改/删除行为。
Another good example is that NULL is an object as well. In ruby, everything is LITERALLY an object. Having such deep OO in it's entire being allows for some fun meta-programming techniques such as method_missing.
另一个很好的例子是NULL也是一个对象。在红宝石中,一切都是一个对象。在它的整个存在中拥有如此深的OO允许一些有趣的元编程技术,例如method_missing。
#2
23
In an object-oriented language, objects are defined by defining objects rather than classes, although classes can provide some useful templates for specific, cookie-cutter definitions of a given abstraction. In a class-oriented language, like C# for example, objects must be defined by classes, and these templates are usually canned and packaged and made immutable before runtime. This arbitrary constraint that objects must be defined before runtime and that the definitions of objects are immutable is not an object-oriented concept; it's class oriented.
在面向对象语言中,对象是通过定义对象而不是类来定义的,尽管类可以为给定抽象的特定cookie切割器定义提供一些有用的模板。在面向类的语言中,例如C#,对象必须由类定义,并且这些模板通常在运行时被封装和打包并变为不可变。必须在运行时之前定义对象并且对象的定义是不可变的这种任意约束不是面向对象的概念;它是面向班级的。
#3
10
IMO, it's really overly defining "object-oriented", but what they are referring to is that Ruby, unlike C#, C++, Java, et al, does not make use of defining a class -- you really only ever work directly with objects. Conversely, in C# for example, you define classes that you then must instantiate into object by way of the new keyword. The key point being you must declare a class in C# or describe it. Additionally, in Ruby, everything -- even numbers, for example -- is an object. In contrast, C# still retains the concept of an object type and a value type. This in fact, I think illustrates the point they make about C# and other similar languages -- object type and value type imply a type system, meaning you have an entire system of describing types as opposed to just working with objects.
IMO,它真的过度定义“面向对象”,但他们所指的是Ruby,不像C#,C ++,Java等,不使用定义类 - 你真的只是直接使用对象。相反,在C#中,您可以定义必须通过new关键字实例化为对象的类。关键是你必须在C#中声明一个类或描述它。另外,在Ruby中,所有东西 - 例如偶数 - 都是一个对象。相比之下,C#仍然保留了对象类型和值类型的概念。事实上,我认为这说明了他们对C#和其他类似语言的观点 - 对象类型和值类型意味着一个类型系统,这意味着你有一个描述类型的整个系统而不是仅仅使用对象。
Conceptually, I think OO design is what provides the abstraction for use to deal complexity in software systems these days. The language is a tool use to implement an OO design -- some make it more natural than others. I would still argue that from a more common and broader definition, C# and the others are still object-oriented languages.
从概念上讲,我认为OO设计提供了用于处理软件系统复杂性的抽象概念。该语言是实现OO设计的工具 - 有些使其比其他设计更自然。我仍然认为,从更常见和更广泛的定义来看,C#和其他仍然是面向对象的语言。
#4
9
There are three pillars of OOP
OOP有三个支柱
- Encapsulation
- Inheritance
- Polymorphism
If a language can do those three things it is a OOP language.
如果一种语言可以做这三件事,那么它就是一种OOP语言。
I am pretty sure the argument of language X does OOP better than language A will go on forever.
我很确定语言X的论点比语言A更好地完成OOP会永远存在。
#5
4
OO is sometimes defined as message oriented. The idea is that a method call (or property access) is really a message sent to another object. How the recieveing object handles the message is completely encapsulated. Often the message corresponds to a method which is then executed, but that is just an implementation detail. You can for example create a catch-all handler which is executed regardless of the method name in the message.
OO有时被定义为面向消息。这个想法是方法调用(或属性访问)实际上是发送给另一个对象的消息。接收对象如何处理消息是完全封装的。通常,消息对应于随后执行的方法,但这只是一个实现细节。例如,您可以创建一个catch-all处理程序,无论消息中的方法名称如何,都会执行该处理程序。
Static OO like in C# does not have this kind of encapsulation. A massage has to correspond to an existing method or property, otherwise the compiler will complain. Dynamic languages like Smalltalk, Ruby or Python does however support "message-based" OO.
像C#中的静态OO没有这种封装。按摩必须与现有方法或属性相对应,否则编译器会抱怨。然而,Smalltalk,Ruby或Python等动态语言支持“基于消息”的OO。
So in this sense C# and other statically typed OO languages are not true OO, sine thay lack "true" encapsulation.
所以从这个意义上来说,C#和其他静态类型的OO语言并不是真正的OO,因为缺乏“真正的”封装。
#6
3
Update: Its the new wave.. which suggest everything that we've been doing till now is passe.. Seems to be propping up quite a bit in podcasts and books.. Maybe this is what you heard.
更新:它的新浪潮......这表明我们到目前为止所做的一切都是过去的..似乎在播客和书籍中支撑了很多..也许这就是你所听到的。
Till now we've been concerned with static classes and not unleashed the power of object oriented development. We've been doing 'class based dev.' Classes are fixed/static templates to create objects. All objects of a class are created equal.
到目前为止,我们一直关注静态类,而不是释放面向对象开发的力量。我们一直在做'基于阶级的开发'。类是固定/静态模板以创建对象。类的所有对象都是相同的。
e.g. Just to illustrate what I've been babbling about... let me borrow a Ruby code snippet from PragProg screencast I just had the privilege of watching. 'Prototype based development' blurs the line between objects and classes.. there is no difference.
例如只是为了说明我一直在讨厌的问题......让我从PragProg的截屏视频中借用一段Ruby代码片段,我有权观看。 “基于原型的开发”模糊了对象和类之间的界限......没有区别。
animal = Object.new # create a new instance of base Object
def animal.number_of_feet=(feet) # adding new methods to an Object instance. What?
@number_of_feet = feet
end
def animal.number_of_feet
@number_of_feet
end
cat = animal.clone #inherits 'number_of_feet' behavior from animal
cat.number_of_feet = 4
felix = cat.clone #inherits state of '4' and behavior from cat
puts felix.number_of_feet # outputs 4
The idea being its a more powerful way to inherit state and behavior than traditional class based inheritance. It gives you more flexibility and control in certain "special" scenarios (that I've yet to fathom). This allows things like Mix-ins (re using behavior without class inheritance)..
这个想法是一种比传统的基于类的继承更强大的继承状态和行为的方法。它在某些“特殊”场景(我还没有想到)中为您提供更多的灵活性和控制力。这允许像Mix-ins这样的东西(重新使用没有类继承的行为)。
By challenging the basic primitives of how we think about problems, 'true OOP' is like 'the Matrix' in a way... You keep going WTF in a loop. Like this one.. where the base class of Container can be either an Array or a Hash based on which side of 0.5 the random number generated is.
通过挑战我们如何思考问题的基本原语,“真正的OOP”在某种程度上就像“矩阵”......你继续循环使用WTF。像这一个..其中Container的基类可以是数组,也可以是基于0.5的生成随机数的哈希值。
class Container < (rand < 0.5 ? Array : Hash)
end
Ruby, javascript and the new brigade seem to be the ones pioneering this. I'm still out on this one... reading up and trying to make sense of this new phenomenon. Seems to be powerful.. too powerful.. Useful? I need my eyes opened a bit more. Interesting times.. these.
Ruby,javascript和新旅似乎是开创这一点的人。我仍然在这一个......阅读并试图理解这一新现象。似乎是强大的...太强大了..有用吗?我需要更多地睁开眼睛。有趣的时代..这些。
#7
2
I've only listened to the first 6-7 minutes of the podcast that sparked your question. If their intent is to say that C# isn't a purely object-oriented language, that's actually correct. Everything in C# isn't an object (at least the primitives aren't, though boxing creates an object containing the same value). In Ruby, everything is an object. Daren and Ben seem to have covered all the bases in their discussion of "duck-typing", so I won't repeat it.
我只是听了引发你问题的播客的前6-7分钟。如果他们的意图是说C#不是纯粹面向对象的语言,那实际上是正确的。 C#中的所有东西都不是对象(至少原语不是,虽然装箱会创建一个包含相同值的对象)。在Ruby中,一切都是对象。达伦和本似乎已经涵盖了他们讨论“鸭子打字”的所有基础,所以我不会重复它。
Whether or not this difference (everything an object versus everything not an object) is material/significant is a question I can't readily answer because I don't have sufficient depth in Ruby to compare it to C#. Those of you who on here who know Smalltalk (I don't, though I wish I did) have probably been looking at the Ruby movement with some amusement since it was the first pure OO language 30 years ago.
这种差异(对象与所有对象都不是一个对象)是否具有实质性/重要性是一个我无法回答的问题,因为我没有足够的深度来将Ruby与C#进行比较。那些在这里知道Smalltalk的人(我不知道,虽然我希望我这样做)可能一直在寻找Ruby运动,因为它是30年前第一个纯粹的OO语言。
#8
2
Maybe they are alluding to the difference between duck typing and class hierarchies?
也许他们暗指鸭子打字和类层次结构之间的区别?
if it walks like a duck and quacks like a duck, just pretend it's a duck and kick it.
如果它像鸭子一样走路,像鸭子一样嘎嘎叫,就假装它是一只鸭子并踢它。
In C#, Java etc. the compiler fusses a lot about: Are you allowed to do this operation on that object?
在C#,Java等中,编译器有很多关于:你是否允许在该对象上执行此操作?
Object Oriented vs. Class Oriented could therefore mean: Does the language worry about objects or classes?
因此,面向对象与面向类可能意味着:语言是否担心对象或类?
For instance: In Python, to implement an iterable object, you only need to supply a method __iter__()
that returns an object that has a method named next()
. That's all there is to it: No interface implementation (there is no such thing). No subclassing. Just talking like a duck / iterator.
例如:在Python中,要实现可迭代对象,您只需提供一个方法__iter __(),该方法返回一个具有名为next()的方法的对象。这就是它的全部内容:没有接口实现(没有这样的东西)。没有子类化。就像鸭子/迭代器一样说话。
EDIT: This post was upvoted while I rewrote everything. Sorry, won't ever do that again. The original content included advice to learn as many languages as possible and to nary worry about what the language doctors think / say about a language.
编辑:当我重写所有内容时,这篇文章被赞成。对不起,再也不会这样了。原始内容包括尽可能多地学习语言的建议,并且不必担心医生对语言的看法/说法。
#9
1
That was an abstract-podcast indeed!
But I see what they're getting at - they just dazzled by Ruby Sparkle. Ruby allows you to do things that C-based and Java programmers wouldn't even think of + combinations of those things let you achieve undreamt of possibilities. Adding new methods to a built-in String class coz you feel like it, passing around unnamed blocks of code for others to execute, mixins... Conventional folks are not used to objects changing too far from the class template. Its a whole new world out there for sure..
那确实是一个抽象播客!但我看到了他们所得到的东西 - 他们只是被Ruby Sparkle迷惑了。 Ruby允许你做一些基于C语言和Java程序员甚至都不会想到的东西+这些东西的组合让你实现不可思议的可能性。将新方法添加到内置的String类中,就像它一样,传递未命名的代码块以供其他人执行,mixins ...传统的人不习惯于更改类模板太远的对象。它肯定是一个全新的世界..
As for the C# guys not being OO enough... dont take it to heart.. Just take it as the stuff you speak when you are flabbergasted for words. Ruby does that to most people.
If I had to recommend one language for people to learn in the current decade.. it would be Ruby. I'm glad I did.. Although some people may claim Python. But its like my opinion.. man! :D
至于C#家伙不够OO ......不要把它放在心上..当你大惊小怪的时候,把它作为你说话的东西。 Ruby为大多数人做到了这一点。如果我不得不推荐一种语言供人们在当前的十年中学习......那就是Ruby。我很高兴我做过..虽然有些人可能会说Python。但它像我的意见..男人! :d
#10
1
I don't think this is specifically about duck typing. For instance C# supports limited duck-typing already - an example would be that you can use foreach on any class that implements MoveNext and Current.
我不认为这是关于鸭子打字的具体内容。例如,C#已经支持有限的鸭子类型 - 例如,你可以在任何实现MoveNext和Current的类上使用foreach。
The concept of duck-typing is compatible with statically typed languages like Java and C#, it's basically an extension of reflection.
duck-typing的概念与Java和C#等静态类型语言兼容,它基本上是反射的扩展。
This is really the case of static vs dynamic typing. Both are proper-OO, in as much as there is such a thing. Outside of academia it's really not worth debating.
这实际上是静态与动态类型的情况。两者都是正确的OO,就像有这样的东西一样。在学术界之外,真的不值得辩论。
Rubbish code can be written in either. Great code can be written in either. There's absolutely nothing functional that one model can do that the other can't.
垃圾代码可以用任何一种编写。伟大的代码可以写入其中。一个模型完全没有功能,而另一个模型无法做到。
The real difference is in the nature of the coding done. Static types reduce freedom, but the advantage is that everyone knows what they're dealing with. The opportunity to change instances on the fly is very powerful, but the cost is that it becomes hard to know what you're deaing with.
真正的区别在于编码的性质。静态类型减少了*,但优点是每个人都知道他们正在处理什么。动态更改实例的机会非常强大,但成本是很难知道你正在使用什么。
For instance for Java or C# intellisense is easy - the IDE can quickly produce a drop list of possibilities. For Javascript or Ruby this becomes a lot harder.
例如,对于Java或C#,intellisense很容易 - IDE可以快速生成一系列可能性。对于Javascript或Ruby来说,这变得更加困难。
For certain things, for instance producing an API that someone else will code with, there is a real advantage in static typing. For others, for instance rapidly producing prototypes, the advantage goes to dynamic.
对于某些事情,例如生成其他人将编码的API,静态类型有一个真正的优势。对于其他人,例如快速生产原型,其优势在于动态。
It's worth having an understanding of both in your skills toolbox, but nowhere near as important as understanding the one you already use in real depth.
值得在你的技能工具箱中理解这两者,但远不如理解你已经在真实深度使用的那个重要。
#11
1
Object Oriented is a concept. This concept is based upon certain ideas. The technical names of these ideas (actually rather principles that evolved over the time and have not been there from the first hour) have already been given above, I'm not going to repeat them. I'm rather explaining this as simple and non-technical as I can.
面向对象是一个概念。这个概念基于某些想法。这些想法的技术名称(实际上是在一段时间内发展并且从第一个小时起就没有进化过的原则)已经在上面给出了,我不打算重复它们。我宁愿把它解释为尽可能简单和非技术性。
The idea of OO programming is that there are objects. Objects are small independent entities. These entities may have embedded information or they may not. If they have such information, only the entity itself can access it or change it. The entities communicate with each other by sending messages between each other. Compare this to human beings. Human beings are independent entities, having internal data stored in their brain and the interact with each other by communicating (e.g. talking to each other). If you need knowledge from someone's else brain, you cannot directly access it, you must ask him a question and he may answer that to you, telling you what you wanted to know.
OO编程的想法是有对象。对象是小的独立实体。这些实体可能嵌入了信息,也可能没有。如果他们有这样的信息,只有实体本身可以访问或更改它。实体通过在彼此之间发送消息来彼此通信。将此与人类进行比较。人类是独立的实体,其内部数据存储在他们的大脑中并且通过交流(例如彼此交谈)彼此交互。如果你需要某人的大脑知识,你不能直接访问它,你必须问他一个问题,他可以回答你,告诉你你想知道什么。
And that's basically it. This is real idea behind OO programming. Writing these entities, define the communication between them and have them interact together to form an application. This concept is not bound to any language. It's just a concept and if you write your code in C#, Java, or Ruby, that is not important. With some extra work this concept can even be done in pure C, even though it is a functional language but it offers everything you need for the concept.
这基本上就是这样。这是OO编程背后的真实想法。编写这些实体,定义它们之间的通信并让它们一起交互以形成应用程序。这个概念不受任何语言的约束。这只是一个概念,如果你用C#,Java或Ruby编写代码,那就不重要了。通过一些额外的工作,这个概念甚至可以在纯C中完成,即使它是一种功能语言,但它提供了概念所需的一切。
Different languages have now adopted this concept of OO programming and of course the concepts are not always equal. Some languages allow what other languages forbid, for example. Now one of the concepts that involved is the concept of classes. Some languages have classes, some don't. A class is a blueprint how an object looks like. It defines the internal data storage of an object, it defines the messages an object can understand and if there is inheritance (which is not mandatory for OO programming!), classes also defines from which other class (or classes if multiple inheritance is allowed) this class inherits (and which properties if selective inheritance exists). Once you created such a blueprint you can now generate an unlimited amount of objects build according to this blueprint.
不同的语言现在采用了OO编程的概念,当然概念并不总是相同的。例如,某些语言允许其他语言禁止。现在涉及的概念之一是类的概念。有些语言有类,有些则没有。类是对象的外观蓝图。它定义了一个对象的内部数据存储,它定义了一个对象可以理解的消息,如果有继承(这对于OO编程来说不是必需的!),类还定义了从哪个其他类(或多个继承允许的类)此类继承(以及存在选择性继承时的属性)。创建这样的蓝图后,您现在可以根据此蓝图生成无限量的对象构建。
There are OO languages that have no classes, though. How are objects then build? Well, usually dynamically. E.g. you can create a new blank object and then dynamically add internal structure like instance variables or methods (messages) to it. Or you can duplicate an already existing object, with all its properties and then modify it. Or possibly merge two objects into a new one. Unlike class based languages these languages are very dynamic, as you can generate objects dynamically during runtime in ways not even you the developer has thought about when starting writing the code.
但是,有些OO语言没有课程。然后如何构建对象?嗯,通常是动态的。例如。您可以创建一个新的空白对象,然后动态添加内部结构,如实例变量或方法(消息)。或者,您可以复制已存在的对象及其所有属性,然后对其进行修改。或者可能将两个对象合并为一个新对象。与基于类的语言不同,这些语言非常动态,因为您可以在运行时动态生成对象,甚至开发人员在开始编写代码时也没有考虑过。
Usually this dynamic has a price: The more dynamic a language is the more memory (RAM) objects will waste and the slower everything gets as program flow is extremely dynamically as well and it's hard for a compiler to generate effective code if it has no chance to predict code or data flow. JIT compilers can optimize some parts of that during runtime, once they know the program flow, however as these languages are so dynamically, program flow can change at any time, forcing the JIT to throw away all compilation results and re-compile the same code over and over again.
通常这种动态有一个代价:语言越动态,内存(RAM)对象就越浪费,一切都变得越慢,因为程序流程也非常动态,如果没有机会,编译器很难生成有效的代码预测代码或数据流。 JIT编译器一旦知道程序流就可以在运行时优化它的某些部分,但是由于这些语言是动态的,程序流可以随时改变,迫使JIT抛弃所有编译结果并重新编译相同的代码一遍又一遍地。
But this is a tiny implementation detail - it has nothing to do with the basic OO principle. It is nowhere said that objects need to be dynamic or must be alterable during runtime. The Wikipedia says it pretty well:
但这是一个很小的实现细节 - 它与基本的OO原则无关。没有人说对象需要是动态的,或者必须在运行时可以改变。*说得很好:
Programming techniques may include features such as information hiding, data abstraction, encapsulation, modularity, polymorphism, and inheritance.
编程技术可以包括诸如信息隐藏,数据抽象,封装,模块化,多态和继承之类的特征。
http://en.wikipedia.org/wiki/Object-oriented_programming
They may or they may not. This is all not mandatory. Mandatory is only the presence of objects and that they must have ways to interact with each other (otherwise objects would be pretty useless if they cannot interact with each other).
他们可能或他们可能没有。这不是强制性的。强制性只是对象的存在,并且它们必须具有相互交互的方式(否则,如果它们不能相互交互,则对象将毫无用处)。
#12
1
You asked: "Can someone show me an example of a wonderous thing I could do with ruby that I cannot do with c# and that exemplifies this different oop approach?"
你问道:“有人能告诉我一个我可以用红宝石做的奇妙事情的例子,我不能用c#来做,这就是这种不同的oop方法的例证吗?”
One good example is active record, the ORM built into rails. The model classes are dynamically built at runtime, based on the database schema.
一个很好的例子是活动记录,ORM内置于rails中。模型类是在运行时根据数据库模式动态构建的。
#13
0
This is really probably getting down to what these people see others doing in c# and java as opposed to c# and java supporting OOP. Most languages cane be used in different programming paradigms. For example, you can write procedural code in c# and scheme, and you can do functional-style programming in java. It is more about what you are trying to do and what the language supports.
这实际上可能是这些人看到别人在c#和java中所做的事情,而不是c#和java支持OOP。大多数语言都可以用于不同的编程范例。例如,您可以用c#和scheme编写过程代码,并且可以在java中进行函数式编程。它更多地是关于您尝试做什么以及语言支持的内容。
#14
0
I'll take a stab at this.
我会捅这个。
Python and Ruby are duck-typed. To generate any maintainable code in these languages, you pretty much have to use test driven development. As such, it is very important for a developer to easily inject dependencies into their code without having to create a giant supporting framework.
Python和Ruby是鸭子类型。要生成这些语言中的任何可维护代码,您几乎必须使用测试驱动开发。因此,开发人员可以轻松地将依赖项注入其代码中,而无需创建巨大的支持框架。
Successful dependency-injection depends upon on having a pretty good object model. The two are sort of two sides of the same coin. If you really understand how to use OOP, then you should by default create designs where dependencies can be easily injected.
成功的依赖注入依赖于具有非常好的对象模型。这两个是同一枚硬币的两面。如果您真的了解如何使用OOP,那么您应该默认创建可以轻松注入依赖项的设计。
Because dependency injection is easier in dynamically typed languages, the Ruby/Python developers feel like their language understands the lessons of OO much better than other statically typed counterparts.
因为依赖注入在动态类型语言中更容易,所以Ruby / Python开发人员觉得他们的语言比其他静态类型的同行更能理解OO的教训。