学习记录:类和方法

'''
# 定义add()函数
def add(a,b):
    print(a+b)

# 调用 add() 函数
add(3,5)
add(a=1,b=1)
'''

'''
# 利用 return 返回值
def add(a,b):
    return a + b
c = add(3, 5)
print(c)

'''

'''
# add() 函数设置默认参数
def add(a=1, b=2):
    return a + b
c1 = add()
c2 = add(3,5)
print(c1)
print(c2)
'''



'''
# 类和方法
# class MyClass(object):
#     def say_hello(self, name):
#         return 'hello, ' + name

# mc = MyClass()
# print(mc.say_hello('jay'))

'''


'''
class A:
    def __init__ (self, a, b):
        self.a = int(a)
        self.b = int(b)
    
    def add(self):
        return self.a + self.b
# 调用类时需要传入初始化参数
count = A(4, 5)
print(count.add())

# B类继承A类
class B(A):
    def sub(self, a, b):
        return a - b
print(B(2, 3).add())

'''

猜你喜欢

转载自blog.csdn.net/qq_26086231/article/details/114240230