类和实例

面向对象

class定义类

类和实例

class Student:

# 类属性

province = '天津'

def init(self,name):

# 实例属性

self.name = name

# 实例方法

def say(self):

print('{}说我今天要唱歌'.format(self.name))

# 类方法

@classmethod

def hello(cls):

print(cls,'hello world')

# 静态方法

@staticmethod

def world():

print('你好')

# 类的实例化

stu1 = Student('张三')

stu2 = Student('李四')

print(stu1.name,stu1.province)

print(stu2.name,stu2.province)

print(Student.province)

stu1.say()#调用实例方法

stu2.say()

Student.hello()#类方法

Student.world()#静态方法

stu1.hello()

stu1.world()

posted @ 2024-06-10 23:06  淡然。。  阅读(34)  评论(0)    收藏  举报