学习日记23

今日内容

单继承下的属性查找
class Foo:
   def __f1(self): # _Foo__f1()
       print('Foo.f1')

   def f2(self):
       print('Foo.f2')
       self.f1() # self._Foo__f1()

class Bar(Foo): # _Bar__f1()
   def __f1(self):
       print('Bar.f1')

obj = Bar()
obj.f2()
多继承下的属性查找

多继承分为经典类和新式类

菱形继承:

经典类是深度优先

新式类是广度优先(python3中全是新式类)

非菱形继承:

经典类和新式类,属性查找没有顺序要求,都一样

#新式类
class G:
   def test(self):
       print('from G')


class E(G):
   def test(self):
       print('from E')


class F(G):
   def test(self):
       print('from F')


class D(G):
   def test(self):
       print('from D')


class B(E):
   def test(self):
       print('from B')


class C(F):
   def test(self):
       print('from C')


class A(B, C, D):
   def test(self):
       print('from A')


f1 = A()
f1.test()
print(A.__mro__)
A--->B--->E--->C--->F--->D--->G
最后去最深的一个非obj的类里找
super与mro
子类想使用父类的属性:
第一种办法:指名道姓
第二种方法:super()
class people()
schOOl = "SH"
   def __init__(self, name, age):
       self.name = name
       self.age = age

class Student(people):
   
   def __init__(self, name, age, course):
    super().__init__(name, age)
       # People.__init__(self, name, age)
       self.course = course
 

   def choose_course(self,course):
       self.course.append(course)
       print('%s 选课成功 %s' % (self.name, self.courses))
       
class Teacher(people):
   def __init__(self,name, age, level):
       super().__init__(name, age)
  # People.__init__(self, name, age)
       self.level = level
   
   def score(self, stu_obj, num):
       stu_obj.num = num
       print('%s老师给%s打了%s分' % (self, name, stu_obj, num))
       
       
obj = Student()
print(obj.name)
print(obj.age)


# mro方法是用列表的方式显示搜索顺序
class A:
   def test(self):
       print('A---->test')
       super().aaa()
class B:
   def test(self):
       print('B---->test')

   def aaa(self):
       print('B---->aaa')

class C(A,B):
   def aaa(self):
       print('C----->aaa')

c=C()
c.test()
print(C.mro())
#打印结果:
'''
A---->test
B---->aaa
[<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]
'''
a = A()
A.test() # A---->test 然后报错,因为没有aaa
模块安装
模块地址:https://pypi.org/project/pip/
安装安装包:
pip install SomePackage              # 最新版本
pip install SomePackage==1.0.4       # 指定版本
pip install 'SomePackage>=1.0.4'     # 最小版本
卸载包
pip uninstall SomePackage
或者在pycharm里面安装
多态与多态性
多态:一种事物的多种状态

继承:解决冗余问题(双刃剑,继承太多也不好)
多态类中的继承:不是让子类遗传父类的属性和方法,是用来给子类定制标准的
import abc # abstract => 抽象

# 该类已经是抽象类了(子类只要继承必须实现,父类中不实现)
class Animal(metaclass=abc.ABCMeta):
   @abc.abstractmethod # (子类只要继承必须实现,父类中不实现)
   def speak(self):
       pass
   def run(self):
       pass

class People(Animal):
   def speak(self):
       pass

class Dog(Animal):
   def speak(self):
       pass

class Pig(Animal):
   def speak(self):
       pass


obj1 = People()
obj2 = Dog()
obj3 = Pig()


鸭子类型:约定俗成同时都有某一功能,下次只要有人调用这个功能就是一个父类 (看起来像,叫起来像,就是鸭子)
class People(Animal):
   def speak(self):
       pass

class Dog(Animal):
   def speak(self):
       pass

class Pig(Animal):
   def speak(self):
       pass
   
多态性:多态带来的特性,在不用考虑对象数据类型的情况下,直接调用对象的方法
   
len()就是多态的测量出多个数据类型的长度

class People(Animal):
   def speak(self):
       pass

class Dog(Animal):
   def speak(self):
       pass

class Pig(Animal):
   def speak(self):
       pass
   
obj1 = People()
obj2 = Dog()
obj3 = Pig()

def animel(animel):
   return animel.speak()

animel(obj1)

 

##### 组合
组合:一个对象拥有一个属性,该属性是另外一个对象

	解决类与类之间的冗余问题
    	1.继承:满足什么是什么的关系
    	2.组合:满足什么有什么的关系

class People():
    school = 'SH'
    def __init__(self,name,age,gender):
        self.name = name
        self.age = age
        self.gender = gender

class Course():
    def __init__(self,course_name,course_period,course_price):
        self.course_name = course_name
        self.course_period = course_period
        self.course_price = course_price
        
class Student(People):
    def __init__(self,name,age,gender,course=None):
        if course is None:
            self.courses = []
        super.__init__(name,age,gender)
        
    def choose_course(self,course):
        self.courses.append(course)
        print(f'{self.name}选课成功{self.courses}')
        
class Teacher(People):
    def __init__(self,name,age,gender,level):
        self.level = level
        super.__init__(name,age,gender)
    
    def score(self,stu_obj,num):
        stu_obj.num = num
        print(f'{self.name}老师给{stu_obj.name}打了{num}分')
        
stu1 = Student('egon',18,'male')
python = Course('python','6month',20000)
stu1.courses.append(python)
stu2.courses.append(linux)

# 这里没有用继承关系继承Course,因为继承需要满足什么是什么的关系
# 明显课程不是学生也不是老师,所以直接存在Course类里
# 需要的时候造一个对象然后再变成另一个对象的属性,没有冗余
posted @ 2021-07-14 17:32  小白白柏柏  阅读(189)  评论(0)    收藏  举报