我之前也写了一篇关于面向对象编程的博客,两篇博客内容相近,包含了我这两次学习的全部内容
第一篇博客
a, b = 5, 10
# 方法1:
print('%d * %d = %d' % (a, b, a * b))
# 方法2:
print('{0} * {1} = {2}'.format(a, b, a * b))
# 方法3:
print(f'{a} * {b} = {a * b}') # 适用于python 3.6以后的版本
对于面向对象,我想大家都不陌生,但是怎么简要的概括什么是面向对象呢?这是一个很有意思的问题。我学习过c语言、c#、java等编程语言,但是对面向对象的解释依然不是很明确。这是一直困扰着我的问题,什么是面向对象?
"把一组数据结构和处理它们的方法组成对象(object),把相同行为的对象归纳为类(class),通过类的封装(encapsulation)隐藏内部细节,通过继承(inheritance)实现类的特化(specialization)和泛化(generalization),通过多态(polymorphism)实现基于对象类型的动态分派。"这样的解释也许不好明白
class Student(object):
# __init__是一个特殊方法用于在创建对象时进行初始化操作
# 通过这个方法我们可以为学生对象绑定name和age两个属性
def __init__(self, name, age):
self.name = name
self.age = age
def study(self, courseName):
print('%s正在学习%s.' % (self.name, courseName))
# PEP 8要求标识符的名字用全小写多个单词用下划线连接
# 但是部分程序员和公司更倾向于使用驼峰命名法(驼峰标识)
def watchMovie(self):
if self.age < 12:
print('%s观看《喜羊羊》.' % self.name)
else:
print('%s快去学校上课.' % self.name)
def main():
# 创建学生对象并指定姓名和年龄
stu1 = Student('amy', 21)
# 给对象发study消息
stu1.study('Python')
# 给对象发watchMovie消息
stu1.watchMovie()
stu2 = Student('李小白', 15)
stu2.study('高等数学')
stu2.watchMovie()
if __name__ == '__main__':
main()
class Test:
def __init__(self, name):
self.__name = Jackson
def __bar(self):
print(self.__name)
print('__age')
def main():
test = Test('hello')
test.__age() # AttributeError: 'Test' object has no attribute '__age'
print(test.__name) # AttributeError: 'Test' object has no attribute '__name'
if __name__ == "__main__":
main()
import random
def generateCode(codeLen=4):
"""
生成指定长度的验证码
:param codeLen: 验证码的长度(默认4个字符)
:return: 由大小写英文字母和数字构成的随机验证码
"""
allChars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
lastPos = len(allChars) - 1 # 生成allChars的长度,应为:61
code = '' # 以空格隔开
for _ in range(codeLen):
index = random.randint(0, lastPos)
code += allChars[index]
return code
def main():
num = int(input('请输入行数为: '))
yh = [[]] * num
for row in range(len(yh)):
yh[row] = [None] * (row + 1)
for col in range(len(yh[row])):
if col == 0 or col == row:
yh[row][col] = 1
else:
yh[row][col] = yh[row - 1][col] + yh[row - 1][col - 1]
print(yh[row][col], end='\t')
print()
if __name__ == '__main__':
main()
from random import randrange, randint, sample
def display(balls):
"""
输出列表中的双色球号码
"""
for index, ball in enumerate(balls):
if index == len(balls) - 1:
print('|', end=' ')
print('%02d' % ball, end=' ')
print()
def random_select():
"""
随机选择一组号码
"""
red_balls = [x for x in range(1, 34)]
selected_balls = []
selected_balls = sample(red_balls, 6)
selected_balls.sort()
selected_balls.append(randint(1, 16))
return selected_balls
def main():
n = int(input('机选几注: '))
for _ in range(n):
display(random_select())
if __name__ == '__main__':
main()
from time import sleep
class Clock(object):
"""数字时钟"""
def __init__(self, hour=0, minute=0, second=0):
"""初始化方法
:param hour: 时
:param minute: 分
:param second: 秒
"""
self._hour = hour
self._minute = minute
self._second = second
def run(self):
"""走字"""
self._second += 1
if self._second == 60:
self._second = 0
self._minute += 1
if self._minute == 60:
self._minute = 0
self._hour += 1
if self._hour == 24:
self._hour = 0
def show(self):
"""显示时间"""
return ('%02d:%02d:%02d' % (self._hour, self._minute, self._second))
def main():
clock = Clock(12, 00, 00)
while True:
print(clock.show())
sleep(1)
clock.run()
if __name__ == '__main__':
main()