多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
> 类 ![](https://box.kancloud.cn/29d472aff3b668800065e8baacad29b6_526x231.png) * `c1.py `文件 ``` class Student(): name = "" age = 24 def getInfo(self): print("name "+self.name) print("age "+str(self.age)) stud1 = Student() stud2 = Student() stud3 = Student() # stud1.getInfo() print(id(stud1)) # 2825425418392 print(id(stud2)) # 2825425418560 print(id(stud3)) # 2825425418728 ``` * `c2.py `文件,类的调用 ``` from c1 import Student stru = Student() print(stru) # <c1.Student object at 0x000002703928CF60> ``` > 构造函数 * `c3.py `文件 ``` class Student(): def __init__(self): print("init") ``` * 打印构造函数 ``` stru = Student() a = stru.__init__() print(a) # None print(type(a)) # <class 'NoneType'> ``` * 返回值,只能返回 `return None`,不能返回其他 ``` def __init__(self): return None ``` * 类变量和实例变量 ``` s1 = Student("Tinywan",24) s2 = Student("TinyAiAI",22) print(s1.name) print(s2.name) print(Student.name) ``` * 类和实例的字典`__dict__` ``` s1 = Student("Tinywan",24) print(s1.__dict__) # 保存当前对象所有的变量和方法 ``` * 访问类属性的两种方法 ``` class Student(): age = 24 sum = 100 def __init__(self,name,age): self.__class__.sum += 1 # 操作 print(self.__class__.sum) # 第一种 print(self.age) # 第二种 ```