#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2017/11/7 22:46
# @Author : lijunjiang
# @File : class2.py """
类的重写 在子类中,使用与父类中相同变量或方法名,可以重新定义父类中的属性和方法 """
class A():
def hello(self):
print('Hello, i am A')
class B(A):
pass
a = A()
b = B()
a.hello()
b.hello()
# 执行结果:
# Hello, i am A
# Hello, i am A
# 上例中可以看出class B 继承 class A 后具有了A 的方法hello() # 重写 hello 方法 class C(A):
def hello(self): # 重写hello()方法
print('Hello, I am class C')
c = C()
c.hello()
# 执行结果:Hello, I am class C
# 当在class C 中 重新定义与父类 class A 同名的方法hellp() 后,class C 的实例将不再调用class A 中定义的hello()方法而调用
# 自身定义的hello() 方法
# 即, 子类定义父类同名函数之后,父类函数被覆盖 # 重定__init__方法 class Person():
def __init__(self):
self.name = 'Person'
# print('Tist is a class Person') class Man(Person):
def __init__(self):
self.sex = 'M'
# print('I am class Man, my name is {0}'.format(name)) # person = Person()
# print(person.name)
# man = Man()
# print(man.name)
# print(man.sex)
# 执行结果:
# File "D:/Python/class/class2.py", line 54, in <module>
# Person
# print(man.name)
# AttributeError: Man instance has no attribute 'name' # 执行报错:子类Man 重写__init__构造函数后,没有从父类Person中继承其name属性
# 构造函数__init__,是每个类的默认方法,如上例在子类中重写了__init__方法后,其默认方法被重写,导致父类中的默认方法
# 无法被调用,失去其构造函数的作用
# 此时,如果在子类中重写__init__方法,且继承父类中的属性和方法时,可以使用方法 super()
# super(className, self).functionName([没有self] argv ) class Person():
def __init__(self):
self.name = 'Person'
# print('Tist is a class Person') class Gril(Person):
def __init__(self):
super(Gril, self).__init__()
self.sex = 'nvhai'
print('Class name is {0}'.format(self.sex)) gril = Gril()
print(gril.name) # 执行结果:
# File "D:/Python/class/class2.py", line 80, in <module>
# gril = Gril()
# File "D:/Python/class/class2.py", line 76, in __init__
# super(Gril, self).__init__()
# TypeError: super() argument 1 must be type, not classobj # 原因:定义父类的时候,一定要写继承object类,不然那就会有如下的报错信息:TypeError: super() argument 1 must be type, not classobj,增加parent(object)就可以轻松解决。 class Person(object): # 执行时需注释上面已声明过的Person类和Man 类
def __init__(self):
self.name = 'Person'
# print('Tist is a class Person') class Gril(Person):
def __init__(self):
super(Gril, self).__init__()
self.sex = 'nvhai'
print('Class name is {0}'.format(self.sex)) gril = Gril()
print(gril.name)
# 执行结果:
# Class name is nvhai
# Person
# 使用super(className,self).functionName(没有self!!)的重点是不需要提供父类,这意味着如果改变了类继承关系,只需要改变一行代码(class C(P)),此时寻找基类的事由super函数完成
# 但其继承的类中必须有一个继承超级类 object

Python 类的重写的更多相关文章

  1. python 基础 5.3 类的重写

    一. 类的重写 只需要重新定义类的属性(变量),就是累的重写了 示例:重新定义类grandson的 name属性   #/usr/bin/python #coding=utf-8 #@Time :20 ...

  2. Python中自定义类如果重写了__repr__方法为什么会影响到str的输出?

    这是因为Python3中,str的输出是调用类的实例方法__str__来输出,如果__str__方法没有重写,则自动继承object类的__str__方法,而object类的__str__方法是调用_ ...

  3. Python类的特点 (1):构造函数与方法

    Python中,类的特点: #encoding:utf-8 class Parent(object): x=1 #x是Parent类的属性(字段) def __init__(self): print ...

  4. 再学python类(终结篇)

    续写 初学python类,这几天吃坏东西了,拖着虚弱的身躯写的.有些乱请各位看官海涵. 声明:本人编程新手,还在学习中.所表述的东西都是基础语法之类的,分享我的学习笔记.还望多多指点,我一定虚心接受. ...

  5. 【Python&数据结构】 抽象数据类型 Python类机制和异常

    这篇是<数据结构与算法Python语言描述>的笔记,但是大头在Python类机制和面向对象编程的说明上面.我也不知道该放什么分类了..总之之前也没怎么认真接触过基于类而不是独立函数的Pyt ...

  6. python 类知识点总结

    python 类知识点总结 面向对象思想: 1.设计的时候,一定要明确应用场景 2.由对象分析定义类的时候,找不到共同特征和技能不用强求 1.简述类.对象.实例化.实例这些名词的含义: 类:从一组对象 ...

  7. 细说python类2——类动态添加方法和slots(转)

    先说一下类添加属性方法和实例添加属性和方法的区别, 类添加属性属于加了一个以类为全局的属性(据说叫静态属性),那么以后类的每一个实例化,都具有这个属性.给类加一个方法也如此,以后类的每一个实例化都具备 ...

  8. python类的相关知识第二部分

    类的继承.多态.封装 一.类的继承 1.应用场景: 类大部分功能相同,大类包含小类的情况 例如: 动物类 共性:都要吃喝拉撒.都有头有脚 特性: 猫类.走了很轻,叫声特别,喜欢白天睡觉 狗类.的叫声很 ...

  9. 学习python类

    类:Python 类提供了面向对象编程的所有基本特征: 允许多继承的类继承机制, 派生类可以重写它父类的任何方法, 一个方法可以调用父类中重名的方法. 对象可以包含任意数量和类型的数据成员. 作为模块 ...

随机推荐

  1. 基于python3.7的一个闯越自动签到脚本--demo版

    望指正demo的定位,有时候会抽风无法接受我的定位信息 #! /usr/bin/python3 # -*- coding:UTF- -*- # time : // : # file : chuangy ...

  2. Susan Sontag【苏珊·桑塔格】

    Sunsan Sontag Sunsan Sontag was one of the most noticeable figures in the world of literature. 苏珊·桑塔 ...

  3. Artwork 18年中南多校第一场A

    一.题意 对于一个矩阵,若干道命令,每道命令将会把某一段格子涂黑,请问每次涂黑之后矩阵中未被涂黑的块的数量? 二.思路 保存每道命令,并且忠实的执行他,到最后一步开始搜索联通块的数量,并将其保存. 之 ...

  4. RxJava Rxandroid retrofit

    其实Retrofit会了.集合RxJava,RxAndroid 就很简单了. 只需要改几个地方. 1.接口里面返回的对象不再是 call,而是Observable public interface A ...

  5. Install ADDS on Windows Server 2012 R2 with PowerShell

    Install ADDS on Windows Server 2012 R2 with PowerShell Posted by ethernuno on 20/04/2014 In this tut ...

  6. R语言分析朝阳医院数据

    R语言分析朝阳医院数据 本次实践通过分析朝阳医院2016年销售数据,得出“月均消费次数”.“月均消费金额”.“客单价”.“消费趋势”等结果,并据此作出可视化图形. 一.读取数据: library(op ...

  7. 【Jump Game】cpp

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  8. CentOS 7 编译安装最新版git

    安装wget yum install wget -y 下载最新版本的git源码,并解压 cd /usr/local/src/ wget https://mirrors.edge.kernel.org/ ...

  9. ibatis selectKey

    <insert id="insert" parameterClass="A"> <selectKey keyProperty="uu ...

  10. 软工实践第八次作业——UML设计

    本次作业博客 团队组成 临时组长:何裕捷 组员:蔡子阳,陈德斌,胡青元,李麒,高裕翔,王焕仁,黄培鑫 UML 用例图 描述的部分: 1 这里是用户个人管理系统的用例图 面临的问题: 1 面临用户登录注 ...