a fastMacedonia Day10: Object-Oriented (classes and objects: initializing the __init__ Method)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43264177/article/details/102765094
# @time:2019/10/27 10:28
# @Author:coco
# @File:03类的定义.py
# @software:PyCharm

"""
定义在类里面的函数:叫做方法(本质上还是函数)

初始化方法:
        创建对象的时候是自动执行的,不需要手动调用这个方法

self:代表的就是对象(谁去调用这个方法,代表的就是谁)
"""


class Cat:
    """
    定义一个猫类
    """
    # 类属性
    leg = '四条腿'
    tail = '长尾巴'

    # 定义实例属性通过初始化方法
    def __init__(self, age, name):
        # 定义实例属性
        self.age = age
        self.name = name
        print('正在给对象设置属性')


# 创建对象
kt = Cat(age=18, name='凯蒂')
hh = Cat(age=20, name='灰灰')

print(kt.age,kt.name,kt.leg,kt.tail)

operation result:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43264177/article/details/102765094