In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to define classes in python. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Common method
There are two ways to create a common method (class A () & class B ()).
Class A (): def _ init__ (self, name, age): self.name = name self.age = age def get_name (self): print ('my name is', self.name) def get_age (self): print (Flemi am {self.age} years old') class B (): def get_name (self, name): print ('my name is', name) def get_age (self Age): print (Flemi am {age} years old') if _ _ name__ ='_ _ main__': a = A ('tom',19) a.get_name () # my name is tom a.get_age () # I am 19 years old b = B () b.get_name (' tom') # my name is tom b.get_age (19) # I am 19 years old
In class A (), _ _ init__ () is a special method, which is equivalent to initializing A. the self in _ _ init__ is the object An itself, and name and age are their formal parameters.
The class needs to be instantiated before each method call
This is the end of this article on "how to define classes in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 285
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.