Python class 5

Contributor:知乎:三生万物【标签:老朽意识】 Type:English Date time:2020-06-15 11:34:28 Favorite:6 Score:0
返回上页 Report
请选择举报理由:




Collection Modify the typo
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!
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
Hot degree:
Difficulty:
quality:
Description: the system according to the heat, the difficulty, the quality of automatic certification, the certification of the article will be involved in typing!

This paper typing ranking TOP20