Why the claim that c# people don't get object-oriented programming? (vs class-oriented)

前端 未结 14 1912
有刺的猬
有刺的猬 2021-02-07 12:15

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 o

相关标签:
14条回答
  • 2021-02-07 12:56

    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.

    0 讨论(0)
  • 2021-02-07 13:00

    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.

    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)..

    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.

    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.

    0 讨论(0)
  • 2021-02-07 13:02

    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.

    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.

    0 讨论(0)
  • 2021-02-07 13:06

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

    One good example is active record, the ORM built into rails. The model classes are dynamically built at runtime, based on the database schema.

    0 讨论(0)
  • 2021-02-07 13:07

    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.

    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.

    So in this sense C# and other statically typed OO languages are not true OO, sine thay lack "true" encapsulation.

    0 讨论(0)
  • 2021-02-07 13:07

    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.

    0 讨论(0)
提交回复
热议问题