Python class 4

贡献者:知乎:三生万物【标签:老朽意识】 类别:英文 时间:2020-06-13 21:38:16 收藏数:4 评分:0
返回上页 举报此文章
请选择举报理由:




收藏到我的文章 改错字
Class Human:
def __init__(self,name):
self.name = name
self.run = "False"
def running(self):
self.run = "True"
human1 = Human("Peter")
print(human1.run)#Would print out "False"
human1.running()
print(human1.run)#Would print out "True"
Ok, if we want to have many human in our program and each one has its state.For instance
,maybe we want to have a human named Zhangsan, and another human named Lisi.So, to specific
the individual one which share the same character of human type,
we now introduce a concept named "instance",
that means 实例,and what we do in Human("Peter") is to instantiate(实例化 a instance
which it's attribute "name" is "Peter".And thus we could modify individual instance and grantee
not influenced other instance.And "__init__"(initialise) is called constructor,used to initialise
the attribute of that instance(like the "name","run" in the example).Constructor is important
since its the default entry while instantiate an instance,python would automatically go through
the code in the __init__ to initialise your instance. When you initialise an instance, the only
place python would go by default is the code under __init__.
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
文章热度:
文章难度:
文章质量:
说明:系统根据文章的热度、难度、质量自动认证,已认证的文章将参与打字排名!

本文打字排名TOP20

登录后可见