今日主要内容:
类的基本知识复习
类和对象的命名空间
组合的用法
组合练习
一,复习
1,基本知识
# 定义类
# class
# 函数 : 方法。 也叫:动态属性 # 类中可以定义方法,方法都有一个必须传的参数self
# 变量 : 类属性。 也叫:静态属性 # 类中可以定义静态属性
# __init__方法:初始化方法
# python帮我们创建了一个对象self
# 每当我们调用类的时候就会自动触发这个方法。默认传self
# 在init方法里面可以对self进行赋值
# self是什么 self拥有属性都属于对象
# 在类的内部,self就是一个对象
# alex = Person()
# alex.walk == Person.walk(alex)
# 实例化
# 对象 = 类(其中参数是init方法的)
# 实例、对象 完全没有区别
# 对象查看属性
# 对象.属性名
# 对象调用方法
# 对象.方法名(参数) #类名.方法名(对象名,参数)
2,小实战
# 正方形 周长和面积
class Square:
def __init__(self, side_len):
self.side_len = side_len
def perimeter(self):
return self.side_len * 4
def area(self):
return self.side_len ** 2
s = Square(5)
print(s.perimeter())
print(s.area())
输出结果:
20
25
二,类和对象命名空间
1,内容一
# 类里 可以定义两种属性
# 静态属性
# 动态属性
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
'''
类(Course) 对象(python)
language teacher
__init__ name
func period
price
类和对象的空间的内容分布!
他们之间是单线联系!
对线能找到类!但是类找不到对象!
'''
#Course.__dict__['language'] = 'Chinese' 这种方法不行,__dict__方法不能修改
Course.language = 'English'
print(Course.language)
python = Course('egon', 'python', '6 months', 20000)
print(python.language) # 其实在对象的空间里是没有静态属性的,系统找不到会去类的空间中找
linux = Course('oldboy', 'linux', '6 months', 20000)
print('00000000000000000000000000000000000000000000000000000')
python.language = 'Chinese' #在自己的字典中找不到的时候!这句话会在自己的空间创建一个language的属性
print(python.language)
print(python.__dict__)
print(Course.language)
print(linux.language)
print(linux.__dict__)
print('11111111111111111111111111111111111111111111111')
del python.language
print(python.language)
print(python.__dict__)
# 类中的静态变量 可以被对象和类调用
# 对于不可变数据类型来说, 类变量最好用类名操作
# 对于可变数据类型来说, 对象名的修改是共享的,重新赋值是独立的
输出结果:
English
English
00000000000000000000000000000000000000000000000000000
Chinese
{‘teacher’: ‘egon’, ‘name’: ‘python’, ‘period’: ‘6 months’, ‘price’: 20000, ‘language’: ‘Chinese’}
English
English
{‘teacher’: ‘oldboy’, ‘name’: ‘linux’, ‘period’: ‘6 months’, ‘price’: 20000}
11111111111111111111111111111111111111111111111
English
{‘teacher’: ‘egon’, ‘name’: ‘python’, ‘period’: ‘6 months’, ‘price’: 20000}
2,内容二
# 模拟人生
class Person:
money = 0
def work(self):
Person.money += 1000
mother = Person()
father = Person()
mother.work()
father.work()
print(Person.money)
输出结果:
2000
3,内容三
# 创建一个类,每实例化一个对象就计数
# 最终所有的对象共享这个数据 (借助静态变量的特性)
class Foo:
count = 0
def __init__(self):
Foo.count += 1
f1 = Foo()
f2 = Foo()
print(f1.count)
print(f2.count)
f3 = Foo()
print(f1.count)
输出结果:
2
2
3
三,组合的用法
# 面向对象的三大特性 : 继承 多态 封装
# 今天先讲:组合
# 人狗大战
class Dog:
def __init__(self, name, aggr, hp, kind):
self.name = name
self.aggr = aggr
self.hp = hp
self.kind = kind
def bite(self, person):
person.hp -= self.aggr
class Person:
def __init__(self, name, aggr, hp, sex):
self.name = name
self.aggr = aggr
self.hp = hp
self.sex = sex
self.money = 0
def attack(self, dog):
dog.hp -= self.aggr
def get_weapon(self, weapon):
if self.money >= weapon.price:
self.money -= weapon.price
self.weapon = weapon #装备武器
self.aggr += weapon.aggr #装备武器之后,攻击力增加
else:
print("余额不足,请先充值")
class Weapon:
def __init__(self, name, aggr, njd, price):
self.name = name
self.aggr = aggr
self.njd = njd
self.price = price
def hand18(self, person):
if self.njd > 0:
person.hp -= self.aggr * 2
self.njd -= 1
alex = Person('alex', 0.5, 100, '男')
jin = Dog('金老板', 100, 500, 'teddy')
w = Weapon('打狗棒', 100, 3, 998)
# alex装备打狗棒
alex.money += 1000
alex.get_weapon(w)
print(alex.weapon) #在get_weapon中,装备到了self大字典中啦!
print(alex.aggr)
alex.attack(jin)
print(jin.hp)
alex.weapon.hand18(jin)
print(jin.hp)
# 组合 :一个对象的属性值是另外一个类的对象
# Eg:alex.weapon 是 Weapon类的对象(w)
输出结果:
<main.Weapon object at 0x0000025BE6B381D0>
100.5
399.5
199.5
四,组合练习
1,case1
# case1:
#
# 圆形类
# 圆环类
'''
组合:用另一个类的对象作为该对象的一个属性值!
这样,在该对象中,就可以调用另一个对象的方法。
'''
from math import pi
class Circle:
def __init__(self, r):
self.r = r
def area(self):
return self.r**2 * pi
def perimeter(self):
return 2*pi*self.r
class Ring:
def __init__(self, outside_r, inside_r):
self.outside_c = Circle(outside_r) # 用另一个类的对象作为属性值
self.inside_c = Circle(inside_r)
def area(self):
return self.outside_c.area() - self.inside_c.area()
def perimeter(self):
return self.outside_c.perimeter()+self.inside_c.perimeter()
ring = Ring(20,10)
print(ring.area())
print(ring.perimeter())
输出结果:
942.4777960769379
188.49555921538757
2,case2
# case2:
# 创建一个老师类
# 老师有生日
# 生日也可以是一个类
# 组合
class Birthday:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
class Course:
def __init__(self, course_name, period, price):
self.name = course_name
self.period = period
self.price = price
class Teacher:
def __init__(self, name, age, sex, birthday):
self.name = name
self.age = age
self.sex = sex
self.birthday = birthday
self.course = Course('python', '6 month', 2000)
b = Birthday(2018, 12, 20)
egg = Teacher('egon', 0, '女', b)
print(egg.name)
print(egg.birthday.year)
print(egg.birthday.month)
print(egg.course.name)
输出结果:
egon
2018
12
python