python学习笔记(5)—— class

  1、空class,占空间

1 class Animal(object):
2     pass

  2、定义__init__()空

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

  3、declare name

class Animal(object):
    def __init__(self,name):
        self.name = name

zebra = Animal('Jeffrey')
print zebra.name
#Jeffrey

  4、another def

class Animal(object):
    """Makes cute animals."""
    is_alive = True
    def __init__(self, name, age):
        self.name = name
        self.age = age
    # Add your method here!
    def description(self):
        print self.name
        print self.age
hippo = Animal('aa',3).description()
#aa
#3

  5、继承

 1 class Shape(object):
 2     """Makes shapes!"""
 3     def __init__(self, number_of_sides):
 4         self.number_of_sides = number_of_sides
 5 
 6 # Add your Triangle class below!
 7 class Triangle(Shape):
 8     def __init__(self,side1,side2,side3):
 9         self.side1 = side1
10         self.side2 = side2
11         self.side3 = side3

  6、继承.复写

 1 class Employee(object):
 2     """Models real-life employees!"""
 3     def __init__(self, employee_name):
 4         self.employee_name = employee_name
 5 
 6     def calculate_wage(self, hours):
 7         self.hours = hours
 8         return hours * 20.00
 9 
10 # Add your code below!
11 class PartTimeEmployee(Employee):
12     def calculate_wage(self,hours):
13         self.hours = hours
14         return 12*hours

  7、super class

 1 class Employee(object):
 2     """Models real-life employees!"""
 3     def __init__(self, employee_name):
 4         self.employee_name = employee_name
 5 
 6     def calculate_wage(self, hours):
 7         self.hours = hours
 8         return hours * 20.00
 9 
10 # Add your code below!
11 class PartTimeEmployee(Employee):
12     def calculate_wage(self,hours):
13         self.hours = hours
14         return 12*hours
15     def full_time_wage(self,hours):
16         return super(PartTimeEmployee,self).calculate_wage(hours)#返回父class函数
17 
18 milton = PartTimeEmployee('aa')
19 print milton.full_time_wage(10)
20 #200

Example

class Triangle(object):
    number_of_sides = 3
    def __init__(self,angle1,angle2,angle3):
        self.angle1  = angle1
        self.angle2  = angle2
        self.angle3  = angle3
    def check_angles(self):
        if self.angle1+self.angle2+self.angle3 == 180:
            return True
        else:
            return False
milton = Triangle(60,60,60)
print milton.check_angles()
#True
 1 class Triangle(object):
 2     number_of_sides = 3
 3     def __init__(self,angle1,angle2,angle3):
 4         self.angle1  = angle1
 5         self.angle2  = angle2
 6         self.angle3  = angle3
 7     def check_angles(self):
 8         if self.angle1+self.angle2+self.angle3 == 180:
 9             return True
10         else:
11             return False
12             
13 class Equilateral(Triangle):
14     angle = 60
15     def __init__(self):
16         self.angle1 = self.angle
17         self.angle2 = self.angle
18         self.angle3 = self.angle
19 my_triangle  = Triangle(60,60,60)
20 print my_triangle.number_of_sides
21 print my_triangle.check_angles()

 

 1 class Car(object):
 2     condition = "new"
 3     def __init__(self, model, color, mpg):
 4         self.model = model
 5         self.color = color
 6         self.mpg   = mpg
 7     def display_car(self):
 8         return "This is a %s %s with %s MPG." %  (self.color,self.model,str(self.mpg))
 9     def drive_car(self):
10         self.condition = 'used'
11 class ElectricCar (Car):
12     def __init__(self,model,color,mpg,battery_type):
13         super(ElectricCar,self).__init__(model,color,mpg)
14         self.battery_type = battery_type
15 #my_car = Car("DeLorean", "silver", 88)
16 my_car = ElectricCar("DeLorean", "silver", 88,'molten salt')
17 print my_car.battery_type
18 print my_car.model
19 print my_car.color
20 print my_car.mpg
21 #my_car.drive_car()
22 #print my_car.condition

 

 1 class Point3D(object):
 2     def __init__(self,x,y,z):
 3         self.x = x
 4         self.y = y
 5         self.z = z
 6     def __repr__(self):
 7         return "(%d, %d, %d)" % (self.x, self.y, self.z)
 8 my_point = Point3D(1,2,3)
 9 print my_point
10 #(1,2,3)

 

转载于:https://www.cnblogs.com/andrew-elec90/p/4111528.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值