Object-Oriented Programming
炒菜机器人.做菜('西红柿炒鸡蛋')
复制代码
Just as we can use the
def
statement from the definition of a function, we use theclass
statement from the definition of a class.
# 语法:创建一个名为“ClassName”的类,类名一般首字母要大写,(): 不能丢
class ClassName():
# 如定义一个名为'狗'的类,可以写成class Dog():
# 规范:class语句后续的代码块要缩进
def function1():
# 定义类中的函数1
复制代码
class 类A():
def 函数1():
print('报道!我是类A的第一个方法!')
def 函数2():
print('报道!我是类A的第二个方法!')
def 函数3():
print('报道!我是类A的第三个方法!')
复制代码
We use the
类名.函数名()
format, you can make class methods up and running
class 类A():
def 函数1():
print('报道!我是类A的第一个方法!')
def 函数2():
print('报道!我是类A的第二个方法!')
def 函数3():
print('报道!我是类A的第三个方法!')
类A.函数1()
类A.函数2()
类A.函数3()
复制代码
Inside the class, you can also add variable ...
class 类A():
变量1 = 100
变量2 = -5.83
变量3 = 'abc'
复制代码
Use
类名.变量名
format, the value of the attribute can be extracted from the class
class 类A():
变量1 = 100
变量2 = -5.83
变量3 = 'abc'
# 这里需要用print语句,才能把提取出的数值打印到屏幕上
print(类A.变量1)
print(类A.变量2)
print(类A.变量3)
复制代码
Variables can be output can also be assigned
I have an intelligent robot ...
class 智能机器人():
胸围 = 33
腰围 = 44
臀围 = 55
# 以上为类属性
def 打招呼():
print('主人你好!')
def 卖萌():
print('主人,求抱抱!')
def 生气():
print('主人,我要报警了!')
复制代码
Add IQ 200, add run, output ( 'I'm happy to run run ...... Oh Hey! Hit the wall.')
class 智能机器人():
胸围 = 33
腰围 = 44
臀围 = 55
智商 = 200
# 以上为类属性
def 打招呼():
print('主人你好!')
def 卖萌():
print('主人,求抱抱!')
def 生气():
print('主人,我要报警了!')
def 奔跑():
print('我快乐的奔跑, 奔跑 ... 哎哟喂, 撞墙了...')
智能机器人.奔跑()
复制代码
The difference between the methods and functions
类.函数名()
More than函数名()
one more class [.],But more importantly, the function "class" can use the "class" in the variable (ie, class methods can call the class attribute).
Guess the code is doing?
class 类A():
变量1 = 100
变量2 = 200
@classmethod
def 函数1(cls):
print(cls.变量1)
print(cls.变量2)
类A.函数1()
复制代码
Let blew a three-dimensional robot
The method of parameter passing to the class
For functions, parameters for internal use only ...
def 加100函数(参数):
总和 = 参数 + 100
print('计算结果如下:')
print(总和)
参数 = 1
加100函数(参数)
复制代码
With an object-oriented ...
# 请直接运行并体验代码效果
class 加100类():
def 加100函数(参数):
总和 = 参数 + 100
print('计算结果如下:')
print(总和)
参数 = 1
加100类.加100函数(参数)
复制代码
Object-oriented process-oriented transfer
一首诗 = ['《卜算子》','我住长江头,','君住长江尾。','日日思君不见君,','共饮长江水。']
def 念诗函数(参数):
for i in 参数:
print(i)
念诗函数(一首诗)
复制代码
External 传参
一首诗 = ['《卜算子》', '我住长江头,', '君住长江尾。', '日日思君不见君,', '共饮长江水。']
class 念诗类():
def 念诗函数(参数):
for i in 参数:
print(i)
念诗类.念诗函数(一首诗)
复制代码
Internal parameter passing
class 念诗类():
一首诗 = ['《卜算子》', '我住长江头,', '君住长江尾。', '日日思君不见君,', '共饮长江水。']
@classmethod
def 念诗函数(cls):
for i in cls.一首诗:
print(i)
念诗类.念诗函数()
复制代码
Class methods use internal and external parameters
class 加100类():
变量 = 100
@classmethod
def 加100函数(cls, 参数):
总和 = cls.变量 + 参数
print('加100函数计算结果如下:')
print(总和)
参数 = int(input('请输入一个整数:'))
加100类.加100函数(参数)
复制代码
class 加100类():
变量 = 100
@classmethod
def 加100函数(cls, 参数1, 参数2, 参数3):
总和 = cls.变量 + 参数1 + 参数2 + 参数3
print('加100函数计算结果如下:')
print(总和)
参数1 = int(input('请输入一个整数:'))
参数2 = int(input('请输入一个整数:'))
参数3 = int(input('请输入一个整数:'))
加100类.加100函数(参数1, 参数2, 参数3) # 不用显示的传入
复制代码
Add / modify class attributes
There are two ways to add or modify class attributes.
One is from the inside, with
类方法
to add / modify;The other is from the outside, with
类.变量 = xx
add / modify class properties directly.
class 类A():
pass
类A.变量1 = 100
print(类A.变量1)
复制代码
Test results
变量1 = 15
#这是类外部的变量1
变量2 = 'abc'
class 类A():
变量1 = 100
#这是类属性变量1
变量1 = 类A.变量1
类A.变量2 = 变量2
print(类A.变量1)
print(类A.变量2)
复制代码
Test results speak code
class 类():
@classmethod
def 打印类属性(cls):
print(cls.变量)
类.变量 = input('请输入字符串:')
类.打印类属性()
复制代码
Receiving a variable from outside the class attribute stored
你的幸运数是多少?请输入一个整数。(用户输入:66)
好的,我把它存了起来,然后翻了888倍还给你:58608
复制代码
class 幸运():
@classmethod
def 好运翻倍(cls):
print('好的,我把它存了起来,然后翻了888倍还给你:' + str(cls.幸运数*888))
# 或者这样写也可以:
# print('好的,我把它存了起来,然后翻了888倍还给你:%d' % (cls.幸运数*888))
幸运.幸运数 = int(input('你的幸运数是多少?请输入一个整数。'))
幸运.好运翻倍()
复制代码
Inner class add / modify class attributes.
class 念诗类():
一首诗 = ['《卜算子》','我住长江头,','君住长江尾。','日日思君不见君,','共饮长江水。']
@classmethod
def 念诗函数(cls,参数):
print('念给'+ 参数 +'的诗:')
for i in cls.一首诗:
print(i)
念诗类.念诗函数('张三')
复制代码
The desired effect
请输入你想给谁念诗:(用户输入“张三”)
念给张三的诗:
《卜算子》
我住长江头,
君住长江尾。
日日思君不见君,
共饮长江水。
复制代码
code show as below
class 念诗类():
一首诗 = ['《卜算子》','我住长江头,','君住长江尾。','日日思君不见君,','共饮长江水。']
@classmethod
def 念诗函数(cls):
cls.接收人 = input('请输入你想给谁念诗:')
print('念给'+ cls.接收人 +'的诗:')
for i in cls.一首诗:
print(i)
念诗类.念诗函数()
复制代码
Practical operation: save student achievement
class 成绩单():
@classmethod
def 录入成绩单(cls):
cls.学生姓名 = input('请输入学生姓名:')
cls.语文_成绩 = int(input('请输入语文成绩:'))
成绩单.录入成绩单()
print(成绩单.学生姓名 + '的成绩单如下:')
print('语文成绩:'+ str(成绩单.语文_成绩))
复制代码
class 成绩单():
@classmethod
def 录入成绩单(cls):
cls.学生姓名 = input('请输入学生姓名:')
cls.语文_成绩 = int(input('请输入语文成绩:'))
@classmethod
def 打印成绩单(cls):
print(cls.学生姓名 + '的成绩单如下:')
print('语文成绩:'+ str(cls.语文_成绩))
成绩单.录入成绩单()
成绩单.打印成绩单()
复制代码
Add a line math
class 成绩单():
@classmethod
def 录入成绩单(cls):
cls.学生姓名 = input('请输入学生姓名:')
cls.语文_成绩 = int(input('请输入语文成绩:'))
cls.数学_成绩 = int(input('请输入数学成绩:'))
@classmethod
def 打印成绩单(cls):
print(cls.学生姓名 + '的成绩单如下:')
print('语文成绩:'+ str(cls.语文_成绩))
print('数学成绩:'+ str(cls.数学_成绩))
成绩单.录入成绩单()
复制代码
I would like to print average
class 成绩单():
@classmethod
def 录入成绩单(cls):
cls.学生姓名 = input('请输入学生姓名:')
cls.语文_成绩 = int(input('请输入语文成绩:'))
cls.数学_成绩 = int(input('请输入数学成绩:'))
@classmethod
def 打印成绩单(cls):
print(cls.学生姓名 + '的成绩单如下:')
print('语文成绩:'+ str(cls.语文_成绩))
print('数学成绩:'+ str(cls.数学_成绩))
@classmethod
def 打印平均分(cls):
平均分 = (cls.语文_成绩 + cls.数学_成绩)/2
print(cls.学生姓名 + '的平均分是:' + str(平均分))
成绩单.录入成绩单()
成绩单.打印成绩单()
成绩单.打印平均分()
复制代码
I would like one more rating
@classmethod
def 评级(cls):
平均分 = (cls.语文_成绩 + cls.数学_成绩)/2
if 平均分>=90:
print(cls.学生姓名 + '的评级是:优')
elif 平均分>= 80 and 平均分<90 :
print(cls.学生姓名 + '的评级是:良')
elif 平均分>= 60 and 平均分<80 :
print(cls.学生姓名 + '的评级是:中')
else:
print(cls.学生姓名 + '的评级是:差')
成绩单.录入成绩单()
复制代码
The method of nesting (in the same class, method intermodulation)
class 成绩单():
@classmethod
def 录入成绩单(cls):
cls.学生姓名 = input('请输入学生姓名:')
cls.语文_成绩 = int(input('请输入语文成绩:'))
cls.数学_成绩 = int(input('请输入数学成绩:'))
@classmethod
def 计算平均分(cls):
平均分 = (cls.语文_成绩 + cls.数学_成绩)/2
return 平均分
@classmethod
def 评级(cls):
平均分 = cls.计算平均分()
if 平均分>=90:
print(cls.学生姓名 + '的评级是:优')
elif 平均分>= 80 and 平均分<90 :
print(cls.学生姓名 + '的评级是:良')
elif 平均分>= 60 and 平均分<80 :
print(cls.学生姓名 + '的评级是:中')
else:
print(cls.学生姓名 + '的评级是:差')
成绩单.录入成绩单()
成绩单.评级()
复制代码
Small exercises
为类calendar添加两个方法:一个删除完成项,一个添加新增项。
由于日程安排是事情和时间的配对,所以我们用字典来存放:
{'给父母买礼物':'9:00', '学习':'10:00', '和朋友聚会':'18:30'}
接下来,需要你新建两个类方法,从而实现字典中的数据增减。
复制代码
The desired effect:
Quick Jump:
Cat brother teach you to write reptile 000-- begins .md
cat brother teach you to write reptile 001 - print () functions and variables .md
cat brother teach you to write reptile 002-- job - Pikachu .md print
cat brother teach you to write reptiles 003 data type conversion .md
cat brother teach you to write reptile 004-- data type conversion - small practice .md
cat brother teach you to write reptile 005-- data type conversion - small jobs .md
cat brother teach you to write reptile 006- - conditional and nested conditions .md
cat brother teach you to write 007 reptile conditional and nested conditions - small operating .md
cat brother teach you to write reptile 008 - input () function .md
cat brother teach you to write reptiles 009 - input () function - AI little love students .md
cat brother teach you to write a list of 010 reptiles, dictionaries, circulation .md
cat brother teach you to write reptile 011-- lists, dictionaries, circulation - small jobs .md
cat brother teach you to write a Boolean value, and four reptile 012-- statements .md
cat brother teach you to write a Boolean value, and four reptile 013-- statements - smaller jobs .md
cat brother teach you to write reptile 014 - pk game. md
cat brother teach you to write reptile 015 - pk game (new revision) .md
cat brother teach you to write reptile 016-- function .md
cat brother teach you to write reptile 017-- function - a small job .md
cat brother to teach you write reptile 018--debug.md
cat brother teach you to write reptile 019 - debug- job. md
cat brother teach you to write reptiles 020-- Classes and Objects (on) .md
cat brother teach you to write reptiles 021-- Classes and Objects (a) - Job .md
Cat brother teach you to write reptiles 022-- Classes and Objects (lower) .md
cat brother teach you to write reptiles 023-- Classes and Objects (lower) - Job .md
cat brother teach you to write reptile 024-- decoding coded && .md
cat brother teach you to write reptile 025 && decoding coded - small jobs .md
cat brother teach you to write reptile 026-- module .md
cat brother teach you to write reptile 027-- module introduces .md
cat brother teach you to write reptile 028- - introduction module - small job - billboards .md
cat brother teach you to write Preliminary -requests.md reptile reptilian 029--
cat brother teach you to write reptile reptilian 030-- Preliminary -requests- job .md
cat brother teach you to write 031 reptiles - reptile basis -html.md
cat brother teach you to write reptile reptilian 032-- first experience -BeautifulSoup.md
cat brother teach you to write reptile reptilian 033-- first experience -BeautifulSoup- job .md
cat brother teach you to write reptile 034- - reptile -BeautifulSoup practice .md
cat brother teach you to write 035-- reptile reptilian -BeautifulSoup practice - job - film top250.md
cat brother teach you to write 036-- reptile reptilian -BeautifulSoup practice - work - work to resolve .md movie top250-
cat brother teach you to write 037-- reptile reptiles - to listen to songs .md baby
cat brother teach you to write reptile 038-- arguments request .md
cat brother teach you to write data stored reptile 039-- .md
cat brother teach you to write reptiles 040-- store data - Job .md
cat brother teach you to write reptile 041-- analog login -cookie.md
Cat brother teach you to write reptile 042 - session usage .md
cat brother teach you to write reptile 043-- analog browser .md
cat brother teach you to write reptile 044-- analog browser - job .md
cat brother teach you to write reptiles 045-- coroutine .md
cat brother teach you to write reptile 046-- coroutine - practice - what to eat not fat .md
cat brother teach you to write reptile 047 - scrapy framework .md
cat brother teach you to write reptile 048-- .md reptile reptiles and anti-
cat brother teach you to write reptile 049-- end Sahua .md
Reproduced in: https: //juejin.im/post/5cfc4ad8f265da1b7c610b39