zoukankan      html  css  js  c++  java
  • Python面向对象--高级(二)

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

     1 class Person(object):
     2     pass
     3 
     4 
     5 Person.name = "Stanley"         # 在外部添加类属性
     6 print(Person.name)              # 输出:Stanley
     7 
     8 per1 = Person()
     9 per1.age = 22                   # 在外部添加实例属性
    10 print(per1.age)                 # 输出:22
    11 
    12 per2 = Person()
    13 # print(per2.age)               # 实例属性只由定义该属性的实例所有,其他的实例无法访问
    14 
    15 
    16 def outer_init(self, name):
    17     self.name = name
    18 
    19 
    20 Person.__init__ = outer_init    # 在外部修改类方法
    21 per3 = Person("Lily")
    22 print(per3.name)                # 输出:Lily, 说明类方法修改成功

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

     1 class Person(object):
     2     __slots__ = ("name", "age")     # 限制实例属性
     3     count = 1
     4 
     5 
     6 Person.nationality = "China"        # 仍然可以定义类属性
     7 print(Person.nationality)           # 输出:China
     8 
     9 
    10 per1 = Person()
    11 per1.name = "Lily"
    12 per1.age = 17
    13 
    14 per1.nationality = "China"
    15 # 类属性为只读属性,无法通过实例修改,只能通过类名修改
    16 # AttributeError: 'Person' object attribute 'nationality' is read-only
    17 
    18 per1.gender = "female"
    19 # 无法通过实例定义新实例属性
    20 # AttributeError: 'Person' object has no attribute 'gender'
    21 
    22 
    23 per1.count = 100
    24 # AttributeError: 'Person' object attribute 'count' is read-only

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

     1 class Person(object):
     2     @staticmethod
     3     def pursuit_happiness():
     4         print("幸福是奋斗出来的!")
     5 
     6 
     7 class Father(Person):
     8     character = "温和,坚韧"
     9 
    10 
    11 class Mather(Person):
    12     interest = "阅读,文艺竞技"
    13 
    14 
    15 class Student(Person):
    16     @staticmethod
    17     def do_homework():
    18         print("是学生就要做作业!")
    19 
    20 
    21 class Son(Father, Mather, Student):     # 多重继承
    22     pass
    23 
    24 
    25 s = Son()
    26 # Son类实例具有了Father类的属性
    27 print(s.character)              # 输出:温和,坚韧
    28 # Son类实例具有了Mather类的属性
    29 print(s.interest)               # 输出:阅读,文艺竞技
    30 # Son类实例具有了Student类的方法
    31 s.do_homework()                 # 输出:是学生就要做作业!
    32 # 由于Father类,Mather类,Student类都各自继承了Person类,所以Son类也有Person类的方法
    33 s.pursuit_happiness()           # 输出:幸福是奋斗出来的!

      - 类的组合使用

     1 class Car(object):
     2     def __init__(self, color, owner):
     3         self.color = color
     4         self.owner = owner
     5 
     6 
     7 class House(object):
     8     def __init__(self, location, owner):
     9         self.location = location
    10         self.owner = owner
    11 
    12 
    13 class Person(object):
    14     def __init__(self, name, car=None, house=None):
    15         self.name = name
    16         self.car = car
    17         self.house = house
    18 
    19 
    20 per1 = Person("Stanley")        # 实例化Person
    21 c = Car("Black", per1)          # 实例化Car
    22 h = House("China", per1)        # 实例化House
    23 per1.car = c                    # 把实例化的car给per1
    24 per1.house = h                  # 把实例化的house给per1
    25 
    26 # 通过person访问car的属性
    27 print(per1.car.color)           # 输出:Black
    28 # 通过person访问house的属性
    29 print(per1.house.location)      # 输出:China
    30 # 通过house和car访问person的属性
    31 print(h.owner.name)             # 输出:Stanley
    32 print(c.owner.name)             # 输出:Stanley

    本文参考:

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

  • 相关阅读:
    软件测试人员的年终绩效考核怎么应对
    收藏
    顶踩组件 前后两版
    订阅组件
    hdu 1963 Investment 完全背包
    hdu 4939 Stupid Tower Defense 动态规划
    hdu 4405 Aeroplane chess 动态规划
    cf 414B Mashmokh and ACM 动态规划
    BUPT 202 Chocolate Machine 动态规划
    hdu 3853 LOOPS 动态规划
  • 原文地址:https://www.cnblogs.com/hycstar/p/9254193.html
Copyright © 2011-2022 走看看