面向对象编程案例02--显示地调用父类的__init__()
2024-08-27 11:18:10
# -*- 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()
banana=Banana("blue")
banana.grow()
最新文章
- Python学习日志(二)
- Android使用HttpURLConnection通过POST方式发送java序列化对象
- 踢爆IT劣书出版黑幕——由清华大学出版社之《C语言入门很简单》想到的(1)
- 转!mysql 查询 distinct多个字段 注意!!
- pip install 出现报asciii码错误的问题
- jQuery插件:模拟select下拉菜单
- Android优秀开源项目
- docker虚拟化之将容器做成镜像
- vector查找元素
- sudo使用详细讲解
- ElasticSearch 6 Windows 安装
- CentOS下SVN服务的启动与关闭
- git安装和GitHub使用
- RuntimeError: Model class app_anme.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.---python学习错误记录
- SparkSQL demo
- 杭电1004 ac code
- 使用sqoop从mysql导入数据到hive
- 对HashMap的理解(二):高并发下的HashMap
- Mysql的概述
- e637. 向剪切板获取和粘贴文本
热门文章
- 系统吞吐量、TPS(QPS)、用户并发量、性能測试概念和公式
- Xilinx FFT IP v9.0 使用
- C# 图片识别(支持21种语言) (转)
- [leetcode]Surrounded Regions @ Python
- [leetcode]Permutations II @ Python
- ASP.NET中Session的个人浅谈
- Search Insert Position leetcode java
- variable_scope和name_scope差别
- webstorm和intellij idea下如何自动编译sass和scss文件
- [Spring boot] Read values from external properties file