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
  • 相关阅读:
    C++中四大强制类型转换!
    队列(queue)的实现
    栈(stack)的实现
    单向链表
    十种排序算法详解及C++实现
    extern “C”
    C语言内存分配及各种数据存储位置
    Python中的classmethod与staticmethod
    关于ORM,以及Python中SQLAlchemy的sessionmaker,scoped_session
    Python中的SQLAlchemy
  • 原文地址:https://www.cnblogs.com/p36606jp/p/15113364.html
Copyright © 2011-2022 走看看