案例21
测试参数化输入输出自定义结构体形式-数组嵌套
效果如图
源码如下:
a = [
["高小一",18,30000,"北京"],
["高小二",19,20000,"上海"],
["高小一",20,10000,"深圳"],
]
for m in range(3):
for n in range(4):
print(a[m][n],end="\t")
print() #打印完一行,换行
案例22
测试参数化输入输出自定义结构体形式-字典形式组合
效果如图
源码如下:
r1 = {"name":"高小一","age":18,"salary":30000,"city":"北京"}
r2 = {"name":"高小二","age":19,"salary":20000,"city":"上海"}
r3 = {"name":"高小五","age":20,"salary":10000,"city":"深圳"}
tb = [r1,r2,r3]
#获得第二行的人的薪资
print(tb[1].get("salary"))
#打印表中所有的的薪资
for i in range(len(tb)): # i -->0,1,2
print(tb[i].get("salary"))
#打印表的所有数据
for i in range(len(tb)):
print(tb[i].get("name"),tb[i].get("age"),tb[i].get("salary"),tb[i].get("city"))
案例23
TURTLE绘制奥运五环图turtle函数
效果如图
源码如下:
import turtle
turtle.width(10)
turtle.color("blue")
turtle.circle(50)
turtle.color("black")
turtle.penup()
turtle.goto(120,0)
turtle.pendown()
turtle.circle(50)
turtle.color("red")
turtle.penup()
turtle.goto(240,0)
turtle.pendown()
turtle.circle(50)
turtle.color("yellow")
turtle.penup()
turtle.goto(60,-50)
turtle.pendown()
turtle.circle(50)
turtle.color("green")
turtle.penup()
turtle.goto(180,-50)
turtle.pendown()
turtle.circle(50)
案例24
定义参数的类:class函数
效果如图
源码如下:
class Student: #类名一般首字母大写,多个单词采用驼峰原则
def __init__(self,name,score): #self必须位于第一个参数
self.name = name
self.score = score
def say_score(self): #self必须位于第一个参数
print("{0}的分数是:{1}".format(self.name,self.score))
s1 = Student("高淇",18) #通过类名()调用构造函数
s1.say_score()
s1.age = 32
s1.salary = 3000
#del s1
print(s1.salary)
s2 = Student("高希希",100)
s2.say_score()
Student.say_score(s2)
print(dir(s2))
print(s2.__dict__)
class Man:
pass
print(isinstance(s2,Man))
精简版源码如下:
class Student: #类名一般首字母大写,多个单词采用驼峰原则
def __init__(self,name,score): #self必须位于第一个参数
self.name = name
self.score = score
def say_score(self): #self必须位于第一个参数
print("{0}的分数是:{1}".format(self.name,self.score))
stu2 = Student
s1 = Student("高淇",60)
s2 = stu2("高希希",100)
s1.say_score()
s2.say_score()
效果如图
案例25
测试 class定义结构体类形式命名参数
效果如图
源码如下:
class Student:
company = "SXT" # 类属性
count = 0 # 类属性
def __init__(self, name, score):
self.name = name # 实例属性
self.score = score
Student.count = Student.count + 1
def say_score(self): # 实例方法
print("我的公司是:", Student.company)
print(self.name, '的分数是:', self.score)
s1 = Student('张三', 80) # s1是实例对象,自动调用__init__()方法
s1.say_score()
s2 = Student("高淇",60)
s3 = Student("高小希",100)
print('一共创建{0}个Student对象'.format(Student.count))
案例26
析构方法
效果如图
源码如下:
#析构方法
class Person:
def __del__(self):
print("销毁对象{0}".format(self))
p1 = Person()
p2 = Person()
del p2
print("程序结束")
案例27
测试类方法、静态方法
效果如图
源码如下:
#测试类方法、静态方法
class Student:
company = "SXT" # 类属性
def __init__(self,name,age):
self.name = name
self,age = age
@classmethod
def printCompany(cls):
print(cls.company)
# print(self.name) #类方法和静态方法中,不能调用实例变量、实例方法
Student.printCompany()
class Student2:
company = "SXT" # 类属性
@staticmethod
def add(a, b): # 静态方法
print("{0}+{1}={2}".format(a,b,(a+b)))
return a+b
Student2.add(20,30)
案例28
测试测试可调用方法__call__()
效果如图
源码如下:
#测试可调用方法__call__()
class SalaryAccount:
'''工资计算类'''
def __call__(self, salary):
print("算工资啦...")
yearSalary = salary*12
daySalary = salary//22.5 #国家规定的每个月的平均工作天数
hourSalary = daySalary//8
return dict(yearSalary=yearSalary,monthSalary=salary,daySalary=daySalary,hourSalary=hourSalary)
s = SalaryAccount()
print(s(30000))
案例29
测试方法的动态性
效果如图
源码如下:
#测试方法的动态性
class Person:
def work(self):
print("努力上班!")
def play_game(s):
print("{0}在玩游戏".format(s))
def work2(s):
print("好好工作,努力上班!赚大钱,娶媳妇!")
Person.play = play_game
p = Person()
p.work()
p.play() #Person.play(p)
Person.work = work2
p.work()
案例30
测试私有属性、私有方法
效果如图
源码如下:
#测试私有属性、私有方法
class Employee:
__company = "百战程序员"
def __init__(self,name,age):
self.name = name
self.__age = age #私有属性
def __work(self): #私有方法
print("好好工作,赚钱娶媳妇!")
print("年龄:{0}".format(self.__age))
print(Employee.__company)
e = Employee("高淇",18)
print(e.name)
#print(e.__age)
print(e._Employee__age)
print(dir(e))
e._Employee__work()
print(Employee._Employee__company)