请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生

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

[复制链接]

0

主题

0

好友

200

积分

中级会员

Rank: 3Rank: 3

发表于 2015-12-17 15:08:01 |显示全部楼层
crossin先生 发表于 2015-12-17 14:19
__init__是两个下划线,内置函数都是两个下划线

嗯...沙发就有说,但我对“__init__是两个下划线”的“两个”理解成,一边一个一共两个...
回复

使用道具 举报

0

主题

0

好友

200

积分

中级会员

Rank: 3Rank: 3

发表于 2015-12-17 15:14:12 |显示全部楼层
crossin先生 发表于 2014-6-28 17:00
因为作为子类,有自己专属的__init__方法和drive方法,不能直接套用父类的

子类Bike(Vehicle))应该是直接套用了父类吧?
不能直接套用父类,是当,子类需要有自己的专属的方法时,才这么说的吧。
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2015-12-17 22:10:29 |显示全部楼层
周末晒被子 发表于 2015-12-17 15:14
子类Bike(Vehicle))应该是直接套用了父类吧?
不能直接套用父类,是当,子类需要有自己的专属的方法时, ...

继承父类
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

1

好友

273

积分

中级会员

Rank: 3Rank: 3

发表于 2016-1-29 10:50:48 |显示全部楼层
get it!
论坛的形式很好,看了大家的讨论,又会多一些理解~~
回复

使用道具 举报

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)
复制代码
回复

使用道具 举报

0

主题

0

好友

64

积分

注册会员

Rank: 2

发表于 2016-3-13 09:19:53 |显示全部楼层
本帖最后由 弹星者 于 2016-3-13 09:26 编辑
  1. class Vehicle:
  2.     def __init__(self, speed):
  3.         self.speed = speed

  4.     def drive(self, distance):
  5.         print 'need %f hour(s)' % (distance / self.speed)

  6. class Bike(Vehicle):
  7.     pass

  8. class Car(Vehicle):
  9.     def __init__(self, speed, fuel):
  10.         Vehicle.__init__(self, speed)
  11.         self.fuel = fuel

  12.     def drive(self, distance):
  13.         Vehicle.drive(self, distance)
  14.         print 'need %f fuels' % (distance * self.fuel)

  15. b = Bike(15.0)
  16. c = Car(80.0, 0.012)
  17. b.drive(100.0)
  18. c.drive(100.0)

  19. d=Car(80,None)
  20. d.Vehicle.drive(100)

  21. #下面是运行报错
  22. '''
  23. need 6.666667 hour(s)
  24. need 1.250000 hour(s)
  25. need 1.200000 fuels
  26. <blockquote>Traceback (most recent call last):
复制代码
在crossin老师代码的最后添加了两行,想尝试一下子类调用父类里的函数,然而运行的时候报错了,这个错误为什么呀(代码复制的时候没复制好,在楼下重发一个
回复

使用道具 举报

0

主题

0

好友

64

积分

注册会员

Rank: 2

发表于 2016-3-13 09:27:47 |显示全部楼层
  1. class Vehicle:
  2.     def __init__(self, speed):
  3.         self.speed = speed

  4.     def drive(self, distance):
  5.         print 'need %f hour(s)' % (distance / self.speed)

  6. class Bike(Vehicle):
  7.     pass

  8. class Car(Vehicle):
  9.     def __init__(self, speed, fuel):
  10.         Vehicle.__init__(self, speed)
  11.         self.fuel = fuel

  12.     def drive(self, distance):
  13.         Vehicle.drive(self, distance)
  14.         print 'need %f fuels' % (distance * self.fuel)

  15. b = Bike(15.0)
  16. c = Car(80.0, 0.012)
  17. b.drive(100.0)
  18. c.drive(100.0)

  19. d=Car(80,None)
  20. d.Vehicle.drive(100)

  21. #下面是报错
  22. '''
  23. need 6.666667 hour(s)
  24. need 1.250000 hour(s)
  25. need 1.200000 fuels
  26. Traceback (most recent call last):
  27.   File "123.py", line 26, in <module>
  28.     d.Vehicle.drive(100)
  29. AttributeError: Car instance has no attribute 'Vehicle'
  30. '''
复制代码
在crossin老师代码的最后添加了两行,想尝试一下子类调用父类里的函数,然而运行的时候报错了,这个错误为什么呀
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2016-3-14 12:40:39 |显示全部楼层
弹星者 发表于 2016-3-13 09:27
在crossin老师代码的最后添加了两行,想尝试一下子类调用父类里的函数,然而运行的时候报错了,这个错误为 ...

没有这种语法。

一般不会这么用。如果硬要对一个子类对象去调用它被覆盖的父类方法,那首先,它的父类要继承自object:
  1. class Vehicle(obect):
  2. ...
复制代码
然后写成:
  1. super(Car, d).drive(100)
复制代码
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

2

主题

0

好友

94

积分

注册会员

Rank: 2

发表于 2016-4-13 11:06:58 |显示全部楼层
不懂就一点speed在整个程序中的作用是什么
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2016-4-13 21:16:26 |显示全部楼层
exchen 发表于 2016-4-13 11:06
不懂就一点speed在整个程序中的作用是什么

self.speed,这里speed就是对象的成员变量,是这个对象独有的属性
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

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

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

GMT+8, 2024-4-17 00:12 , Processed in 0.029588 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部