# 练习 # 定义一个圆环类 # 属性:记录大圆半径,小圆半径 # 要求: # 1.实现计算面积 大圆面积 -小圆面积 # 2.计算周长 大圆周长 + 小圆周长 """圆""" 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 self.r ** 2 * pi # # # c1 = Circle(10) # print(c1.area()) # print(c1.perimeter()) # c1.r = 20 # print(c1.area()) # print(c1.perimeter()) """圆环""" # class Ring: # def __init__(self, outer_r, inner_r): # self.outer_r = outer_r # self.inner_r = inner_r # # def area(self): # 面积 # o_area = self.outer_r ** 2 * pi # i_area = self.inner_r ** 2 * pi # return o_area - i_area # # def perimeter(self): # 周长 # o_perimeter = 2 * self.outer_r * pi # i_perimeter = 2 * self.inner_r * pi # # return o_perimeter + i_perimeter # # # r1 = Ring(10, 5) # print(r1.area()) # print(r1.perimeter())
组合
# 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 self.r ** 2 * pi # # # class Ring: # '''圆环类''' # def __init__(self, outer_r, inner_r): # c1 = Circle(outer_r) # c2 = Circle(inner_r) # # self.outer_r = c1 # print(self.outer_r) # self.inner_r = c2 # # # def area(self): # 面积 # return self.outer_r.area() -self.inner_r.area() # def perimeter(self): # 周长 # return self.outer_r.perimeter() + self.inner_r.perimeter() # # # r1 = Ring(10, 5) # print('r1----',r1) # print(r1.area()) # print(r1.perimeter()) # 练习 # 学生:姓名:性别,年龄,所学习的课程 # 学生类, 课程类 class Student: def __init__(self, name, sex, age, course): self.name = name self.sex = sex self.age = age self.course = course class Course: def __init__(self, name, period, price): self.name = name self.period = period self.price = price python = Course('python', '3个月', 19800) go = Course('go', '一个月', 15800) php = Course('php', '二个月', 18000) zhangchen = Student('张晨', '男', 30, 'python') print(zhangchen.course) # 你拿不到python周期和价格 zhangchen.course =go print(zhangchen.course.name) print(zhangchen.course.period) print(zhangchen.course.price) # 例子 # class A: # def __init__(self, name): # self.name = name # # # a = A('张 三') # # a是对象,是A的对象 # print(a.name) # 是对象a的属性 # # a.name 是个字符串 '张 三' # '张 三'.split(' ') # # a.name.split(' ') # 这是不是组合
# class Animal: # def eat(self): # # 吃药 # self.hp += 10 # print('吃药的方法') # class Person(Animal): # def __init__(self, name, hp, ad, sex, job): # self.name = name # self.hp = hp # self.ad = ad # self.sex = sex # self.job = job # def attack(self, dog): # dog.hp = dog.hp - self.ad # print('%s攻击了%s,%s掉了%s点血' % # (self.name, dog.name, dog.name, self.ad)) # class Dog(Animal): # def __init__(self, name, kind, hp, ad): # self.name = name # self.kind = kind # self.hp = hp # self.ad = ad # def bite(self,person): # """咬的方法""" # person.hp = person.hp - self.ad # print('%s咬了%s,%s掉了%s点血'% # (self.name,person.name,person.name,self.ad)) # zhangsan = Person('张三', 100, 30, '男', '魔法师') # lisi = Person('李四', 100, 20, '女', '战士') # # wangcai = Dog('旺财', '土狗', 1000, 30) # erha = Dog('二哈', '哈士奇', 500, 20) # zhangsan.eat() # # wangcai.bite(lisi) ##################################### # class Animal: # def __init__(self,name,hp,ad): # self.name = name # self.hp = hp # self.ad = ad # def eat(self): # # 吃药 # self.hp += 10 # print(self.hp) # class Person(Animal): # def __init__(self, name, hp, ad, sex, job): # # 当你自己没有init方法时,走父类的init,当自己有init方法走自己init方法 # self.sex = sex # self.job = job # # 如果还想走父类的init 方法,就需要手动去调,类名.init(),主动传self,父类要self,必须传 # Animal.__init__(self,name,hp,ad) # # def attack(self, dog): # dog.hp = dog.hp - self.ad # print('%s攻击了%s,%s掉了%s点血' % # (self.name, dog.name, dog.name, self.ad)) # # # def eat(self): # # pass # # class Dog(Animal): # def __init__(self, name, kind, hp, ad): # self.kind = kind # Animal.__init__(self,name,hp,ad) # def bite(self,person): # """咬的方法""" # person.hp = person.hp - self.ad # print('%s咬了%s,%s掉了%s点血'% # (self.name,person.name,person.name,self.ad)) # zhangsan = Person('张三', 100, 30, '男', '魔法师') # lisi = Person('李四', 100, 20, '女', '战士') # wangcai = Dog('旺财', '土狗', 1000, 30) # erha = Dog('二哈', '哈士奇', 500, 20) # zhangsan.eat() # wangcai.bite(lisi) # print(Animal.__dict__) # 总结: # A和B类都需要调用相同的方法,创建父类C,把相同的方法放到C类中, # A和B继承C,A(C) B(C) 然后A的对象和B的对象就可以直接调用C中的方法了 # A和B中有相同的方法,一部分功能相同,还有不同的部分,创建父类C, # 把相同的部分放到C类的方法中,在A类和B类中保留不同的部分,然后分别在A类和B类调用C类的方法即可 # # 练习,用继承的方式进行优化: # class Course: # def __init__(self,name,period,price): # self.name = name # self.period = period # self.price = price # # # class Student: # def __init__(self,name): # self.name = name # # # def show_course(self): # '''查看课程的方法''' # for item in course_lst: # print(item) # # class Manager: # def __init__(self,name): # self.name = name # # def show_course(self): # '''查看课程的方法''' # for item in course_lst: # print(item) # # python=Course('python全栈开发','6个月',25800) # linux=Course('linux运维','3个月',18000) # # course_lst = [python,linux] # 用继承来实现 class Role: def __init__(self,name): self.name = name print(self.name) def show_course(self): '''查看课程的方法''' for item in course_lst: print(item.name,item.price,item.period) class Course: def __init__(self,name,period,price): self.name = name self.period = period self.price = price class Student(Role): # pass def __init__(self,name): Role.__init__(self,name) class Manager(Role): pass python=Course('python全栈开发','6个月',25800) linux=Course('linux运维','3个月',18000) course_lst = [python,linux] # 优化 # m=Manager('lisir') # m.show_course() s=Student('李有才') print(Linux.show_course()) # 用继承来实现 优化 ---super() # class Role: # def __init__(self,name): # self.name = name # print(self.name) # # def show_course(self): # '''查看课程的方法''' # for item in Course.course_lst: # print(item.name,item.price,item.period) # # class Course: # # def __init__(self,name,period,price): # self.name = name # self.period = period # self.price = price # # class Student(Role): # # pass # def __init__(self,name): # # super 第一个参数是当前所在的类,第二个参数self当前所在的对象 # # super(Student, self).__init__(name) # # super 简写,我不传super内部会替我传过去 # super().__init__(name) # # class Manager(Role): # pass # # # python=Course('python全栈开发','6个月',25800) # linux=Course('linux运维','3个月',18000) # # Course.course_lst = [python,linux] # m=Manager('lisir') # m.show_course() # s=Student('李有才') # s.show_course()
多继承
# class C: # def func(self): # print(' in C') # # # class B: # # def func(self): # print(' in B') # # # class A(B, C): # 从左到右依次去找你调用的方法 # pass # # # a = A() # a.func() # B 和 C 都有func 先用谁的,先用B的,B离得最近,B没有才走C # 再看一个,钻石继承 # class A: # pass # def func(self): # print(' in A') # class B(A): # pass # # def func(self): # print(' in B') # class C(A): # pass # def func(self): # print(' in C') # class D(B,C): # pass # # d = D() # d.func() # 我们多个类继承之间,我们管它叫广度优先 # 横向叫广度,纵向(竖着)叫深度 # class A: # pass # def func(self): # print(' in A') # class B(A): # pass # # def func(self): # # print(' in B') # class C(A): # pass # def func(self): # print(' in C') # class D(B): # pass # # def func(self): # # print(' in D') # class E(C): # pass # def func(self): # print(' in E') # class F(D,E):pass # f=F() # f.func() # # 遵循C3 算法,算法比较复杂 # print(F.mro()) # 查看多继承中得继承顺序 # # super 重新认识一下 # class B: # pass # def func(self): # print('B') # # # class A(B): # def func(self): # super().func() # print('A') # # a=A() # a.func() # DBA # DBCA # BCDA # DCBA class D: def func(self):print('D') class C(D): def func(self): super(C, self).func() print('C') class B(D): def func(self): super().func() print('B') class A(B,C): def func(self): super().func() print('A') a=A() a.func() print(A.mro()) # 单继承 中华 super作用就是找父类 # 在多继承中 super 是找mro 顺序得下一个类
新式类 和经典类 # object 类: # 在python3.x中所有得类都是object得子类 # 对于一些内置得方法都会写在object类中 class A(object): pass a = A() # 但凡实例化都会调用__init__得方法 # 所有继承object类都是新式类 # 在python2中 不继承object得类 都是经典类 # 经典类: 1.在多继承当中遵循得是深度优先 # 经典类中没有super 和mro得方法 # java 不支持多继承 # 正常你用单继承就够了
封装
# 面向对象三大特性: 封装,继承,多态 # 封装分为: # 广义上得封装: # # 规范创建对象 比如人狗大战,人有人得方法,狗有狗得方法 # # 把所有方法装进一个角色里 # 狭义上得封装: 在类得外部干脆不能直接调用了 # class Student: # def __init__(self,name): # # self.__name = name # 两个下划线,把这个name属性私有化 # # def get_name(self): # return self.__name # # laowang = Student('老王') # laowang.name = 'laoli' # print(laowang.name) # print(laowang.__name) # print(laowang.get_name()) # 有什么用? # 为了更好得限制用户,不能随意得查看和随意修改,增加程序安全性 # 私有化静态变量 # class A: # __VAL = [] # 私有化静态变量 # # def __init__(self): # print(A.__VAL) # # A() # # print(A.__VAL) # 私有得方法 class A: def func(self): self.__aaa() # 里面可以调 def __aaa(self): '''相当于把这个方法保护起来''' print('aaaa') def get_name(self): return self.__aaa() a=A() # a.__aaa() # a.aaa() # a.func() # # a.get_name() # 总结: # 在类得内部,对象属性可以变成私有得,类变量可以变成私有得,实例方法可以变成私有得, # 一旦变成私有得就只能在类得内部使用,而不能在类得外部使用了 # 思考: # 1.私有化怎么做到了? # 2.私有得变量能不能被继承? # 1.私有化怎么做到了? # class B: # __abc = 123 # _B__abc 存储时候给我加上_类名xxx # # 偷偷改了名字 # # # print(__abc) # print(B.__dict__) # print(B._B__abc) # 能调,没人这么调 # 2.私有得变量能不能被继承? # 不能被继承 # class A: # def __func(self): # _A__func # print('in A') # # # # class B(A): # def wahaha(self): # self.__func() #_B__func # # b=B() # b.wahaha() # 练习 # class A: # def __init__(self): # self.__func() # 这里变形 # # def __func(self): # 这里变形 # print('in A') # # # class B(A): # def __func(self): # print('in B') # # b=B()
多态
# # 在python 中一切皆对象,处处是多态 # def func(a,b): # print(a) # print(b) # # # func(1,'22') # go 语言 func(int a,str b) # go编译型语言比较严谨 ,在传递参数得时候必须指定参数得类型 # class Student: # class Manager: # pass # # def change_pwd() # # class User:pass # class Student(User):pass # class Manager(User):pass
三个装饰器
import time # property # class Student: # def __init__(self, name, brith): # self.name = name # self.brith = brith # # @property # 伪类装饰器,把方法伪装成属性 # def age(self): # 名词 # return time.localtime().tm_year - self.brith # # # zs=Student('张三',1989) # print(zs.brith) # ret=zs.age # 看起来调得是属性,实际调得是方法 # print(ret) # staticmethod -- 静态方法 # staticmethod 作用: 可以将类函数 不经过实例化而直接被调用,被该装饰器调用得函数不需要传入self,cls参数 # class Cat: # # def __init__(self, name): # self.name = name # # def eat(self): # print(self.name, '喜欢吃鱼') # self.work() # # def work(self): # print('会抓老鼠') # @staticmethod # 加上静态方法装饰器后,不必传任何得默认参数 # def color(): # print('黑猫警长') # # # # c=Cat('狸花猫') # # c.eat() # Cat.color() # classmethod - 类方法 # 功能:可以将类函数 不经过实例化而直接被调用 class Manger: def __init__(self,name): self.name = name print(self.name) @classmethod def login(cls): # cls永远表示我当前类得内存地址 # print('11111111') # # print('cls',cls) # print('Manger',Manger) # m = Manger('张三') m = cls('张三') return m ret=Manger.login() print(ret)
""" 题目一:定义名为MyTime的类,其中有三个实例变量 ,时hour,分minute,秒second 1.为了给对像初始化赋值,编写构造方法,对时分秒附初始值 2.为了保证数据的安全性,这三个成员变量应声明为私有 3.对三个属性分别定义封装get查看方法,set修改方法 """ # class MyTime: # def __init__(self,hour,minute,second): # self.__hour=hour # self.__minute=minute # self.__second=second # # def get_hour(self): # return self.__hour # # def get_minute(self): # return self.__minute # # def get_second(self): # return self.__second # # def set_hour(self,hour1): # self.__hour = hour1 # return self.__hour # # def set_minute(self,minute1): # self.__minute = minute1 # # def set_second(self, second1): # self.__second = second1 # # c1=MyTime(10,20,30) # print(c1.set_hour(16)) # print(c1.get_second()) # c1.set_minute(59) # print(c1.get_minute()) # 题目二 """ 1.利⽤封装和继承的特性完成如下操作: ⼩学⽣: 属性:姓名、学号、年龄、性别 ⾏为:学习 打架 中学⽣: 属性:姓名、学号、年龄、性别 ⾏为:学习,谈恋爱 ⼤学⽣: 属性: 姓名、学号、年龄、性别 ⾏为:学习 打游戏 测试类中: 创建⼩学⽣对象 调⽤学习的⽅法 打印内容为: xx 学习的内容为:语⽂ 数学 英语 创建中学⽣对象 调⽤学习的⽅法 打印内容为:xx 学习的内容为:语数外 ⽣物化 史地政 创建⼤学⽣对象 调⽤学习的⽅法: 打印内容为: 逃课中。。。。。。 """ class Student: def __init__(self,name,no,age,sex): self.name = name self.no = no self.age = age self.sex = sex def study(self,subject): print(f"{self.name}学习的内容为:{subject}") class LittleStudent(Student): def fight(self): print("在打架") class MiddleStudent(Student): def love(self): print("谈恋爱") class BigStudent(Student): def play(self): print("打游戏") def study(self,name,sid,age,sex): super().study(2222) # Student(name,sid,age,sex).study('666') print("逃课中。。。") # zyy = LittleStudent("⼩左",12,19,"女") # zyy.study("语⽂ 数学 英语") # pgm = MiddleStudent("⼩彭",13,18,"女") # pgm.study("语数外 ⽣物化 史地政") yc = BigStudent("余成",14,22,"男") yc.study('张三',11,22,33) #