Introduction to code Python (9, object-oriented)

#!/usr/bin/env python
# -*- coding: UTF-8 -*-


class Critter: # define the class
    """A virtual pet"""
    total = 0 # Create a class feature, similar to a static variable of a C++ class, the assignment statement will only be executed once

    @staticmethod # static method modifier
    # Create a static method without self in the parameter list, because static methods are called through the class (not through the object)
    # That is, static methods cannot pass a reference to an object
    def status():
        print("\nThe total number of critters is", Critter.total)

    def __init__(self, name, mood): # Constructor
        print("A new critter has been born!")
        self.name = name # public property
        self.__mood = mood # private property
        Critter.total += 1 # Each time a new object is created, the class attribute total increases by 1

    @property # Properties can precisely control how properties are accessed or modified
    def mood(self): # Create property, get read access to private properties
        return self.__mood

    @mood.setter # A modifier used to create a property value setter, indicating the setter property of the name property, suggesting that the next method is to set the mood property
    def mood(self, new_mood): # create properties, get write access to private properties
        if new_mood == "":
            print("A critter's name can't be the empty string.")
        else:
            self.__mood = new_mood
            print("Name change successful.")
    
    def __str__(self): # Create a string representation for this object: this string will be displayed when the object is printed
        rep = "Critter object\n"
        rep += "name:" + self.name + "\n"
        return rep

    def __private_method(self): # create a private method
        print("This is a private method.")

    def public_method(self): # access private methods
        print("This is a public method.")
        self.__private_method()

    def talk(self): # define the method
        print("\nHi. I'm", self.name)
        print("Right now I feel", self.__mood, "\n")
        print("Right now I feel", self.mood, "\n") # self.mood is used to access attributes, which is actually calling the method mood()

# program body
crit1 = Critter("Poochie") # instantiate the object
crit1.talk() # call the object's method

print(crit1.mood) # Access the mood property of the object
print(crit1._Critter__mood) # access private properties outside the class definition
crit1._Critter__private_method() # Access private methods outside the class definition

print("\nAttempting to change crit1's name to sad..")
crit1.mood = "sad" # Access the mood attribute of the object and write (modify) __mood
print(crit1.mood)     

crit2 = Critter("Randolph")
crit2.talk()

# access class properties
print(Critter.total)
print(crit1.total) # Each object can read the class properties of the class to which it belongs
Critter.status() # call static method

print("Printing crit1:")
print(crit1)
print("Directly accessing crit1.name :")
print(crit1.name)



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324599374&siteId=291194637