2022年 11月 7日

Python中的调用

1.在同一个类内,函数之间互相调用

首先类中的方法在定义时需要先加参数self

class cat():
    def __init__(self,age):
        cat.age = age
        
    def show_color(self):
        print("The color of the cat is white")
        
    def show_age(self):
        self.show_color()#调用show_color方法,类里面的方法想调用另外一个方法,需要在方法体中用self.方法名的方式来调用
        print("The age of the cat is "+str(self.age))#类里面的方法想调用类的属性,需要用self.属性名的方式

Ragdoll = cat(2)
Ragdoll.show_age()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

输出:
在这里插入图片描述

注:其中,Ragdoll调用了show_age方法,但因为在show_age中调用了show_color方法,所以两个方法最后都执行了。

2.类外中,def函数之间的调用

def show_num(num):
    print(num)

def test(num):
    if num>3:
        show_num(num)#只需要函数名可实现调用彼此
test(4)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注:类外 的函数调用,只需要函数名可实现调用彼此
参数列表要保持一致,如果要调用的函数参数列表中有多个参数,那么自身函数的参数列表中也要有那些参数

错误示范:

def create_cat(color,age):
    print("The age and color of the cat are"+str(age)+color)
    
def show_cat():
    create_cat(color,age)

show_cat()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

正确调用

def create_cat(color,age):
    print("The age and color of the cat are "+str(age)+" and "+color)
    
def show_cat(color,age):#其中调用了create_cat,参数列表中也要有create_cat中的参数
    create_cat(color,age)

show_cat("white",3)#也要有方法的参数

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3.在类外的函数,需要调用类内的方法

class cat():
    def __init__(self,age):
        self.age = age
        
    def age_increase(self):
        self.age += 1
        print("the age of the cat is "+str(self.age))
        

def year():
    Ragdoll = cat(3)
    Ragdoll.age_increase()

year()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

输出结果:
在这里插入图片描述
创建该类的实例对象,再通过实例调用方法,最后运行函数即可

class cat():
    def __init__(self,age):
        self.age = age
        self.color = "white"
        
    def age_increase(self):
        self.age += 1
        print("the age of the cat is "+str(self.age))
        

def year():
    Ragdoll = cat(3)
    Ragdoll.age_increase()
    print(Ragdoll.color)

year()        

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

输出结果:
在这里插入图片描述

4.不同类之间的方法调用

class cat():
    def __init__(self, age1):
        self.age1 = age1
        
    def show_age(self):
        print("the age of the cat is "+str(self.age1))
        
class dog():
    def __int__(self, age2):
        self.age2 = age2
    
    def show_cat_age(self):
        Radoll = cat(3)#调用cat类中的方法
        Radoll.show_age()
        
Husky = dog(2)
Husky.show_cat_age()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

想在这个类里面调用其他类里面的属性值,则需要这样做:

class cat():
    def __init__(self,age1):
        self.age1 = age1
        self.color = "white"
        
    def show_age(self):
        print("the age of the cat is "+str(self.age1))

class dog():
    def __init__(self,age2,Ragdoll):
        self.age2 = age2
        self.Ragdoll = Ragdoll
    
    def show_cat_age(self,Ragdoll):
        Ragdoll = cat(3)#调用方法,首先要调用类,且类中有参数
        Ragdoll.show_age()#调用方法
        print(self.Ragdoll.color)
        if self.Ragdoll.age1 > self.age2:
            print("the cat is older than the dog")
        
def run():
    Ragdoll = cat(3)
    Husky = dog(2,Ragdoll)#需要将对象作为参数放入类里面
    Husky.show_cat_age(Ragdoll)

run()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

输出结果:
在这里插入图片描述
原文章