What makes a language Object-Oriented?

后端 未结 15 2183
无人共我
无人共我 2020-12-07 17:01

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 no

15条回答
  •  萌比男神i
    2020-12-07 17:39

    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

提交回复
热议问题