What's the best way to define the words “class” and “object” to someone who hasn't used them?

前端 未结 29 862
情话喂你
情话喂你 2020-12-13 05:11

My neighbor is taking \"Intro to Java\", and asked me to help explain a few of the first-day concepts. I realized that since I do this everyday, I don\'t have the beginner\

29条回答
  •  半阙折子戏
    2020-12-13 05:51

    Object Oriented programming is about creating programs using as building blocks, "things" that exists in the real world, these real world things are called objects, hence object oriented

    For instance, if you're creating a Address Book program, you may define the following objects:

    person, address, phone
    

    Among many, many others. Those would be real life objects, and you describe your program in terms of these abstractions.

    With that in mind you can start describing some concepts.

    Class is used to define the characteristics an objects will have. A class is used only as a template, or a blueprint. For instance, for all the persons in your address book, you may say they all will have:

    Person:
       - name 
       - last name 
       - phone number 
       - address 
    

    Etc.

    An address may have:

     Address:
        - street 
        - number
        - city 
        - zip code 
        - country 
    

    And so on. As you can notice, a class me be defined in terms of other classes, for instance, in this context, a person has one address.

    An Object is a particular instance of a given class. When you add an entry to your address book, you create an object and fill in the attributes.

     onePerson  ofType Person is (  
         - name = "Oscar"
         - last name = "Reyes" 
         - phone number = "56 58 11 11"
         - address = anAddress ofType Address (
                         - street = "Tecolotes" 
                         - number = 32
                         - city   = "D.F." 
                         - zip code = 23423
                         - country = "Mexico"
                     ) 
      )
    

    So, this object is a class instantiated with data. Other entry in the address book are other objects with different data.

    That shows the difference between them.

    There are other relevant concepts in OOP that are worth listing, and interrelate with the concept of object and class:

    Abstraction You don't need to list all the attributes of a person, to use it. for instance, in this case, you don't care if that person is single or married, even when in real life, persons are either single or married.

    Encapsulation Attributes from the person are hidden to other objects and are accessed through methods, this prevent from data corruption.

    Polymorphism A different type may respond differently to the same message or method.

    Inheritance classes may have subclasses and attributes and behavior which inherit the characteristics of the super classes.

提交回复
热议问题