Python入门之面向对象和类
定义Python中的类

举例1:
class Student:
pass
print(id(Student)) #1149225945800
print(type(Student)) #<class 'type'>
print(Student) #<class '__main__.Student'>
举例2:
class Student:
native_place='吉林' #类属性
def __init__(self,name,age):
self.name=name
self.age=age
#实例方法
def eat(self):
print("学生在吃饭")
#静态方法
@staticmethod
def method():
print("我是静态方法")
#类方法
@classmethod
def cm(cls):
print("我是类方法")
本文来自博客园,作者:半抹灯芯,转载请注明原文链接:https://www.cnblogs.com/wanxiangsucai/p/17567263.html

浙公网安备 33010602011771号