[Python object-oriented] (1) instantiated object

Note: Variables beginning with "__" are private variables by default and cannot be accessed directly

Verification example: isinstance

class Cat (object):
     "" " Cats " "" 
    tag = " I am a domestic cat " 

    def  __init__ (self, name, age):
         "" " The attributes that start with double underscores are private variables by default and cannot be direct its operations "" " 
        self.name = name 
        Self. __age = Age 

    DEF set_age (Self, Age):
         " "" 
        change the cat's age 
        : param age: Age 
        : return: after the change of age 
        "" " 
        Self . __age = age
         return self.__age

    defshow_info (self):
         "" " 
        Show cat information 
        : return: Cat information 
        " "" 
        print ( " My name is {0}, I am {1} years old this year " .format (self.name, self. __age )) 

    def eat (self):
         "" " " "" 
        print ( " Cats like to eat fish " ) 

    def catch (self):
         "" " caught " "" 
        print ( " Cat caught mouse " ) 


if  __name__ == " __main__ " :
    # Instantiate an object 
    cat_black = Cat (" Black " , 6 ) 
    cat_black.catch ()   # Output: cat and mouse 
    cat_black.show_info ()   # output: My name is black, 6 years old this year 
    Print (cat_black.name)   # output: black 
    # Print (cat_black .__ age) # private variable can not directly access the 
    Print (cat_black.tag)   # output: I am a domestic cat 

    # instance of a class judgment 
    Print (isinstance (cat_black, cat))   # output: True

 

Guess you like

Origin www.cnblogs.com/ac-chang/p/12699548.html