创建类的语法
类的组成
class Student:
native_place='吉林'
def _init_(self,name,age):
self.name=name
self.age=age
def eat(self):
print('学生在吃饭')
@staticmethod
def method():
print('我使用了staticmethod进行修饰,所以我是静态方法')
@classmethod
def cm(cls):
print('我使用了classmethod进行修饰,所以我是类方法')
def drink():
print('喝水')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
对象的创建
class Student:
native_place='吉林'
def _init_(self,name,age):
self.name=name
self.age=age
def eat(self):
print('学生在吃饭')
stu1=Student('张三',20)
stu1.eat()
print(stu1.name)
print(stu1.age)
Student.eat(stu1)