1 个回答|524 次阅读
init是Python中的方法或构造函数。创建类的新对象/实例时,将自动调用此方法以分配内存。所有类都具有init方法。这是一个如何使用它的例子
class Employee: def __init__(self, name, age,salary): self.name = name self.age = age self.salary = 20000 E1 = Employee("XYZ", 23, 20000) # E1 is the instance of class Employee. #__init__ allocates memory for E1. print(E1.name) print(E1.age) print(E1.salary)
输出:
XYZ 23 20000
请先 登录 后评论