Python class 5
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!
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!
Last one:Niche influencer
Next one:【夜读】很累的时候,别硬扛,别逞强
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
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
- 1游客13683097... 06-16 23:44251 KPM
- 2沐玥 06-17 18:15238 KPM
- 3pythondecora... 06-16 18:33220 KPM
- 4游客13496444... 06-17 16:57215 KPM
- 5游客13683559... 06-17 07:53207 KPM
- 6Douglaswan... 06-17 13:24196 KPM
- 7游客13559572... 06-16 22:03195 KPM
- 8游客13604531... 06-16 23:09190 KPM
- 9游客13685231... 06-17 16:32174 KPM
- 10游客13634691... 06-16 18:27170 KPM
- 11游客13050529... 06-16 21:12162 KPM
- 12游客13649792... 06-17 11:00160 KPM
- 13当落雪只能玩... 06-17 13:46157 KPM
- 14越今朝 06-15 21:55154 KPM
- 15下雨天遇到鬼 06-17 17:27148 KPM
- 16游客13426573... 07-07 08:12145 KPM
- 17游客13685640... 06-17 17:44137 KPM
- 18游客13369301... 06-16 19:08135 KPM
- 19游客13486446... 06-16 20:33134 KPM
- 20游客13681850... 06-17 09:00129 KPM
用户更多文章推荐
- 我们的认识如何构建2021-12-22
- Arch?2021-07-12
- o_O2021-07-12
- 哥林多前书13章4-8节(圣经)2021-01-30
- Nucleus(细胞核)2021-01-29
- A sea dirge(poem)2020-08-22
- 英文小说段落(3)2020-08-22
- 英文小说片段(2)2020-08-19
- 递归以及其利弊(1)2020-08-18
- 英文小说段落(1)2020-08-18
- 10 Bible Verses on Salvation 12020-08-18
- 奥古斯丁忏悔录【卷二】12020-07-05
- 床前明月光2020-07-02
- Python list method(1)2020-06-26
- Python class 42020-06-13
- Python class 32020-06-13
- Python class简介22020-04-29
- Python Class简介12020-04-29
- 圣经-歌罗西书-3章(1-17节)(英文)2020-04-20
- 圣经-传道书-11章-英文2020-04-17