09 类的继承

 

继承一个类

class Person(object):

    def __init__(self, name, gender):

        self.name = name

        self.gender = gender

 

class Student(Person):

    def __init__(self, name, gender, score):

        super(Student, self).__init__(name, gender)

        self.score = score

 

判断类型

isinstance()可以判断类型

isinstance(p, Person)

 

多态

动态语言和静态语言(例如java)的最大差别是,不检查类型,只要方法存在,参数正确,就可以调用。

 

支持多重继承

 

获取对象信息

type()    # 获取对象类型

dir()    # 获取变量所有属性字符串list,可结合filter()获得定制的属性列表

 

已知屬性名:

getattr(s, 'name')

setattr(s, 'name', 'Adam')

 

 

特殊方法

定义在class中,不需要直接调用,Python的某些函数或操作符会调用对应的特殊方法。

 

__getattr__

 

__setattr__

__delattr__

 

__str__    # 给print看的信息

__repr__    # 给开发人员看的信息

__cmp__    #排序用

__len__    #让len()函数正常工作的话,就必须定义此方法,返回元素个数

 

四则运算:

__add__

__sub__

__mul__

__div__

 

__int__    #让int()函数工作

__float__    #让float()函数工作

 

 

__slots__    #一个类允许的属性列表,即限制当前类所能拥有的属性

class Student(object):

    __slots__=('name', 'gender', 'score')

    def __init__(self, name, gender, score):

        self.name = name

        self.gender = gender

        self.score = score

 

__call__    #将实例变爲可调用对象

在Python中,函数其实是一个对象;

所有的函数都是可调用对象;

一个类实例也可以变成一个可调用对象,只需要实现一个特殊方法__call__()

例子:把Person类实例变成一个可调用对象:

class Person(object):

    def __init__(self, name, gender):

        self.name = name

        self.gender = gender

   

    def __call__(self, friend)

        print 'My name is %s...' % self.name

        print 'My friend is %s...' % friend

 

>>> p = Person('Bob', 'male')

>>> p('Tim')

My name is Bob...

My friend is Tim...

 

对私有属性的访问


用get/set方法来封装对一个属性的访问在许多面向对象编程的语言中都很常见。

但是写s.get_score()和s.set_score()方法没有写s.score来的直接。

可以用装饰器函数

class Student(object):

    def __init__(self, name, score):

        self.name = name

        self.__score = score

    @property

    def score(self):

        return self.__score

    @score.setter

    def score(self, score):

        if score < 0 or score > 100:

            raise ValueError('invalid score')

        self.__score = score

 

第一个score(self)是get方法,用@property装饰

第二个score(self, score)是set方法,用@score.setter装饰,是@property装饰后的副产品。

现在就可以像使用属性一样设置score了。

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/387105.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue 中v-if 与v-show 的区别

相同点或者说功能&#xff0c;都可以动态操作dom元素的显示隐藏 不同点&#xff1a; 1.手段&#xff1a;v-if是动态的向DOM树内添加或者删除DOM元素&#xff1b;v-show是通过设置DOM元素的display样式属性控制显隐&#xff1b;2.编译过程&#xff1a;v-if切换有一个局部编译/卸…

vue打包后放在 nginx部署时候的配置文件

部署了三套程序,默认的&#xff0c;admin和design#user nobody; worker_processes 1;#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024; }http {include …

淘淘商城之技术选型、开发工具和环境、人员配置

一、技术选型 1&#xff09;Spring、SpringMVC、Mybatis 2&#xff09;JSP、JSTL、jQuery、jQuery plugin、EasyUI、KindEditor&#xff08;富文本编辑器&#xff09;、CSSDIV 3&#xff09;Redis&#xff08;缓存服务器&#xff09; 4&#xff09;Solr&#xff08;搜索&#x…

启动代码格式:nginx安装目录地址 -c nginx配置文件地址

启动启动代码格式&#xff1a;nginx安装目录地址 -c nginx配置文件地址 例如&#xff1a;[rootLinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf停止nginx的停止有三种方式&#xff1a; 从容停止1、查看进程号[rootLinuxServer ~]# ps -ef…

Lecture 3 Divide and Conquer

1.Divide the problem(instance) into one or more sub-problem; 2.Conquer each sub-problem recursively; 3.Combine solutions.

Maven报错找不到jre

富人之所以越来越富&#xff0c;穷人之所以越来越穷&#xff0c;中产阶级之所以总是在债务泥潭中挣扎&#xff0c;其主要原因之一在于他们对金钱的观念不是来自学校&#xff0c;而是来自家庭。 ---《穷爸爸富爸爸》 一、报错提示 常规配置maven环境变量&#xff0c;报错&#x…

vue按照url地址访问出错404

问题描述&#xff1a; 最近在开发cms的时候使用Vue.js框架&#xff0c;利用vue-route结合webpack编写了一个单页路由项目&#xff0c;自己在服务器端配置nginx。部署完成后&#xff0c;访问没问题&#xff0c;从页面中点击跳转也没问题&#xff0c;但是只要点击刷新或通过浏览器…

Lecture 4 Quick Sort and Randomized Quick Sort

Quick Sort --Divide and Conquer --Sorts “in place” --Very practical with tuning Divide and Conquer: 1.Divide: Partition array into 2 sub-arrays around pivot x such that elements in lower sub-array < x < elements in upper sub-array; 2.Conquer: …

HDU 3966 树链剖分后线段树维护

题意: 一棵树, 操作1.$path(a,b)$之间的点权$k$ 操作2.单点查询 题解: 树链剖分即可,注意代码细节,双向映射 主要是记录一下板子 #include <string.h> #include <stdio.h> #include <algorithm> #define endl \n #define ll long long #define ull unsigned …

VUE config/index.js文件配置

&#xfeff;&#xfeff; 当我们需要和后台分离部署的时候&#xff0c;必须配置config/index.js: 用vue-cli 自动构建的目录里面 &#xff08;环境变量及其基本变量的配置&#xff09;123456789101112131415var path require(path)module.exports {build: {index: path.res…

数据规则列表加导入导出

1.进入bos&#xff0c;打开数据规则&#xff0c;进入列表菜单 2.点击事件-新增操作 3.点击新增 4.点击操作类型&#xff0c;输入%引入 5.点击确定&#xff0c;保存后生效&#xff0c;导出 、引入模板设置同理转载于:https://www.cnblogs.com/RogerLu/p/10643521.html

Lecture 6 Order Statistics

Given n elements in array, find kth smallest element (element of rank k) Worst-case linear time order statistics --by Blum, Floyd, Pratt, Rivest, Tarjan --idea: generate good pivot recursively. Not so hot, because the constant is pretty big.

C++ qsort() 函数调用时实参与形参不兼容的问题解决

《剑指OFFER》刷题笔记 —— 扑克牌顺子 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿&#xff01;&#xff01;“红心A…

linux jenkins部署之路之,ftp部署怎么匿名还好用咋解决思密达

怎么安装就不说了&#xff0c;网上一堆 这噶搭是配置 目录是/etc/vsftpd/vsftpd.conf # Example config file /etc/vsftpd/vsftpd.conf# # The default compiled in settings are fairly paranoid. This sample file # loosens things up a bit, to make the ftp daemon more u…

powerCat进行常规tcp端口转发

实战中&#xff0c;我们也会遇到需要我们进行端口转发的情况&#xff0c;比如已经拿下的目标机1是在dmz区&#xff0c;而目标1所在内网的其他目标只能通过目标1去访问&#xff0c;这时候我们就需要端口转发或者代理来进行后渗透。这次就要介绍一个加强版的nc&#xff0c;基于po…

Lecture 7 Hashing Table I

Hash |---Hash function: Division, Multiplication |---Collision: Chaining, Open addressing(Linear,Double hasing) Symbol-table problem: Table S holding n records pointer --> key|satelite data (record) Hashing: Hash function h maps keys “randomly”…

SpringCloud 微服务

一微服务架构概述1.1 微服务特性以及优点每个服务可以独立运行在自己的进程里一系列独立运行的微服务(goods,order,pay,user,search…)共同构建了整个系统每个服务为独立的业务开发&#xff0c;一个微服务只关注某个特定的功能&#xff0c;例如用户管理&#xff0c;商品管理微服…

window起别名

http://www.bagualu.net/wordpress/archives/1714 转载于:https://www.cnblogs.com/wei-huan/p/10654026.html

vue在ie9中的兼容问题

问题总结 https://github.com/vuejs-templates/webpack/issues/260 首先npm install --save babel-polyfill然后在main.js中的最前面引入babel-polyfillimport babel-polyfill在index.html 加入以下代码&#xff08;非必须&#xff09;<meta http-equiv"X-UA-Compatib…