初入Python继承
2024-10-19 02:14:44
1.什么是继承?
新类不用从头编写
新类从现有的类继承,就自动拥有了现有类的所有功能
新类只需要编写现有类缺少的新功能
2.继承的好处
复用已有代码
自动拥有了现有类的所有功能
只需要编写缺少的新功能
3.父类和子类
#定义一个父类,也称为及基类,超类
class father(object):
def __init__(self,name,gender):
self.name = name
self.gender = gender #定义一个子类,也称为派生类,继承类
class child(father):
def __init__(self,name,gender,score):
#初始化父类,否则继承自father的child将没有name和gender
super(child,self).__init__(name,gender)
self.score = score t = child('Tom','Six','')
print t.name
4.继承的特点
子类和父类是is关系
class child(father):
pass p = father()
s = child()
p 是一个 father 但不是一个child
s 是一个father 同时也是一个person
最新文章
- 关于C#联接数据库是出现'未在本地计算机上注册'错误的解决办法
- 突袭HTML5之SVG 2D入门1 - SVG综述////////////////zzzzzzzz
- 网络存储技术介绍(2) ( based on zt)
- Office 2010启动时出现无法验证此应用程序的许可证的解决
- HTML常用的特殊字符格式
- JavaScript--匿名函数和闭包(16)
- 使用cocos2d-x制作 Texture unpacker
- Ubuntu 12.04 安装sougou for linux
- Android 自定义View修炼-自定义弹幕效果View
- 在SQL Server 实现递归
- jQuery设置checkbox全选(区别jQuery版本)
- shell编程-1到100的求和与冒泡排序
- tomcat 下部署 php
- js对象个人理解及记录
- Elasticsearch聚合——aggregation
- Android Intent 基本使用及对象构成
- Twisted 使用多线程
- AngularJS学习之旅—AngularJS 模块(十五)
- [NOI 2018] 归程
- mysql导出表的字段及相关属性
热门文章
- IOS-连接
- [AngularJS] Angular 1.5 $transclude with named slot
- Archlinux 从 AUR 源中安装应用
- http://msh.baidu.com/UTWpR6wY4722
- django: template - built-in tags
- java开发的web下载大数据时的异常处理
- ASP.net导出Excel的几种方式
- Arcgis - Personal Geodatabase 和 File Geodatabase的区别.
- YUI 之getLocation
- 在Yii2中使用Pjax导致Yii2内联脚本载入失败的问题