Python study notes (eight): create classes and objects, and the usage of __init__() method

1. Create classes and objects

1.1. Define the format of the class:

class 类名:
    方法列表

1.2. Create one or more objects based on the class. The format of creating an object is as follows:

对象名1 = 类名()
对象名2 = 类名()

1.3. Sample code

# 定义一个Person类
class Person:
    # 实例方法
    def speak(self):
        print("我在学Python")

# 实例化对象p1
p1 = Person()
# 给p1对象添加属性,以及对应的属性值
p1.name = "crystal"
#调用对象的实例方法
p1.speak()
print("我的名字叫", p1.name)

# 实例化对象p2
p2 = Person()
# 给p2对象添加属性,以及对应的属性值
p2.name = "allan"
# 调用实例化方法
p2.speak()
print("我的名字叫", p2.name)

2. Get the properties of the object through self

After the object is created and attributes are added, how to get these attributes in the instance method of the class? ===》Get the properties of the object through self

The code in 1.3 can be modified as follows:

# 定义一个类
class Person:
    # 实例化方法
    # self指向调用speak()方法的对象
    def speak(self):
        print("我在学Python")
        # print("我的名字叫%s,年龄%d" % (self.name, self.age))
        # print("我的名字叫{n},年龄{a}".format(a=self.age, n=self.name))
        print("我的名字叫{},年龄{}".format(self.name, self.age))

# 实例化一个对象
p1 = Person()
# 给p1对象添加属性,以及对应的属性值
p1.name = "crystal"
p1.age = 20
#调用实例化方法,即可获取对象p1的属性
p1.speak()

3.__init__() method

3.1.__init__() magic method

Magic method: The method that starts with two underscores __ and ends with two underscores is the magic method.

__init__() magic method: usually used to do attribute initialization and assignment operations, the __init__() magic method is also called the construction method.

The sample code is as follows:

# 定义一个类
class Person(object):
    # __init__()方法用来对变量做初始化和赋值操作,在类实例化对象的时候,会自动被调用
    def __init__(self):
        self.name = "crystal"
        self.age = 20

    # 实例化方法
    def speak(self):
        print("我的名字叫{},年龄{}".format(self.name, self.age))

# 实例化一个对象p,会自动调用__init()__对象
p = Person()
# 调用实例化方法speak(),即可获取对象p的属性
p.speak();

3.2. __init__() method with parameters

Define the fixed value of the attribute in the __init__() method, then the attribute value of each instantiated object is the same, how to make each instantiated object have different attribute values? ===》The __init__() method with parameters can be used

The sample code is as follows:

# 定义一个类
class Person(object):
    # __init__()构造方法,用来对变量做初始化和赋值操作
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # 实例化方法
    def speak(self):
        print("我的名字叫{},年龄{}".format(self.name, self.age))

# 实例化一个对象p1
p1 = Person("crystal", 20)
# 调用实例化方法speak(),即可获取对象p1的属性值
p1.speak()

# 实例化一个对象p2
p2 = Person("allan", 29)
# 调用实例化方法speak(),即可获取对象p2的属性值
p2.speak()

3.3. Attention

  • __init__(self)In, there is one parameter named self by default. If two actual parameters are passed when creating an object, then __init__(self)two parameters are required in addition to self as the first parameter, for example:__init__(self,x,y);
  • Get properties and instance methods inside the class, and get them through self;
  • Get properties and instance methods outside the class, and get them by object name;
  • If there are multiple objects in a class, the properties of each object are stored separately and have their own independent addresses;
  • The instance method is shared by all objects and only occupies one memory space. The class will use self to determine which object called the instance method.

Guess you like

Origin blog.csdn.net/weixin_44679832/article/details/114183700