面向对象 面向对象作业——校园管理系统

一、面向对象初步

1、面向对象的人狗大战例子

 1 def Person(name,blood,aggr,sex):
 2     person={
 3         'name':name,
 4         'blood':blood,
 5         'aggr':aggr,
 6         'sex':sex,
 7     }
 8     def attack(dog):
 9         dog['blood'] -= person['aggr']
10         print('%s被打了,掉了%s的血'%(dog[name],person[aggr]))
11     person['attack'] = attack
12 
13     return person
14 
15 def Dog(name,blood,aggr,kind):
16     dog={
17         'name':name,
18         'blood':blood,
19         'aggr':aggr,
20         'kind':kind,
21     }
22     def bite(person):
23         person['blood'] -= dog['aggr']
24         print('%s被咬了,掉了%s血'%(person['name'],dog['aggr']))
25     dog['bite'] = bite
26     return dog
27 
28 jin = Dog('旺财',500,10,'tddy')
29 alex = Person('小市民',100,5,'man')
30 print(jin)
31 jin['bite'](alex)
1 {'name': '旺财', 'blood': 500, 'aggr': 10, 'kind': 'tddy', 'bite': <function Dog.<locals>.bite at 0x0000000001E8D7B8>}
2 小市民被咬了,掉了10血
结果

 二、面向对象

1、面向对象词语解释

对象 = 类名()

过程:

  类名()首先会创建一个对象,创建了一个self变量

  调用init方法,类名括号里的参数会被这里接收

  执行init方法

  返回self

对象能做的事情:

  查看属性

  调用方法

  __dict__对于对象的增删改查操作都可以通过字典的语法进行

类名能做的事情:

  实例化

  调用方法:只不过要自己传递self参数

  调用类中的属性,也就是调用静态属性

  __dict__对于类中的名字只能看,不能操作

类里面的元素定义:

      函数:方法,动态属性,类中可以定义方法,方法都有一个必须传的参数self

     变量:类属性,静态属性,类中可以定义静态属性

__init__方法,初始化方法

     python帮我们创建了一个对象self

    每当我们调用类的时候就会自动触发这个方法,默认传self

    在init方法里面可以对self进行赋值

self是什么

     self拥有的属性都属性对象

    在类的内部,self就是一个对象

    

 1 class Person:
 2     country = 'china'
 3     def __init__(self,*args):
 4         self.name = args[0]
 5         self.blood = args[1]
 6         self.aggr = args[2]
 7         self.sex = args[3]
 8     def walk(self,n):
 9         print('%s今天走了%s多步'%(self.name,n))
10 peng = Person('Pengjun',500,100,'man') #实例化
11 print(Person.__dict__) #查看所有属性
12 print(peng.name) #参看属性值
13 print(peng.sex) #查看属性值
14 print(peng.__dict__)
15 peng.walk(100) #调用方法
View Code
1 {'__module__': '__main__', 'country': 'china', '__init__': <function Person.__init__ at 0x00000000021CD730>, 'walk': <function Person.walk at 0x00000000021CD7B8>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
2 Pengjun
3 man
4 {'name': 'Pengjun', 'blood': 500, 'aggr': 100, 'sex': 'man'}
5 Pengjun今天走了100多步
结果

 2、例子

求圆的面积及周长

 1 from math import pi
 2 
 3 class Circle:
 4     def __init__(self,r):
 5         self.r = r
 6     def area(self):
 7         return pi * (self.r**2)
 8     def perimeter(self):
 9         return 2 * pi * self.r
10 
11 C1 = Circle(8)
12 print(C1.area())
13 print(C1.perimeter())
View Code
1 201.06192982974676
2 50.26548245743669
结果

 3、对象的静态变量赋值

对象的静态变量进行赋值,相当于在对象的命名空间里增加了静态变量,则该对象先在自己的空间找到变量,就不会到类里面去找,
这样,类里面的静态变量没有改变。
 1 class Course:
 2     langure = 'chinese'
 3     def __init__(self,name,teacher,period,price):
 4         self.name = name
 5         self.teacher = teacher
 6         self.period = period
 7         self.price = price
 8     def func(self):
 9         pass
10 
11 Course.langure = 'English'
12 # Course.__dict__ ['language'] = 'Chinese' #类的静态属性不能通过init修改
13 python = Course('python','peng','12 mouths',38000)
14 linux = Course('linux','peng','6 mouths',28000)
15 #类中的静态变量,可以被对象和类调用
16 python.langure = '日语'#
17 print(python.langure)
18 print(linux.langure)
19 print(Course.langure)
静态变量赋值的变化
1 日语
2 English
3 English
结果

 4、组合

软件重用的重要方式除了继承之外还有另外一种方式,即:组合

组合指的是,在一个类中以另外一个类的对象作为数据属性,称为类的组合

当类之间有显著不同,并且较小的类是较大的类所需要的组件时,用组合比较好

例1:

 1 class Person :
 2     def __init__(self,name,hp,aggr,sex,money):
 3         self.name = name
 4         self.hp = hp
 5         self.aggr = aggr
 6         self.sex = sex
 7         self.money = 0
 8     def atter(self,dog):
 9         dog.hp -= self.aggr
10     def get_weapon(self,weapon):
11         if self.money >= weapon.price:
12             self.money -= weapon.price
13             self.weapon = weapon
14             self.aggr += weapon.aggr
15         else:
16             print('余额不足,请充值')
17 
18 class Dog:
19     def __init__(self,name,hp,aggr,kind):
20         self.name = name
21         self.hp = hp
22         self.aggr = aggr
23         self.kind = kind
24     def atter(self,person):
25         person.hp -= self.aggr
26 
27 class Weapon:
28     def __init__(self,name,aggr,times,price):
29         self.name = name
30         self.times = times
31         self.aggr = aggr
32         self.price = price
33     def wugong(self,person):
34         if self.times > 0:
35             person.hp -= self.aggr
36             self.times -= 1
37 
38 
39 
40 hand = Weapon('MMA',50,3,200)
41 peng = Person('',1000,100,'man',1000)
42 telangpu = Dog('特朗普',500,50,'taidi')
43 peng.money += 1200
44 
45 print(telangpu.hp)
46 peng.atter(telangpu)
47 print(telangpu.hp)
48 
49 peng.get_weapon(hand)
50 peng.weapon.wugong(telangpu)
51 print(telangpu.hp)
View Code
1 500
2 400
3 350
结果

 例2:用类的组合方法计算环形的面积及周长

 1 from  math import pi
 2 
 3 class Circle:
 4     def __init__(self,r):
 5         self.r = r
 6     def Area(self):
 7         return pi * self.r**2
 8     def Perimeter(self):
 9         return 2*pi*self.r
10 
11 class Ring:
12     def __init__(self,outside_r,inside_r):
13         self.outside_r = Circle(outside_r) #类Ring的属性是类Circle的对象----组合
14         self.inside_r = Circle(inside_r)   #类Ring的属性是类Circle的对象----组合
15     def Huan_Area(self):
16         return Circle.Area(self.outside_r) - Circle.Area(self.inside_r)
17     def Huan_Perimeter(self):
18         return  Circle.Perimeter(self.outside_r) + Circle.Perimeter(self.inside_r)
19 
20 A = Ring(5,4)
21 print(A.Huan_Area())
22 print(A.Huan_Perimeter())
View Code
28.27433388230814
56.548667764616276

 例3:

 1 class Birthday:
 2     def __init__(self,year,month,day):
 3         self.year = year
 4         self.month = month
 5         self.day = day
 6 
 7 class Teacher:
 8     def __init__(self,name,age,sex,birthday):
 9         self.name = name
10         self.age =age
11         self.sex = sex
12         self.birthday = birthday
13 B1 = Birthday('2020','05','22')
14 peng = Teacher('fei',25,'female',B1)
15 print(peng.name)
16 print(peng.birthday.year)
17 print(peng.birthday.day)
View Code
fei
2020
22

  

创建一个老师类,老师有生日,生日也可以是一个类,用组合完成

5、面向对象作业——校园管理系统

角色:

学校、学员、课程、讲师

要求:

1. 创建北京、上海 2 所学校

2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开

3. 课程包含,周期,价格

4. 班级关联课程、讲师

5. 创建学员时,选择学校,关联班级

5. 创建讲师角色时要关联学校

6. 提供三个角色视图

  6.1 学员视图, 登陆, 查看课程、查看班级

  6.2 讲师视图, 讲师可查看自己教学的班级、课程。

          进阶需求:可管理自己的班级, 查看班级学员列表 , 修改所管理的学员的成绩

  6.3 管理视图,创建讲师, 创建班级,创建课程

7. 上面的操作产生的数据都通过pickle序列化保存到文件里

四、面向对象的继承

一个类可以被多个类继承;一个类可以继承多个父类
没有继承父类默认继承object类,也叫新式类
1 class A:pass #父类,超类,基类
2 class B:pass
3 
4 class A_son(A):pass #子类,派生类
5 class AB_son(A,B):pass #
6 
7 print(A.__bases__)#
8 print(A_son.__bases__)
9 print(AB_son.__bases__)
View Code
1 (<class 'object'>,)
2 (<class '__main__.A'>,)
3 (<class '__main__.A'>, <class '__main__.B'>)
结果

 例1:

 1 class Animal:
 2     def __init__(self,name,aggr,hp):
 3         self.name = name
 4         self.aggr = aggr
 5         self.hp = hp
 6 
 7 class Dog(Animal):
 8     def bite(self,person):
 9         person.hp -= self.aggr
10 class Person(Animal):
11     pass
12 
13 jin = Dog('zhang',200,1000)
14 print(jin.name)
1 zhang
View Code

猜你喜欢

转载自www.cnblogs.com/xiaofei1106/p/12892023.html