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
  • 相关阅读:
    HTML初识
    使用python操作Memcache、Redis、RabbitMQ、
    使用salt-cloud创建虚拟机
    运维堡垒机----Gateone
    ELK日志分析系统
    Python MySQL API
    浅谈Java中static作用--转
    oracle如何设置最大连接数
    转--oracle查看允许的最大连接数和当前连接数等信息
    oracle 查看未关闭连接
  • 原文地址:https://www.cnblogs.com/p36606jp/p/15113364.html
Copyright © 2011-2022 走看看