代码: class A(): def __init__(self,a,b): self.a =a self.b =b def add(self): print(self.a+self.b) B =A(3,4) B.add()
结果:
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
======================= RESTART: D:/selenium/test/5.py =======================
7
>>> __init__()方法为类A的初始化函数,一般在创建类时会首先声明这个函数,而add方法可以直接拿初始化方法的self.a和self.b两个参数进行计算
时间: 2025-01-04 10:52:48