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

登录后可见