Python Learning Notes 008_ Class _ Object

Source: Internet
Author: User

# Object = attribute + method
>>> # Class name conventions in Python start with uppercase letters
>>> # TT = Turtle () This is the way to create class instances, other languages with new, which is not required
>>>
>>> # The self in Python is equivalent to this in Java


>>> # Self , usually put in the first parameter of the method this is the default requirement

classBall :defSetName (self,name): Self.name=namedefKick (self):Print("My name is%s, who kicked me and found death ..."%self.name) A= Ball () #  No need to use the New keyword, notice the difference with other languages A.setname ("Huang") b=Ball () b.setname ("Huanghongyi") A.kick () My name is Huang, who kicks me and dies ... b.kick () My name is Huanghongyi, who kicks me, find death ...


>>>
>>> # Python's Magic method, which is automatically called by Python at special Times
>>> # One feature of these magic methods is that they are surrounded by double underscores
>>> # such as __init__ (self)
>>> # __ init__ (Self,param1,param2,...), is a construction method that is automatically called when an object is instantiated
>>>

class Ball :     def __init__ (self,name):         = name    def  Kick (self):        print(" My name is%s, who kicks me and dies ...)  " %self.name)        = Ball (' potatoes ') B.kick () My name is potatoes, Who kicked me, looking for death ...

>>>

>>># To define a private variable or function in Python, just add two underscore "__" before the name

>>>classPerson :__name='Pig'>>> p =Person ()>>> p.__nameTraceback (most recent): File"<pyshell#45>", Line 1,inch<module>p.__nameAttributeerror:' Person'object has no attribute'__name'

>>>

>>># in fact,Python is the name of a private property and method that starts with __ class name __ Variable Name
>>> # From this point, it can be seen thatthe privatization of Python is a pseudo-private

class Person :     __name ' Pig '    def GetName (self):         return self. __name    >>> p = person ()>>> p.getname ()' pig '> >> >>> p._person__name' pig '

Python Learning Notes 008_ Class _ Object

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.