(实例解析)Python 函数调用的几种方式(类里面,类之间,类外面)

您所在的位置:网站首页 python不同类间参数传递 (实例解析)Python 函数调用的几种方式(类里面,类之间,类外面)

(实例解析)Python 函数调用的几种方式(类里面,类之间,类外面)

2024-07-14 05:09:11| 来源: 网络整理| 查看: 265

第一种 是在class内部,方法之间的互相调用

举一个简单的例子

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() print("The age of the cat is "+str(self.age)) Ragdoll = cat(2) Ragdoll.show_age()

结果为 在这里插入图片描述 这里我只用实例Ragdoll调用了show_age方法,但因为在show_age中调用了show_color方法,所以两个方法最后都执行了

类里面的方法想调用另外一个方法,需要在方法体中用self.方法名的方式来调用

同样,类里面的方法想调用类的属性,需要用self.属性名的方式

第二种 是在类的外面,def函数之间的彼此调用

还是来看一个简单的例子

def show_num(num): print(num) def test(num): if num>3: show_num(num) test(4)

结果为 在这里插入图片描述 在类外面的函数之间,只需要函数名就可以调用彼此

另外要记得参数列表保持一致,如果你要调用的函数参数列表中有多个参数,那么自身这个函数的参数列表中也要有那些参数

以下是错误示范

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()

结果是 在这里插入图片描述 正确的函数调用是这样

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(color,age) show_cat("white",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()

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

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()

在函数中直接用实例调用属性,获取属性值也是可以的 结果为 在这里插入图片描述

第四种 是不同的类之间的方法的彼此调用 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 __init__(self,age2): self.age2 = age2 def show_cat_age(self): Ragdoll = cat(3) Ragdoll.show_age() Husky = dog(2) Husky.show_cat_age()

结果为 在这里插入图片描述 而如果想在这个类里面调用其他类里面的属性值,则需要这样做

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()

结果为 在这里插入图片描述

需要将对象作为参数放入类里面



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭