Python class 5

貢獻者:知乎:三生万物【标签:老朽意识】 類別:英文 時間:2020-06-15 11:34:28 收藏數:6 評分:0
返回上页 舉報此文章
请选择举报理由:




收藏到我的文章 改錯字
Last time we talked about the instance and constructor in instance.Today let us review them.
Instance is the object that possess corresponding characteristic in the class where it is from.
And if you want to define something for your instance in this type, or do something when
trying to initialise the action in the class, you may use constructor __init__,which are the
default entry of the class.For example:
Class Human:
\tdef __init__(self,name):(\t represent tab, which means 空4格 here)
\t\tself.name = name
\t\tself.run = False
\t\tself.running()
\tdef running(self):
\t\tself.run = True
human1 = Human("Zhangsan")#Instantiate one human with its attribute "name" is "Zhangsan"
print(human.run)#We have execute self.running() in constructor, the result would be True.
Here is the class with an instance method running and the constructor.When I instantiate the
human to the variable human1, what python is doing is going to the __init__, set the instance's
name as "Zhangsan",set the instance's run state as False(means not running),and then execute
the instance method,self.running(),same as Human.running(self).Then the self.run would
turned to be wrong.So now you have learned the basic part of class, you could attempt it on your
machine! Have fun and you would excavate more possibility!
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
文章熱度:
文章難度:
文章質量:
說明:系統根據文章的熱度、難度、質量自動認證,已認證的文章將參與打字排名!

本文打字排名TOP20