学习视频地址:https://www.bilibili.com/video/BV15J411T7WQ?p=155
P155 私有化
封装:1.私有化属性 2.定义公有set和get方法
#__属性 : 就是将属性私有化,访问范围仅仅限于类中
好处:
1.隐藏属性不被外界随意修改
2.也可以修改:通过函数def setXXX(self,xxx):3.筛选赋值的内容if xxx是否符合条件赋值else :不赋值
3. 如果想获取具体的某一个属性使用get函数 def getXXX(self) :return self._xxx
例子:
class Student:def __init__(self, name, age):self.name = nameself.__age = agedef setAge(self, age):if age > 0 and age < 100:self.__age = ageelse:print('不在规定范围内')def getAge(self):return self.__agedef __str__(self):return '姓名:{},年龄:{}'.format(self.name, self.__age)s = Student('lisi',21)
s.name = 'nannan'
s.setAge(22)
print(s.getAge())print(s)
结果:
E:\python\python.exe C:/Users/Administrator/Desktop/Demo/代码/18day/71901.py
22
姓名:nannan,年龄:22
私有化的装饰器 @property
例:
class Student:def __init__(self, name, age):self.name = nameself.__age = age@propertydef age(self): return self.__age@age.setterdef age(self, age):if age > 0 and age < 100:self.__age = ageelse:print('不在规定范围内')def __str__(self):return '姓名:{},年龄:{}'.format(self.name, self.__age)s = Student('lisi', 21)
s.name = 'nannan'
s.age = 22 # 用完装饰器能跟之前一样直接修改
print(s)
结果:
姓名:nannan,年龄:22
P157 继承 继承中 has a ,is a ,super
知识点:
1. has a一个类中使用另外一种自定义的类型student使用comput book
2.类型:系统类型:str int floatlist dict tuple set自定义类型:算是自定义的类,都可以将其当成一种类型s =Student()s是Student类型的对象
例:
class Computer:def __init__(self, brand, type, color):self.brand = brandself.type = typeself.color = colordef online(self):print('正在使用电脑上网。。。')def __str__(self):return self.brand + '-----' + self.type + '-----' + self.colorclass Book:def __init__(self, bname, author, number):self.bname = bnameself.author = authorself.number = numberdef __str__(self):return self.bname + '----' + self.bname + '---------' + self.numberclass Student:def __init__(self, name, computer, book):self.name = nameself.computer = computerself.books = []self.books.append(book)def borrow_book(self, book):for book1 in self.books:if book1.bname == book.bname:print('以借过此书')breakelse:self.books.append(book)print('添加成功')def show_book(self):for book in self.books:print(book.bname)def __str__(self):return self.name + '----' + str(self.computer) + '------' + str(self.books)computer = Computer('mac', 'mac pro 2018', '深灰色')book = Book('盗墓笔记', '南派三叔', 10)
stu = Student('nannan', computer, book)
book1 = Book('鬼吹灯', 'ya', 10)stu.borrow_book(book)
stu.borrow_book(book1)
stu.show_book()
print(stu)
结果:
E:\python\python.exe C:/Users/Administrator/Desktop/Demo/代码/18day/71903.py
以借过此书
添加成功
盗墓笔记
鬼吹灯
nannan----mac-----mac pro 2018-----深灰色------[<__main__.Book object at 0x000001CDC81A7C88>, <__main__.Book object at 0x000001CDC81A7CF8>]
is a base class 父类 基类
#Exeption
继承:
Student , Employee , Doctor ------> 都属于人类
相同代码 ------》 代码冗余,可读性不高
将相同代码提取 -----》 Person类
Student , Employee , Doctor ------> 继承Person
class Studen(Person)
pass
特点:
1.如果类中不定义 __init__
,调用父类 super class 的 __init__
2.如果类继承父类也需要定义自己的 __init__
, 就需要在当前类的 init 调用一下
父类的 __init__
3.如何调用父类 __init__
:
super() . __init__
(参数)
super(类名,对象) . __init__
(参数)
4. 如果父类有eat() , 子类也定义一个eat方法 ,默认搜索的原则;先找当前类,再去找父类
s.eat()
override: 重写(覆盖)
父类提供的方法不能满足子类的需求,就需要在子类中定义一个同名的方法,这种行为:重写
5. 子类的方法中也可以调用父亲方法:
super().方法名(参数)
例:
class Person:def __init__(self, name, age):self.name = nameself.age = 18def eat(self):print(self.name + '正在吃饭。。。')def run(self):print(self.name + '正在跑步。。。')class Student(Person):def __init__(self, name, age, clazz):super().__init__(name, age)self.clazz = clazzdef study(self, course):print('{}正在学习{}课程'.format(self.name, course))def eat(self, food):print(self.name + '正在吃饭。。。喜欢吃:' + food)class Employee(Person):def __init__(self, name, age, salary, manager):super().__init__(name, age)self.salary = salaryself.manager = managerclass Doctor(Person):def __init__(self, name, age, patients):super(Doctor, self).__init__(name, age)self.patients = patientss = Student('nannan', 18, 'tongxin2')
s.eat('糖醋里脊')
s.study('斑马英语')
s.run()
e = Employee('lily', 22, 5000, 'sam')
e.salary =6000
print(e.salary)list1 = ['shangcan', 'lisi', 'wangwu', 'zhaoliu']
d = Doctor('sam', 40, list1)
print(d.patients)结果:
E:\python\python.exe C:/Users/Administrator/Desktop/Demo/代码/18day/71904.py
nannan正在吃饭。。。喜欢吃:糖醋里脊
nannan正在学习斑马英语课程
nannan正在跑步。。。
6000
['shangcan', 'lisi', 'wangwu', 'zhaoliu']
应用:
1.添加所有类型的人员 2计算月薪 3显示示所有人工资情况
class Person:def __init__(self, job_num, name, salary):self.job_num = job_numself.name = nameself.salary = salarydef __str__(self):msg = '工号' + str(self.job_num) + '姓名' + str(self.name) + '工资:' + str(self.salary)return msgclass Worker(Person):def __init__(self, job_num, name, salary, day, hourlpay):super().__init__(job_num, name, salary)self.day = dayself.hourlpay = hourlpaydef Getsalary(self):money = self.day * self.hourlpayself.salary += moneyreturn self.salaryclass Salesman(Person):def __init__(self, job_num, name, salary, sales, proportion):super().__init__(job_num, name, salary)self.sales = salesself.proportion = proportiondef Getsalary(self):money = self.sales * self.proportionself.salary += moneyreturn self.salarya = Worker('123456', 'nannan', 2000, 10, 30)
print(a.Getsalary())
b = Person('123', 'AAA', '3000')
print(b)
si = Salesman('7578','sisi',3000,80000,0.0003)
print(si.Getsalary())
结果:
E:\python\python.exe C:/Users/Administrator/Desktop/Demo/代码/18day/71905.py
2300
工号123姓名AAA工资:3000
3024.0
多继承的搜索顺序:广度优先
例:
# 多继承的搜索顺序:按层搜素
class P1:def foo(self):print('p1------>foo')def bar(self):print('p1-------->bar')class P2:def foo(self):print('p2------->foo')class C1(P1, P2):passclass C2(P1, P2):def bar(self):print('c2-------->bar')class D(C1, C2):passd = D()
print(D.__mro__)d.foo()
d.bar()
结果:
E:\python\python.exe C:/Users/Administrator/Desktop/Demo/代码/18day/71906.py
(<class '__main__.D'>, <class '__main__.C1'>, <class '__main__.C2'>, <class '__main__.P1'>, <class '__main__.P2'>, <class 'object'>)
p1------>foo
p1-------->bar
广度优先,按层去搜索,先搜C1->C2->P1->92
P164 多态;
python中没有明确的多态,如果要判断是不是父类的子类 用isinstance(obj,类) 进行判断
面对对象: 封装 继承 多态
P166 单例
作用是:节约内存,使对象的地址保持一致。