OOP Terminology: class, attribute, property, field, data member

前端 未结 7 735
有刺的猬
有刺的猬 2020-12-05 04:49

I am starting studying OOP and I want to learn what constitutes a class. I am a little confused at how loosely some core elements are being used and thus adding to my confus

相关标签:
7条回答
  • 2020-12-05 05:33

    Firstly, you need to select a language. For example, I would recommend you to select Ruby language and community. Until you select a language, you cannot escape confusion, as different communities use different terms for the same things.

    For example, what is known as Module in Ruby, Java knows as abstract class. What is known as attributes in some languages, is known as instance variables in Ruby. I recommend Ruby especially for its logical and well-designed OOP system.

    Write the following in a *.rb file, or on the command line in irb (interactive Ruby interpreter):

    class Dog                         # <-- Here you define a class representing all dogs.
      def breathe                     # <-- Here you teach your class a method: #breathe
        puts "I'm breathing."
      end
    
      def speak                       # <-- Here you teach your class another method: #speak
        puts "Bow wow!"
      end
    end
    

    Now that you have a class, you can create an instance of it:

    Seamus = Dog.new
    

    You have just created an instance, a particular dog of class Dog, and stored it in the constant Seamus. Now you can play with it:

    Seamus.breathe                   # <-- Invoking #breathe instance method of Seamus
    #=> I'm breathing.
    Seamus.speak                     # <-- Invoking #speak instance method of Seamus
    #=> Bow wow!
    

    As for your remaining terminology questions, "property" or "attribute" is understood as "variable" in Ruby, almost always an instance variable. And as for the term "data member", just forget about it. The term "field" is not really used in Ruby, and "class variable" in Ruby means something very rarely used, which you definitely don't need to know at this moment.

    So, to keep the world nice and show you that OOP is really simple and painless in Ruby, let us create an attribute, or, in Ruby terminology, an instance variable of Dog class. As we know, every dog has some weight, and different dogs may have different weights. So, upon creation of a new dog, we will require the user to tell us dog's weight:

    class Dog
      def initialize( weight )  # <-- Defining initialization method with one argument 'weight'
        @weight = weight        # <-- Setting the dog's attribute (instance variable)
      end
      attr_reader :weight       # <-- Making the dog's weight attribute visible to the world.
    end
    
    Drooly = Dog.new( 16 )      # <-- Weight now must provide weight upon initialization.
    Drooly.weight               # <-- Now we can ask Drooly about his weight.
    #=> 16
    

    Remember, with Ruby (or Python), things are simple.

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