Ruby Study Notes (4)-Dynamic Modification of class attributes

Source: Internet
Author: User

The most obvious feature of Dynamic Language is that the behavior/attribute of a class instance can be dynamically modified after the new version is released! I personally think this kind of processing is more in line with the real world than Java/C # (static language.

For example, when a person is born, almost none of the other basic primitive instincts except crying and breastfeeding (primitive instinct can understand the attributes and methods most initially defined in class definitions ), but over time, I learned to read books, walk, and talk... (equivalent to adding a method/attribute), no one can predict in the future. Everything is unknown, so it is unlikely to be like a static language, before running the program, you can write all the attributes/methods in advance. Even if a person forgets what he has learned, it is impossible (for example, he suddenly does not speak, it is equivalent to dynamically deleting the method/attribute of the instance. After being well-managed, you can talk again (re-Add a method ).

Of course, I do not mean that static languages are not bad. I can only say that each has its own characteristics.CodeRight:

 class persondef cryputs "Wow... "enddef eatputs", please do not disturb... "enddef showbodyputs" streaking to show your own body "# This method is relatively straightforward. endendaperson = person. new # creates a newborn aperson. cry # I was just born. Of course I have to cry a few times. eat # Eat aperson when you are tired. showbody # For Children, just show me JJ. Class persondef readputs "learned to read books" enddef mongoputs "learned to walk" endendendaperson. walkaperson. readclass personundef read # later, for some reason, I suddenly won't be able to read, maybe it's a strange problem :) end # aperson. read # Here the error "undefined method 'read' for # 
  
    (nomethoderror)" class personprivate: showbody # after growing up, of course, you cannot show your body to strangers. End # aperson. showbody # the following error is returned: Private method 'showbody' called for # 
   
     (nomethoderror) 
   
  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.