类
命名:由一个或多个单词组成,每个单词的首字母大写,其余小写
属性:类方法外的变量称为类属性,被该类的所有对象所共享
实例方法:def
既可以获取构造函数定义的变量,也可以获取类的属性值。
类方法:
修饰符 @classmethod 使用类名直接访问的方法
不能获取构造函数定义的变量,可以获取类的属性。
静态方法:
修饰符 @staticmethod 使用类名直接访问的方法
不能获取构造函数定义的变量,也不可以获取类的属性。
调用:
创建对象 student1 = Student('jack', 20)
类方法调用:
对象名.方法名() student1.info()
类名.方法名(类对象),实际上就是方法定义处的 self
动态绑定属性、方法:
student1.sex = '男'
def show():
pass
student1.show = show
class Student:
# 类的变量称为 类属性
native_pace = 'zhejiang'
def __init__(self, name, age):
self.name = name
self.age = age
# 实例方法
def info(self):
print('我的名字叫:{0}, 年龄是:{1}'.format(self.name, self.age))
print(self.native_pace)
# 类方法
@classmethod
def cm(cls):
print(cls)
print(cls.native_pace)
# print(cls.age)
# 静态方法
@staticmethod
def sm():
print('staticmethod')
# 实例化调用
student1 = Student('Jack', 20)
student1.info()
# 类.方法 调用
Student.info(student1)
