python面向对象基础

# 万物皆对象
# python类的语法 关键字 class
# 类名的规范:数字、字母、下划线组成,不能以数字开头,首字母大写,驼峰命名
# 类属性:类中的变量值
# 类方法:类中的函数
# 概括出实例共有的属性、方法

class Boy_Friend():  # 定义一个BoyFriend类
    # 类属性
    height = 175
    weight = 120
    money = "500万"

    def __init__(self, name, age):  # 初始化函数,实例方法,一般不传动态参数(*args)和关键字参数(**kwargs)
        self.name = name
        self.age = age

    # 某个属性是多个函数公用的,采用实例方法,没有return
    # 类函数
    @classmethod  # 类方法,当某个函数与其他函数属性无关时创建类方法
    def swimming(cls):
        print("酷酷酷")

    def cooking(self):  # 实例方法
        print("身高{},体重{}".format(self.height, self.weight))
        print("我会做饭")

    def earn(self):
        print("月薪3万")

    @staticmethod  # 静态方法
    def sing():
        print("会唱歌")

运行结果:

  120
  酷酷酷

原文地址:https://www.cnblogs.com/jialeliu/p/14064026.html