In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you the Python object-oriented programming example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
1. Design a circle class, including center position, radius, color and other attributes. Write a construction method to calculate the perimeter and area.
Import mathclass Circle: def _ _ init__ (self,o,r Colour): self.o = o self.r = r self.colour = colour def length (self): # perimeter C = math.pi*self.r*2 return C def area (self): # area S = math.pi*self.r**2 return S if _ _ name__ = "_ _ main__": r=eval ("Please enter the radius: ") circle = Circle ((0recom 0)) ) # coordinate, radius, color C = circle.length () S = circle.area () print ('circle circumference: {: .2f}' .format (C)) print ('circle area: {: .2f}' .format (S))
two。 A class describing the motion of free fall is designed, which requires that the speed and displacement at any time can be obtained and tested. The known acceleration of gravity is 9.8m/s.
Class Free: def _ _ init__ (self T): self.t = t def getv (self): # Speed function v=9.8*self.t return v def getx (self): # displacement function x=0.5*9.8*self.t**2 return x if _ _ name__ = = "_ _ main__": t=eval (input ("Please enter the time t:) f=Free (t) # call Print ("t = {} moment" .format (t) End='') print ("speed: {: .2f}" .format (f.getv () print ("t = {} moment" .format (t), end='') print ("displacement: {: .2f}" .format (f.getx ()
3. Design a class named Equation for the quadratic equation ax ²+ bx+c=0, which includes:
Member variables a, b, c representing three coefficients
A construction method with parameters a, b, c
A method named getDiscriminant1 returns the value of the discriminant
A method named getRoot1 and getRoot2 returns two roots of the equation, and if the discriminant is negative, these methods return 0.
Import mathclass Equation: def _ _ init__ (self,a,b C): self.a = a self.b = b self.c = c def getDiscriminant1 (self): # Discriminant d = (self.b) * * 2-(4*self.a*self.c) return d def getRoot1 (self): if self.getDiscriminant1 () > = 0: x1 = (- self.b+math.sqrt (self.getDiscriminant1 () / 2*self.a Return x1 else: return 0 def getRoot2 (self): if self.getDiscriminant1 () > = 0: x2 = (- self.b+math.sqrt (self.getDiscriminant1 () / 2*self.a return x2 else: return 0 if _ _ name__ = = "_ _ main__": a=eval ("Please enter a factor a:") B=eval (input ("Please enter coefficient b:") c=eval (input ("Please enter Factor c:") f=Equation (a) Print ("Discriminant:", f.getDiscriminant1 ()) print ("Root 1:", f.getRoot1 ()) print ("Root 2:", f.getRoot2 ())
4. A two-dimensional vector class is designed to realize the addition and subtraction of vectors and the multiplication and division of vectors and scalars, in which the input is required to be tuples.
Class Vector: def _ init__ (self,a,b): self.a = a self.b = b def jia (self): C1 = (self.a [0] + self.b [0]) c2 = (self.a [1] + self.b [1]) return (C1) C2) def jian (self): C1 = (self.a [0]-self.b [0]) c2 = (self.a [1]-self.b [1]) return (C1) C2) def diancheng (self): C1 = (self.a [0] * self.b [0]) c2 = (self.a [1] * self.b [1]) return c1+c2 # Vector non-division if _ _ name__ = = "_ _ main__": a=eval ("please enter vector a:") b=eval (input ("please enter vector b:") f=Vector (a B) print ("plus:", f.jia ()) print ("minus:", f.jian ()) print ("dot multiplication:", f.diancheng ())
5. Design a course class, including course number, course name, teacher, place and other attributes, set the place variable to private, and add the method of construction and the method of displaying course information.
Class Course: def _ _ init__ (self,Cno,Cname,teacher,local): self.Cno = Cno self.Cname = Cname self.teacher = teacher self.__local = local # set the venue variable to private def information (self): print ("course number:", self.Cno) print ("course name:", self.Cname) print ("instructor:" Self.teacher) print ("venue:", self.__local) if _ _ name__ = "_ _ main__": f=Course ('NO.','what','who','where') f.information ()
6. Design a wrapper class for an integer array that requires the following functions:
Show all array data
Displays a contiguous array of data starting from a location.
Import randomclass List: def _ _ init__ (self,lis): self.__lis=lis def quanbu (self): # all array data return self.__lis def bufen (self Id): # A piece of continuous array data starting from a partial position return self.__ lish [id:] f=List ([random.randint (1mem51) for i in range (20)]) # sets the array to 20 random number combinations of 1-50 ("Show all array data:" F.quanbu () random_index = random.randint (0Power21) # randomly select the index number print ("Show a continuous array of data from a location: .format (random_index), f.bufen (random_index)). These are all the contents of the article" sample Analysis of programming in Python object-oriented " Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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: 0
*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.