Day4----class properties, static methods, polymorphism

'''
access control
    Private: prefix methods and properties with __
    Permissions: cannot be directly accessed and used outside the class through objects
         Can only be used inside a class
         cannot be inherited by subclasses
    Access and use private properties: define a public method to use

Private method: add __ before the method
        cannot be used outside the class
        For internal use, do not allow external direct calls

class Dog:
    def born(self):
        print('a puppy was born')
        self.__sleep()
    def __sleep(self):
        print('30 days off')

dog=Dog()
dog.born()

private property

class People:
    def __init__(self):
        self.__ICBC_money=0 # private attribute

    # define public methods
    def get_money(self):
        return self.__ICBC_money

    def set_money(self,money):
        self.__ICBC_money+=money
        return self.__ICBC_money

# Instance object.__dict__ View the attribute information of the object
xw=People()
money=xw.get_money()
print(money)
xw.set_money(1000)
money=xw.get_money()
print(money)


Object (instance object): Created by a class defined by a class, that is, instantiated by a class, called an instance
    Defined properties are instance properties
    Instance attribute: one copy exists in each instance object, and the value may be different
Class (class object): through the class definition, the python interpreter is automatically created when the class is created
    Define an instance object
    Attribute information can be saved, known as class attributes
    Class attributes: variables defined inside the class and outside the method. There is only one copy in word memory
Judging instance attributes and class attributes:
    Assuming it is an instance attribute, see whether the attribute value is the same for different instance objects, and needs to be changed at the same time
        Yes: class attribute
        not: instance attribute
If there is no class attribute same as instance attribute, you can use instance object to access class attribute

Instance method: the method defined by default in the class, the first parameter is self
Class method: the first parameter of the method decorated with @classmethod is cls
Static method: @staticmethod
        Object.MethodName()
        classname.methodname()

Use instance attributes in methods, use instance methods
Instance attributes are not used in methods, class attributes are used, and class methods are used
Instead of using instance attributes and class attributes, static methods can be defined at this time
class Dog():
    class_name='dog class'

    def __init__(self,name,age):
        self.name=name
        self.age=age
    def play(self):
        print(f'The puppy {self.name} is playing happily')

    @classmethod
    def get__class_name(cls):
        return cls.class_name

    @staticmethod
    def show_info():
        print('This is a static method')


dog=Dog('rhubarb',2)
dog.play()
dog.show_info()
Dog.show_info()

'''

'''
Polymorphism: where you need to use parent class objects, you can also use subclass objects to get different results
Steps for usage:
        Subclass inherits parent class
        Subclass overrides parent class method
        Call this method through the object
        
  class Dog():

    def __init__(self,name):
        self.name=name

    def play(self):
        print(f'The puppy {self.name} is playing happily')

class XTQ(Dog):
    def play(self):
        print(f'The puppy {self.name} is chasing the sea of ​​clouds in the sky')

def play_with_dog(obj_dog):
    obj_dog.play()

dog=Dog('rhubarb')
play_with_dog(dog)
xtq=XTQ('Dabai')
play_with_dog(xtq)      

'''

'''
polymorphism with duck typing

class Cat():
    def __init__(self,name):
        self.name=name

    def play(self):
        print('The kitten is playing')


def play_with_dog(obj_dog):
    obj_dog.play()

cat=Cat('Xiaohui')
play_with_dog(cat)
'''

Guess you like

Origin blog.csdn.net/m0_46493223/article/details/126091086