python类的继承-1
#!/usr/bin/python3 #类定义
class people:
#定义基本属性
name = ''
age = 0
#定义私有属性,私有属性在类外部无法直接进行访问
__weight = 0
#定义构造方法
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s 说: 我 %d 岁。" %(self.name,self.age)) #单继承示例
class student(people):
grade = ''
def __init__(self,n,a,w,g):
#调用父类的构函,否则继承自people的student将没有n、a、w属性!!!!!!!!!!!!!!!
people.__init__(self,n,a,w)#
self.grade = g
#覆写父类的方法
def speak(self):
print("%s 说: 我 %d 岁了,我在读 %d 年级"%(self.name,self.age,self.grade)) s = student('ken',10,60,3)
s.speak()
结果是:
若是上述代码没有第23行,代码如下:
#!/usr/bin/python3 #类定义
class people:
#定义基本属性
name = ''
age = 0
#定义私有属性,私有属性在类外部无法直接进行访问
__weight = 0
#定义构造方法
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s 说: 我 %d 岁。" %(self.name,self.age)) #其实也可以用people.age访问类属性,但是此时实例化后调用此句时类属性age的值为0。 #单继承示例
class student(people):
grade = ''
def __init__(self,n,a,w,g):
#调用父类的构函
#people.__init__(self,n,a,w)
self.grade = g
#覆写父类的方法
def speak(self):
print("%s 说: 我 %d 岁了,我在读 %d 年级"%(self.name,self.age,self.grade)) s = student('ken',10,60,3)
s.speak()
结果为:
name为空,age为0,这不是直接继承的父类people的属性 name = '' age = 0 么,也就是第31行
s = student('ken',10,60,3) name和age只是传递到了子类student中,子类由于继承了父类的属性和方法,但父类的初始化(构造函数)函数却没有被自动执行。
参考:
http://blog.csdn.net/goodluckac/article/details/53100957
http://www.runoob.com/python3/python3-class.html
python类的继承-1的更多相关文章
- Python类的继承(进阶5)
转载请标明出处: http://www.cnblogs.com/why168888/p/6411918.html 本文出自:[Edwin博客园] Python类的继承(进阶5) 1. python中什 ...
- 孤荷凌寒自学python第二十二天python类的继承
孤荷凌寒自学python第二十二天python类的继承 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) python中定义的类可以继承自其它类,所谓继承的概念,我的理解 是,就是一个类B继承自 ...
- python 类定义 继承
0 前言 系统:win7 64bit IDE : python(x,y) 2.7.6.1 IDE集成的解释器:Python 2.7.6 (default, Nov 10 2013, 19:24:18) ...
- 记录Python类与继承的一个错误
今天在学python的类与继承的时候遇到一个错误,原来是自己在ctrl+c ctrl+v的时候漏了一个括号 class Car(): def __init__(self,make,year,mode ...
- Python 类的继承__init__() takes exactly 3 arguments (1 given)
类(class),可以继承基类以便形成具有自己独特属性的类,我们在面向对象的编程中,经常用到类及其继承,可以说没有什么不是类的,今天我们就来详细探讨一下在python中,类的继承是如何做的. 我们假设 ...
- day30 python类的继承,抽象类等
Python之路,Day17 = Python基础17-面向对象入门 继承 class Student(People): pass print(Student.__bases__) # 查看 Stud ...
- python类、继承
Python 是一种面向对象的编程语言.Python 中的几乎所有东西都是对象,拥有属性和方法.类(Class)类似对象构造函数,或者是用于创建对象的"蓝图". 一.python ...
- python类的继承和多态,获取对象信息
继承 类的继承机制使得子类可以继承父类中定义的方法,拥有父类的财产,比如有一个Animal的类作为父类,它有一个eat方法: class Animal(object): def __init__(se ...
- python类的继承、多继承及其常用魔术方法
继承 一个类可以派生出一个子类,这个子类可以使用父类的属性及方法,也可以在父类的基础上添加自己的独特属性或方法.属性和方法的继承的顺序是先从自己开始,找不到再去找父类,父类没有再找父类的父类,其尽头就 ...
- python类的继承
继承一个类 如果已经定义了Person类,需要定义新的Student和Teacher类时,可以直接从Person类继承: class Person(object): def __init__(self ...
随机推荐
- fb 发布桌面应用图标
1.以src文件夹为根目录,即图标放在src文件内 2.修改app.xml文件icon,按尺寸填入,如下图 同时可修改app应用的名字,接受中文,如下图红色涂鸦: 导出发行版的时候,注意打包内容有么有 ...
- springBoot异常处理
1.status=404 Whitelabel Error Page Whitelabel Error Page This application has no explicit mapping fo ...
- ubuntu 安装u盘恢复
XP下进入CMD命令窗体,Vista及7/8下右键以管理员方式运行DOS窗体(win8.1:开始屏幕-windows系统-命令提示符) 输入DISKPART,会显示计算机名,及DISKPART> ...
- 2018.8.14-C#复习笔记总
using System; using System.Collections.Generic; //using System.Linq; using System.Text; using System ...
- Graylog安装配置
ES集群健康检测:curl -sXGET http://localhost:9200/_cluster/health?pretty=true | grep "status" | a ...
- DNN例子
[Tensorflow DNNClassifier ValueError]http://stackoverflow.com/questions/40264622/tensorflow-dnnclass ...
- dns server 配置
# cat /etc/named.conf//// named.conf//// Provided by Red Hat bind package to configure the ISC BIND ...
- Nexus 使用配置
Nexus使用的一些基本设置 1.更改中央仓库地址为私服地址 既然我们配置了私服,那么相应的,我们的项目就应该使用Nexus的地址(Public Repository)来下载jar包 1.1.基于PO ...
- mysql连接数据库存报下面错误:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
输入 mysql -u root 登录 mysql 的时候出现以下错误: ERROR 2002 (HY000): Can't connect to local MySQL server through ...
- np.identity()
二.np.identity()这个函数和之前的区别在于,这个只能创建方阵,也就是N=M 函数的原型:np.identity(n,dtype=None) 参数:n,int型表示的是输出的矩阵的行数和列数 ...