面向对象三大特征

面向对象三大特征

封装

class Student():
    school = 'SH'
    name = 'egon'
    
    # 当调用类的时候自动触发的函数
    def __init__(self, name, age ,gender):
        self.name = name
        self.age = age
        self.gender = gender
#之前的类一直是封装的 一直用到了封装

继承

# 1.什么是继承?
  继承就是新建类的一种方式,新建出来的类我们称为子类或者叫派生类,被继承的类称为父类或者基类
  # 子类可以遗传父类的属性

# 2. 为什么要用继承?
	类解决的是对象与对象之间的代码冗余问题
     继承解决类与类之间的代码冗余问题

# 3. 如何使用继承?
	新式类:继承了object类的子子孙孙类都是新式类
     经典类:不继承object类的子子孙孙类都是经典类
        
     # 新式类和经典类只在python2中区分
    在python3中都是新式类
    
class People():
    school = 'SH'

    def __init__(self, name, age, gender):
        # self => stu
        self.name = name
        self.age = age
        self.gender = gender

class Student(People):
    def __init__(self, name, age, gender, course=None):
        if course is None:
            course = []
        self.courses = course
        # self => stu
        People.__init__(self, name, age, gender)  # 普通方法, 指名道姓的调用方法

    def choose_course(self, course):
        # stu_dic['course'].append(course)
        # stu_obj['course'].append(course)
        # stu_obj.courses.append(course)
        # self.courses.append(course)
        self.courses.append(course)
        print("%s选课成功%s" % (self.name, self.courses))


stu = Student('egon', 19, 'male')  # Student(stu, 'egon', 19, 'male')
.单继承下的属性查找
class Foo:
    def f1(self):
        print("Foo.f1")

    def f2(self):
        print("Foo.f2")
        self.f1()  # self => obj  指明道姓调_foo__f1


class Bar(Foo):
    def f1(self):
        print("Bar.f1")


obj = Bar()
obj.f2()
#输出结果
Foo.f2
Bar.f1
#-------------------------------------------------------------
class Foo:
    def __f1(self):  # def _Foo__f1()
        print("Foo.f1")

    def f2(self):
        print("Foo.f2")
        self.__f1()   # self._Foo__f1()         #隐藏属性


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


obj = Bar()
obj.f2()
#输出结果:
Foo.f2
Foo.f1

多继承下的属性查找

# 新式类中属性查找按照   广度优先查询
# 经典类中属性查找按照   深度优先查询

class G(object):
    # def test(self):
    #     print('from G')
    pass

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


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


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


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


class H():
    def test(self):
        print('from H')
    pass


class B(E, H):
    # def test(self):
    #     print('from B')
    pass


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

    pass


f1 = A()
f1.test()
先在A找  然后B  B继承E,H这么一层一层找  H先不找最后CD都找完G也找完再找H

python3全是新式类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IVBtG6p4-1629891051923)(C:\Users\张世虎\AppData\Roaming\Typora\typora-user-images\image-20210825164644124.png)]

在这里插入图片描述

 

super()和mro()

根据起始位置找
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()
# print(A.__mro__)

多态

# 多态:一种事物的多种形态
水:固态水,液态水, 气态水

动物:人,猪, 狗
# 继承, 子类使用父类的属性
多态类中的继承:不是让子类遗传父类的属性

多态带来的特性:在不考虑对象的数据类型的情况下,直接调用方法
import abc  # abstract  抽象类


# 抽象类只能被继承,不能被实例化
class Animal(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def speak(self): pass
    # def speak1(self):pass
    # def speak2(self):pass
    # def speak3(self):pass


# Animal()

# class People(Animal):
#     def speak1(self):
#         pass
#     pass
#
# class Pig(Animal):
#     def speak(self):
#         print("哼哼哼哼")
#     pass
#
#
# class Dog(Animal):
#     def speak(self):
#         print("汪汪汪")
#     pass


# obj1 = People()
# obj2 = Pig()
# obj3 = Dog()
#
# obj1.speak()
# obj2.speak()
# obj3.speak()


# 在python中不推荐上面的写法, 推荐的是下面的写法
class People():
    def speak(self):
        pass


class Pig():
    def speak(self):
        print("哼哼哼哼")

    pass


class Dog():
    def speak(self):
        print("汪汪汪")

    pass


class Txt():
    def speak(self):
        print("汪汪汪")

    pass


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


# obj1.speak()
# obj2.speak()
# obj3.speak()
# obj4.speak()
def animal(animal):
    return animal.speak()


animal(obj1)
animal(obj2)
animal(obj3)
animal(obj4)


print("abc".__len__())
print([1, 2, 3].__len__())
print({'username':"egon"}.__len__())

def len(item):
    return item.__len__()

len("abc")
len([1, 2, 3])
posted @ 2021-08-25 19:31  迪迦张  阅读(21)  评论(0编辑  收藏  举报