songboyao- day01-1h

2021-06-19  本文已影响0人  __method__

面向对象特性

继承, 多态, 封装

类和对象

类是对象的模板, 对象是类的一个实例
类是对一系列具有相同特征和行为的事物的统称,是一个抽象的概念,不是真实存在的事物。

定义类

class 类名():
    属性
    方法
    ......

使用

class Student():
    def study(self):
        print("疯狂学习中。。。")
        print("self 的地址 = {}".format(id(self)))

# 类创建对象的过程叫做 类的实例化
# 对象名 = 类名()
stu1 = Student()
stu2 = Student()
stu1.study()
stu2.study()
print("id stu1 = {}".format(id(stu1)))
print("id stu1 = {}".format(id(stu2)))

self 是当前类对象

init 构造方法

当类被实例化的时候会自动调用

class Student():
    def __init__(self):
        self.name = "张三"
        self.age = 19
    def study(self):
        print("{} 疯狂学习中。。。".format(self.name))
        print("self 的地址 = {}".format(id(self)))

# 类创建对象的过程叫做 类的实例化
# 对象名 = 类名()
stu1 = Student()
stu2 = Student()
stu1.study()

继续迭代

class Student():
    def __init__(self, name = "哈哈", age=0 ):
        print("__init__被调用")
        self.name = name
        self.age = age
    def study(self):
        print("{} 疯狂学习中。。。".format(self.name))
        print("self 的地址 = {}".format(id(self)))
    def __str__(self):
        return "name = {}".format(self.name)

# 类创建对象的过程叫做 类的实例化
# 对象名 = 类名()
stu0 = Student()
stu1 = Student("张三", 12) # 匿名对象
print(stu1.name)
print(stu1.age)
print(stu1) #<__main__.Student object at 0x00000174AF069DC8>
print(stu1) #<__main__.Student object at 0x00000174AF069DC8>

继承

extends 子类继承父类

class 类名(object):
  代码

单继承

一个子类继承一个父类
故事主线:一个煎饼果子老师傅,在煎饼果子界摸爬滚打多年,研发了一套精湛的摊煎饼果子的技术。师父要把这套技术传授给他的唯一的最得意的徒弟。

# 1. 师父类
class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果子配方]'


class Prentice(Master):
    pass

prentice = Prentice()
print(prentice.kongfu)

多继承
一个子类拥有多个父类
故事推进:daqiu是个爱学习的好孩子,想学习更多的煎饼果子技术,就需要去学校进修

class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class School(object):
    def __init__(self):
        self.kongfu = '[知名院校果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')
# 多继承
class Prentice(School, Master):
    pass

prentice = Prentice()
print(prentice.kongfu)
prentice.make_cake()

当一个类有多个父类的时候,默认使用第一个父类的同名属性和方法

子类重写(覆盖 override)父类同名方法和属性

class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

    def haha(self):
        print("dasdasdasd")


class School(object):
    def __init__(self):
        self.kongfu = '[知名院校果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')


# 多继承
class Prentice(School, Master):
    def __init__(self):
        self.kongfu = '[独创院校果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')


prentice = Prentice()
print(prentice.kongfu)
prentice.make_cake()

子类和父类具有同名属性和方法,默认使用子类的同名属性和方法。

子类调用父类的同名方法和属性
class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class School(object):
    def __init__(self):
        self.kongfu = '[知名院校果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')


# 多继承
class Prentice(School, Master):
    def __init__(self):
        self.kongfu = '[独创院校果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')
    # 调用父类方法,但是为保证调用到的也是父类的属性,必须在调用方法前调用父类的初始化
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)

    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)


prentice = Prentice()
prentice.make_master_cake()
prentice.make_school_cake()

super()调用父类方法

class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class School(Master):
    def __init__(self):
        self.kongfu = '[知名院校果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')
        super().__init__()
        super().make_cake()
        print(f'运用{self.kongfu}制作煎饼果子')
        self.__init__()
        print(f'运用{self.kongfu}制作煎饼果子')

# 多继承
class Prentice(School, Master):
    def __init__(self):
        self.kongfu = '[独创院校果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')
    # 调用父类方法,但是为保证调用到的也是父类的属性,必须在调用方法前调用父类的初始化
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)

    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)


# prentice = Prentice()
# prentice.make_master_cake()
# prentice.make_school_cake()
s = School()
s.make_cake()

私有权限

class School(Master):
    def __init__(self):
        self.kongfu = '[知名院校果子配方]'
        self.__money = 100000

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')
        super().__init__()
        super().make_cake()
        print(f'运用{self.kongfu}制作煎饼果子')
        self.__init__()
        print(f'运用{self.kongfu}制作煎饼果子')
    def get_money(self):
        return self.__money
    def set_money(self, money):
        self.__money = money

多态

多态指的是一类事物有多种形态,(一个抽象类有多个子类,因而多态的概念依赖于继承)。

class Dog(object):
    def work(self):  # 父类提供统一的方法,哪怕是空方法
        print('指哪打哪...')
class ArmyDog(Dog):  # 继承Dog类
    def work(self):  # 子类重写父类同名方法
        print('追击敌人...')
class DrugDog(Dog):
    def work(self):
        print('追查毒品...')
class Person(object):
    def work_with_dog(self, dog):  
        # 传入不同的对象,执行不同的代码,即不同的work函数
        dog.work()

ad = ArmyDog()
dd = DrugDog()
guagtouqiang = Person()
guagtouqiang.work_with_dog(ad)
guagtouqiang.work_with_dog(dd)
上一篇下一篇

猜你喜欢

热点阅读