一. 类和对象命名空间
类里 可以定义两种属性:
1. 静态属性
2. 动态属性
class Course:
language = 'Chinese'
def __init__(self, teacher, course_name, period, price):
self.teacher = teacher # 授课老师
self.name = course_name # 课程名字
self.period = period # 课程周期
self.price = price # 课程价格
def func(self):
pass
python = Course('egon', 'python', '6 months', 20000)
print(Course.language)
Course.language = 'English' # 这种方式可以修改静态属性,但通过Course.__dict__['language'] = 'English' 修改不了
print(Course.language)
linux = Course('oldboy', 'linux', '6 months', 18000)
print(python.language)
python.language = 'INS'
print(Course.language)
print(python.language)
print(linux.language)
print(python.__dict__)
print(linux.__dict__)
1 # linux 比python少了一个language,说明python创建了一个language = 'INS',
2 # 但Course里面的language并不会变
3 # 但这样的话就永远用不到Course里面的language了
4 # 把python里面的删除就可以了:
5 del python.language
6 print(python.language)
对于 不可变数据类型 来说,类变量最好用类操作,而不适合用对象操作
对于 可变数据类型 来说,对象名的修改是共享的,重新赋值是独立的
类中的静态变量,可以被对象和类调用
上面说的是对于不可变类型,那么对于可变数据类型呢?
class Course:
language = ['Chinese']
def __init__(self, teacher, course_name, period, price):
self.teacher = teacher # 授课老师
self.name = course_name # 课程名字
self.period = period # 课程周期
self.price = price # 课程价格
def func(self):
pass
python = Course('egon', 'python', '6 months', 20000)
Course.language[0] = 'English'
print(Course.language)
linux = Course('oldboy', 'linux', '6 months', 18000)
# python.language[0] = 'INS'
# print(Course.language)
# print(python.language)
# print(linux.language) # 三个都变成了'INS', 列表里面的值改变了,但不改变列表所在的内存地址
# 但是,如果这样写
python.language = ['Ins']
print(Course.language)
print(python.language) # 依然只有python.language变了, 相当于 重新赋值
print(linux.language)
# 创建一个类,每实例化一个对象就记录下来
# 最终所有的对象共享这个数据
class Foo:
count = 0
def __init__(self):
Foo.count += 1
f1 = Foo()
f2 = Foo()
f3 = Foo()
print(f1.count)
print(f2.count)
print(f3.count)
print(Foo.count) # 通过类名就可以知道实例化了多少个对象
# 函数名不要和静态属性重名
# 认识绑定方法
def func():pass
print(func)
class Foo:
def func(self):
print('func')
f1 = Foo()
print(Foo.func)
print(f1.func)
print(f1)
二. 组合
1 # 面向对象的三大特性:继承 多态 封装 -- 之后学
2 # 组合
3 # 人狗大战
4
5 class Dogs:
6 def __init__(self,name ,HP, aggr, kind):
7 self.name = name
8 self.HP = HP
9 self.aggr = aggr
10 self.kind = kind
11
12 def bite(self,person):
13 person.HP -= self.aggr
14
15 class Person:
16 def __init__(self,name ,HP, aggr, sex):
17 self.name = name
18 self.HP = HP
19 self.aggr = aggr
20 self.sex = sex
21 self.money = 0
22
23 def attack(self,dog):
24 dog.HP -= self.aggr
25
26 def get_weapon(self,weapon):
27 if self.money >= weapon.price:
28 self.money -= weapon.price
29 self.weapon = weapon
30 self.aggr += weapon.aggr
31 else:
32 print('余额不足, 请充值')
33 class Weapon: # 定义一个装备类
34 def __init__(self, name, aggr, naijiu, price):
35 self.name = name
36 self.aggr = aggr
37 self.naijiu = naijiu
38 self.price = price
39
40 def hand_18(self, peason):
41 if self.naijiu > 0:
42 peason.HP -= self.aggr * 2
43 self.naijiu -= 1
44 alex = Person('alex', 100, 1, '不详')
45 jin = Person('金老板', 200, 2, '泰迪')
46 weapon = Weapon('打狗棒', 5, 100, 9.9)
47
48 # 装备
49 # 增加伤害
50 # 增加血量
51 # 给alex装备打狗棒
52 # 打不过金老板,于是 充钱买装备
53 alex.money += 1000
54 alex.get_weapon(weapon)
55 print(alex.weapon)
56 print(alex.aggr)
57 alex.attack(jin)
58 print(jin.HP)
59
60 alex.weapon.hand_18(jin)
61 print(jin.HP)
62
63
64 # 组合:一个对象的属性值是另外一个类的对象
65 # alex.weapon 是 Weapon 类的一个对象
三. 组合的一些小练习
1 创建一个圆环类
2
3 from math import pi
4 class Circle:
5 def __init__(self, r):
6 self.r = r
7 def area(self):
8 return self.r ** 2 * pi
9 def perimeter(self):
10 return 2 * pi * self.r
11
12 class Ring:
13 def __init__(self,outside_r, inside_r):
14 self.outside_c = Circle(outside_r)
15 self.inside_c = Circle(inside_r)
16 def area(self):
17 return self.outside_c.area() - self.inside_c.area()
18 def perimeter(self):
19 return self.outside_c.perimeter() + self.inside_c.perimeter()
20
21 ring = Ring(20, 10)
22 print(ring.area())
23 print(ring.perimeter())
1 # 创建一个老师类
2 # 老师有生日
3 # 生日也是一个类:年、月、日 三个属性
4 # 组合实现
5
6 class Teacher:
7 def __init__(self,name, age, sex, birthday):
8 self.name = name
9 self.age = age
10 self.sex = sex
11 self.bir = birthday
12 self.course = Course('python','6 month',20000)
13
14 class Course:
15 def __init__(self, course_name, period, price):
16 self.name = course_name # 课程名字
17 self.period = period # 课程周期
18 self.price = price # 课程价格
19
20
21
22 class Birthday:
23 def __init__(self, year, month, day):
24 self.year = year
25 self.month = month
26 self.day = day
27 birthday = Birthday(2017, 3, 20)
28 teacher = Teacher('KID', 21, '女', birthday)
29 print(teacher.name)
30 print(teacher.bir.year)
31 print(teacher.bir.month)
32 print(teacher.course.price)