Python Learning Notes (5)--class

Source: Internet
Author: User

1, empty class, Occupy space

1 class Animal (object): 2     Pass

2. Define __INIT__ () null

1 class Animal (object): 2     def __init__ (): 3         Pass

3. Declare name

class Animal (object):     def __init__ (self,name):         == Animal ('Jeffrey')print  zebra.name  #Jeffrey

4. Another def

classAnimal (object):"""makes cute animals."""is_alive=Truedef __init__(self, Name, age): Self.name=name Self.age= Age#Add your method here!    defdescription (self):PrintSelf.namePrintSelf.agehippo= Animal ('AA', 3). Description ()#AA#3

5. Inheritance

1 classShape (object):2     """makes shapes!"""3     def __init__(self, number_of_sides):4Self.number_of_sides =Number_of_sides5 6 #Add your Triangle class below!7 classTriangle (Shape):8     def __init__(SELF,SIDE1,SIDE2,SIDE3):9Self.side1 =Side1TenSelf.side2 =Side2 OneSelf.side3 = Side3

6, Inheritance. Replication

1 classEmployee (object):2     """Models real-life employees!"""3     def __init__(self, employee_name):4Self.employee_name =Employee_Name5 6     defcalculate_wage (Self, hours):7Self.hours =hours8         returnHours * 20.009 Ten #Add Your code below! One classParttimeemployee (Employee): A     defcalculate_wage (self,hours): -Self.hours =hours -         return12*hours

7. Super class

1 classEmployee (object):2     """Models real-life employees!"""3     def __init__(self, employee_name):4Self.employee_name =Employee_Name5 6     defcalculate_wage (Self, hours):7Self.hours =hours8         returnHours * 20.009 Ten #Add Your code below! One classParttimeemployee (Employee): A     defcalculate_wage (self,hours): -Self.hours =hours -         return12*hours the     deffull_time_wage (self,hours): -         returnSuper (Parttimeemployee,self) calculate_wage (Hours)#returns the parent class function -  -Milton = Parttimeemployee ('AA') + PrintMilton.full_time_wage (10) - # $

Example

classTriangle (object): Number_of_sides= 3def __init__(self,angle1,angle2,angle3): Self.angle1=angle1 Self.angle2=Angle2 Self.angle3=Angle3defcheck_angles (self):ifSelf.angle1+self.angle2+self.angle3 = = 180:            returnTrueElse:            returnFalsemilton= Triangle (60,60,60)Printmilton.check_angles ()#True
1 classTriangle (object):2Number_of_sides = 33     def __init__(self,angle1,angle2,angle3):4Self.angle1 =Angle15Self.angle2 =Angle26Self.angle3 =Angle37     defcheck_angles (self):8         ifSelf.angle1+self.angle2+self.angle3 = = 180:9             returnTrueTen         Else: One             returnFalse A              - classequilateral (Triangle): -Angle = 60 the     def __init__(self): -Self.angle1 =Self.angle -Self.angle2 =Self.angle -Self.angle3 =Self.angle +My_triangle = triangle (60,60,60) - PrintMy_triangle.number_of_sides + PrintMy_triangle.check_angles ()

Python Learning Notes (5)--class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.