设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【Python 第50课】 面向对象(4)

[复制链接]

2

主题

0

好友

59

积分

注册会员

Rank: 2

楼主
发表于 2016-2-2 14:55:30 |显示全部楼层
这个面向对象看的不太懂,继续学习
  1. # -*- coding: UTF-8 -*-
  2. class Vehicle:
  3.         def __init__(self,speed):
  4.                 self.speed = speed
  5.                
  6.         def drive(self,distance):
  7.                 print 'need %f hour(s)' %(distance / self.speed)
  8.                
  9. class Bike(Vehicle):
  10.         pass

  11. class Car(Vehicle):
  12.         def __init__(self,speed,fuel):
  13.                 Vehicle.__init__(self,speed)
  14.                 self.fuel = fuel
  15.                
  16.         def drive(self,distance):
  17.                 Vehicle.drive(self,distance)
  18.                 print 'need %f fuels' % (distance * self.fuel)
  19.                
  20. b = Bike(15.0)
  21. c = Car(80.0,0.012)
  22. b.drive(100.0)
  23. c.drive(100.0)
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-3 16:56 , Processed in 0.015501 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部