zoukankan      html  css  js  c++  java
  • 笨办法42对象、类及从属关系

    # coding=utf-8
    class A:
        def __init__(self):
            self.n = 100    
    
        def add(self, m):
            self.n += m     # self.n = 3+4+1
    
    
    class B(A):
        def __init__(self):
            self.n = 3
    
        def add(self, m):
            super().add(m)  # m = 1
            self.n += 4     # self.n = 3+4
    
    b = B()         
    b.add(1)    # 从b里调用函数add,传递参数为1
    print(b.n)  # 用b里调用属性n

    执行结果:

    D:pytestvenvScriptspython.exe D:/pytest/ex42c.py
    9
    关于+= :
    a = 2
    b = 3
    a += b
    print(a)
    print(b)
    
    输出结果:
    5
    3
    
    即a+=b后,将结果返回给a

    class Animal(object):
        def __init__(self):
            print("animal")
        def a(self):
            print("animal2")
    
    
    class Person(Animal):
        def __init__(self):
            print("person1")
        def a(self):
            print("person2")
    
    
    class Employee(Person):
        def __init__(self):
            super().__init__()
            super().a()
            pass
    
    Employee().a()

    输出结果:

    D:pytestvenvScriptspython.exe D:/pytest/ex42b.py
    person1
    person2
    person2
  • 相关阅读:
    网络爬虫概述
    Redis常见问题汇总
    分布式锁和Redis事务
    Redis主从复制
    数据持久化
    位图操作bitmap
    数据类型:Hash散列数据类型
    进程池
    事件Event实现消费者模型
    事件Event
  • 原文地址:https://www.cnblogs.com/p36606jp/p/15113364.html
Copyright © 2011-2022 走看看