Python3 study notes--object-oriented programming

What is an object?

class:
object:
method:
instantiate: assign some property to an object

Class syntax:

class dog:
    def name(self):
        print(hello master,my name is Python)

class method:

self: methods of a class have only one special difference from normal functions - they must have an extra first parameter name, but when you call the method you don't assign a value to this parameter, Python will provide this value, this special The variable refers to the object itself, which by convention is named self.

If you have a class called MyClass and an instance of this class MyObject, when you call the method MyObject.method(arg1, arg2) on this object, this will be automatically converted by Python to MyClass.method(MyObject, args1, args2 )

init : initialization

class Person:
    def __init__(self,name,age):
        print("I am being called right now")
        self.Name = name
        self.Age = age

    def sayHi(self):
        print("Hi my name is %s,i am %s years old" %(self.Name,self.Age))

    def __del__(self):
        print("I got killed just now ....bye")

p = Person('Alex',29)

def init (self,name,age): This function is a constructor, which is automatically executed when the class is called
def del (self): This function is a destructor, which is executed when the class finishes calling and the memory is ready to be released
Destructor Destructor and constructor are optional

Define a variable in the class. If you need to call the variable, use the self. variable. It can be understood like this:
each variable is a function and a local variable, so if I want to call this variable, I need to specify the call from this class, so I need to use self.value.

How to call functions and local variables in a class:
turn local variables into class variables

seld.Favor = favorate_food

Call: self.Favor to complete the call

class inheritance

class modification

setattr(): the built-in function implements the method of the class Add
getattr(): the method of obtaining the class

public and private properties

Private attributes starting with two underscores can only take effect inside the class and cannot be called outside: __private_var

Guess you like

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