# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#面向对象编程案例02--显示地调用父类的__init__() '''
继承是面向对象的重要特征之一,继承是2个类或多个类之间的父子关系,子类继承父类的所有共有实例变量和方法。
继承实现了代码的重用,减少代码的编写量
python在类名后用圆括号来表示继承关系,括号中的类表示父类
如果父类有init方法,则子类会显示地调用其父类的init方法,不需要重写定义。如果子类需要拓展父类的init方法,则可以父类的基础上继续拓展和添加新的属性和方法
''' class Fruit():
def __init__(self,color):
self.color=color
print '__init__()首先被调用,',self.color
def grow(self):
print 'grow():','xiaodeng' class Apple(Fruit):
def __init__(self,color):
Fruit.__init__(self,color)#显示地调用父类的__init__()
print "apple\'s color:",self.color class Banana(Fruit):
def __init__(self,color):
Fruit.__init__(self,color)
print 'banana\'s color:',self.color
def grow(self):#覆盖父类的grow方法,对grow方法进行了重写
print 'fengmei' if __name__=="__main__":
apple=Apple("red")
print '---'*10
apple.grow()
print
print
banana=Banana("blue")
banana.grow()

最新文章

  1. Python学习日志(二)
  2. Android使用HttpURLConnection通过POST方式发送java序列化对象
  3. 踢爆IT劣书出版黑幕——由清华大学出版社之《C语言入门很简单》想到的(1)
  4. 转!mysql 查询 distinct多个字段 注意!!
  5. pip install 出现报asciii码错误的问题
  6. jQuery插件:模拟select下拉菜单
  7. Android优秀开源项目
  8. docker虚拟化之将容器做成镜像
  9. vector查找元素
  10. sudo使用详细讲解
  11. ElasticSearch 6 Windows 安装
  12. CentOS下SVN服务的启动与关闭
  13. git安装和GitHub使用
  14. RuntimeError: Model class app_anme.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.---python学习错误记录
  15. SparkSQL demo
  16. 杭电1004 ac code
  17. 使用sqoop从mysql导入数据到hive
  18. 对HashMap的理解(二):高并发下的HashMap
  19. Mysql的概述
  20. e637. 向剪切板获取和粘贴文本

热门文章

  1. 系统吞吐量、TPS(QPS)、用户并发量、性能測试概念和公式
  2. Xilinx FFT IP v9.0 使用
  3. C# 图片识别(支持21种语言) (转)
  4. [leetcode]Surrounded Regions @ Python
  5. [leetcode]Permutations II @ Python
  6. ASP.NET中Session的个人浅谈
  7. Search Insert Position leetcode java
  8. variable_scope和name_scope差别
  9. webstorm和intellij idea下如何自动编译sass和scss文件
  10. [Spring boot] Read values from external properties file