当前位置:网站首页>类的初始化与回调的用法
类的初始化与回调的用法
2022-06-27 23:59:00 【菜鸟要爱学习】
学习目标:
正确区分类的初始化与回调
有关类的初始化
class A():
def __init__(self):
print('Hello, world!')
A()

有关类的调用
class A():
def __call__(self):
print('hello,world!!!!!!!!!!!!!!')
a = A()
a()

这就是__init__与__call__在用法上的区别!
关于二者在编程中具体应用
示例一
class A():
def __init__(self):
print('hello,world!')
def __call__(self):
print('hello,world!!!!!!!!!!!!!')
a = A()
a()

示例二
class A():
def __init__(self, init_age):
print('我年龄是:', init_age)
self.age = init_age
def __call__(self, added_age):
res = self.forward(added_age) #调用forward函数
return res
def forward(self, input_):
print('forward 函数被调用了')
return input_ + self.age
print('对象初始化………………')
a = A(16)
input_param = a(2)
print("我现在的年龄永远是:", input_param)

示例三
class Animal():
def __init__(self,name):
self.name = name
def greet(self):
print('animal is {}'.format(self.name))
class Cat(Animal):
def greet(self):
super(Cat, self).greet()
print("It's name is wangwang")
a=Cat('cat')
a.greet()

边栏推荐
- 药物发现综述-01-药物发现概述
- Golang monkeys eat peaches and ask for the number of peaches on the first day
- PV操作原语
- Adobe Premiere Basics - common video effects (corner positioning, mosaic, blur, sharpen, handwriting tools, effect control hierarchy) (16)
- 【DNS 解析】将Name.com的域名接入DNSPod解析
- Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)
- How to build an e-commerce platform at low cost
- MapReduce elementary programming practice
- 零基础多图详解图神经网络
- 数据人面试指南 | 准备好这几点做到有备无患!
猜你喜欢

Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)

Web3 技术初体验以及相关学习资料

面试官问:JS的继承

药物发现综述-03-分子设计与优化

Ten thousand words long article understanding business intelligence (BI) | recommended collection

DeepMind | 通过去噪来进行分子性质预测的预训练

Machine learning notes - time series as features

Adobe Premiere Basics - general operations for editing material files (offline files, replacing materials, material labels and grouping, material enabling, convenient adjustment of opacity, project pa

Review of drug discovery-02-prediction of molecular properties

Adobe Premiere foundation - sound adjustment (volume correction, noise reduction, telephone tone, pitch shifter, parameter equalizer) (XVIII)
随机推荐
【永艺XY椅】试用体验
如何阅读一篇论文
要搞清楚什么是同步,异步,串行,并行,并发,进程,线程,协程
[embedded foundation] serial port communication
fiddle如何使用代理
766. 托普利茨矩阵
对比学习中的4种经典训练模式
ionic4实现半星评分
【MySQL】-【函数】
The contents of the latex table are left, middle and right
Is it safe to open an online futures account?
MySQL - function
Scala 基础 (三):运算符和流程控制
万字长文看懂商业智能(BI)|推荐收藏
Import the data table in MySQL into Excel
力扣今日题-522. 最长特殊序列
TI AM3352/54/59 工业核心板硬件说明书
PV operation primitive
Interface component telerik UI for WPF Getting Started Guide - how to switch custom styles using themes
[Yocto RM]1 - System Requirements