## 使用__slots__限制类的属性
  - 之前说到,可以通过在类外部实例或者类名任意定义实例属性或者类属性或者方法

 class Person(object):
pass Person.name = "Stanley" # 在外部添加类属性
print(Person.name) # 输出:Stanley per1 = Person()
per1.age = 22 # 在外部添加实例属性
print(per1.age) # 输出:22 per2 = Person()
# print(per2.age) # 实例属性只由定义该属性的实例所有,其他的实例无法访问 def outer_init(self, name):
self.name = name Person.__init__ = outer_init # 在外部修改类方法
per3 = Person("Lily")
print(per3.name) # 输出:Lily, 说明类方法修改成功

  - 若想要限制实例的属性,可以使用__slots__

 class Person(object):
__slots__ = ("name", "age") # 限制实例属性
count = 1 Person.nationality = "China" # 仍然可以定义类属性
print(Person.nationality) # 输出:China per1 = Person()
per1.name = "Lily"
per1.age = 17 per1.nationality = "China"
# 类属性为只读属性,无法通过实例修改,只能通过类名修改
# AttributeError: 'Person' object attribute 'nationality' is read-only per1.gender = "female"
# 无法通过实例定义新实例属性
# AttributeError: 'Person' object has no attribute 'gender' per1.count = 100
# AttributeError: 'Person' object attribute 'count' is read-only

## 多重继承
  - Python是允许多重继承的,多重继承时代子类拥有多重特征

 class Person(object):
@staticmethod
def pursuit_happiness():
print("幸福是奋斗出来的!") class Father(Person):
character = "温和,坚韧" class Mather(Person):
interest = "阅读,文艺竞技" class Student(Person):
@staticmethod
def do_homework():
print("是学生就要做作业!") class Son(Father, Mather, Student): # 多重继承
pass s = Son()
# Son类实例具有了Father类的属性
print(s.character) # 输出:温和,坚韧
# Son类实例具有了Mather类的属性
print(s.interest) # 输出:阅读,文艺竞技
# Son类实例具有了Student类的方法
s.do_homework() # 输出:是学生就要做作业!
# 由于Father类,Mather类,Student类都各自继承了Person类,所以Son类也有Person类的方法
s.pursuit_happiness() # 输出:幸福是奋斗出来的!

  - 类的组合使用

 class Car(object):
def __init__(self, color, owner):
self.color = color
self.owner = owner class House(object):
def __init__(self, location, owner):
self.location = location
self.owner = owner class Person(object):
def __init__(self, name, car=None, house=None):
self.name = name
self.car = car
self.house = house per1 = Person("Stanley") # 实例化Person
c = Car("Black", per1) # 实例化Car
h = House("China", per1) # 实例化House
per1.car = c # 把实例化的car给per1
per1.house = h # 把实例化的house给per1 # 通过person访问car的属性
print(per1.car.color) # 输出:Black
# 通过person访问house的属性
print(per1.house.location) # 输出:China
# 通过house和car访问person的属性
print(h.owner.name) # 输出:Stanley
print(c.owner.name) # 输出:Stanley

本文参考:

  [美]Bill Lubanovic 《Python语言及其应用》
  https://www.liaoxuefeng.com 廖雪峰的官方网站

Python面向对象--高级(二)的更多相关文章

  1. Python之路【第十二篇】:Python面向对象高级

    一.反射 1 什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究 ...

  2. python 面向对象高级应用(三)

    目录: isinstance(obj,cls)和issubclass(sub,super) 反射 __setattr__,__delattr__,__getattr__ 二次加工标准类型(包装) __ ...

  3. Python学习笔记【第十一篇】:Python面向对象高级

    isinstance(obj,cls)和issubclass(sub,super) class Person(object): def __init__(self, name, age, sex, n ...

  4. python面向对象高级:Mixin多重继承

    继上一篇学习笔记:python面向对象的继承与多态,本篇就Mixin扩展类的方法写下学习笔记 Mixin Mixin编程是一种开发模式,是一种将多个类中的功能单元的进行组合的利用的方式,这听起来就像是 ...

  5. python 面向对象高级编程

    数据封装.继承和多态只是面向对象程序设计中最基础的3个概念.在Python中,面向对象还有很多高级特性,允许我们写出非常强大的功能. 我们会讨论多重继承.定制类.元类等概念.

  6. python - 面向对象(二)

    类的三大特性 类的三大特性包括: 封装.继承.多态 一.封装 封装就是将类所用到的所有字段.属性.方法都包含在类代码段里面,当实例调用直接调用类中的方法即可. class People(object) ...

  7. 17、Python面向对象高级

    一.isinstance和issubclass type():不会认为子类实例是一种父类类型: isinstance():认为子类实例是一种父类类型. issubclass():判断是否为其子类. c ...

  8. #3 Python面向对象(二)

    前言 上一节主要记录面向对象编程的思想以及Python类的简单创建,这节继续深入类中变量的相关知识,Here we go! Python中类的各种变量 1.1 类变量 类变量定义:在类中,在函数体(方 ...

  9. 初学Python——面向对象(二)

    一.抽象类.接口类和抽象接口 转自博客园魏恒https://www.cnblogs.com/weihengblog/p/8528967.html (一)接口类 什么是接口类?在继承中,我们可以声明某个 ...

随机推荐

  1. C# FTPHelper帮助类

    网上的FTPHelper类感觉用起来不方便,而且代码的质量也不高,因此自己重新写了一个FTPHelper.此文之前是发布在我的CSDN博客中的,现在转过来. 主要就是借鉴了DbHelper的Creat ...

  2. 【起航计划 013】2015 起航计划 Android APIDemo的魔鬼步伐 12 App->Activity->SetWallpaper 设置壁纸 WallpaperManager getDrawingCache使用

    SetWallpaper介绍如何在Android获取当前Wallpaper,对Wallpaper做些修改,然后用修改后的图像重新设置Wallpaper.(即设置>显示>壁纸>壁纸的功 ...

  3. http 状态码集合

    HTTP常见状态码 200 301 302 404 500   HTTP状态码(HTTP Status Code) 状态码并不是每个都有,为了后期扩展.[update20170505] 一些常见的状态 ...

  4. 08、Spark常用RDD变换

    08.Spark常用RDD变换 8.1 概述 Spark RDD内部提供了很多变换操作,可以使用对数据的各种处理.同时,针对KV类型的操作,对应的方法封装在PairRDDFunctions trait ...

  5. ansible安装php

    环境:Centos 7.x 独立php-fpm.conf配置文件 [root@master playbook]# tree php php ├── php-fpm.conf └── php.yml p ...

  6. meta详解(常用)

    1.<meta http-equiv="X-UA-Compatible" content="IE=edge"> 说明:设置浏览器的兼容模式版本.表示 ...

  7. iOS开发:小技巧积累2

    http://blog.sina.com.cn/s/articlelist_1935098904_1_1.html .获取全局的Delegate对象,这样我们可以调用这个对象里的方法和变量: [(My ...

  8. 模仿ArcGIS用Graphics重绘的直方图分级调节器

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  9. [ difflib] simple1.py

    #!/usr/bin/env python # _*_ coding:utf-8 _*_ import difflib text1 = """text1: # 定义字符串 ...

  10. JSON对象与XML相互转换工具类

    依赖jar <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId ...