python基础-类

一、继承

# 1、继承:如果一个类里面的属性和方法可以复用,则通过继承的方式传递到另一类里
# 2、父类:基类
# 3、子类:派生类

class Master(object):

    def __init__(self):
        self.gong_fu = "古法煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作了一份煎饼果子..." % self.gong_fu)


class Prentice(Master):  # 4、子类可以继承父类所有的属性和方法(私有方法除外)
    pass

caozhe = Prentice()
print(caozhe.gong_fu)
caozhe.make_cake()

二、多继承

class Master(object):
    def __init__(self):
        self.gong_fu = "古法煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作了一份煎饼果子..." % self.gong_fu)

class School(object):

    def __init__(self):
        self.gong_fu = "现代煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作一份煎饼果子..." % self.gong_fu)

class Prentice(Master, School):  # 多继承:继承了多个父类。
    pass

# 多继承可以继承多个父类,也继承了所有父类的属性和方法
# 如果父类有同名的属性和方法,则默认使用第一个父类的属性和方法
# 不重复的属性和方法,没有任何影响。
cao = Prentice()
print(cao.gong_fu)
cao.make_cake()

三、子类重写父类的重名方法

class Master(object):

    def __init__(self):
        self.gong_fu = "古法煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作了一份煎饼果子..." % self.gong_fu)

class School(object):

    def __init__(self):
        self.gong_fu = "现代煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作一份煎饼果子..." % self.gong_fu)


class Prentice(Master, School):

    def __init__(self):
        self.gong_fu = "曹氏煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作了一份煎饼果子..." % self.gong_fu)

# 1、如果子类和父类的属性名和方法名相同,那么默认使用子类的。
cao = Prentice()
print(cao.gong_fu)
cao.make_cake()

四、子类调用父类的重名方法

class Master(object):

    def __init__(self):
        self.gong_fu = "古法煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作了一份煎饼果子..." % self.gong_fu)


class School(object):

    def __init__(self):
        self.gong_fu = "现代煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作一份煎饼果子..." % self.gong_fu)


class Prentice(Master, School):

    def __init__(self):
        self.gong_fu = "曹氏煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作了一份煎饼果子..." % self.gong_fu)

    # 调用父类方法格式:父类类名.父类方法(self),不能用类名+实例属性
    def make_old_cake(self):
        Master.__init__(self)
        # 由于还是用的self.gong_fu即“曹氏煎饼果子配方”,所以的重新调用
        Master.make_cake(self)

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


cao = Prentice()  # 实例化对象,自动执行构造函数
print(caozhe.gong_fu)
cao.make_cake()
cao.make_old_cake()
cao.make_new_cake()

五、多层继承和私有属性和方法

# 1、面向对象的三大特征:封装,继承,多态
# 封装:①将属性和方法放到一起作为一个整体,然后实例化对象来处理
#      ②隐藏内容实现细节,只需要和对象,属性和方法交互就可以了
#      ③对类的属性和方法增加访问权限设置
# 私有属性:在属性名和方法名前面加上__
#    ①类的私有属性,不能通过对象直接访问,但是可以在类内部进行访问
#    ②类的私有方法,不能通过对象直接访问,但是可以在类里访问
#    ③类的私有属性和私有方法都是不对外公开的,用来处理内部事情,起到安全
class Master(object):

    def __init__(self):
        self.gong_fu = "古法煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作了一份煎饼果子..." % self.gong_fu)


class School(object):

    def __init__(self):
        self.gong_fu = "现代煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作一份煎饼果子..." % self.gong_fu)


class Prentice(Master, School):

    def __init__(self):
        self.gong_fu = "曹氏煎饼果子配方"
        # self._Prentice__money = 50 不允许这种写法(害怕重名)
        self.__money = 10000

    def set_money(self, money):
        self.__money = money

    def get_money(self):
        return self.__money

    def make_cake(self):
        print("按照<%s>制作了一份煎饼果子..." % self.gong_fu)

    def make_old_cake(self):
        Master.__init__(self)
        Master.make_cake(self)

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


class PrenticePrentice(Prentice):  # 多层继承
    pass
"Python本身没有强制性的机制来阻止你干坏事(访问私有变量),一切靠自觉。"
# 如果在代码里看见两个__开头的属性,表示是一个私有属性,不能访问和修改
# 如果看到_开头的属性,用法和公有的没有区别,但是有特殊意义,表示这个属性视为保护对象
# 尽管可以访问和修改,但是也不能这么做

pp = PrenticePrentice()
pp.set_money(50)
# 子类不能继承父类的私有属性和私有方法
print(pp.get_money())

六、super()执行父类方法

class Master(object):

    def __init__(self):
        self.gong_fu = "古法煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作了一份煎饼果子..." % self.gong_fu)


class School(object):

    def __init__(self):
        self.gong_fu = "现代煎饼果子配方"

    def make_cake(self):
        print("按照<%s>制作一份煎饼果子..." % self.gong_fu)


class Prentice(Master):

    def __init__(self):
        self.gong_fu = "曹氏煎饼果子配方"
        # self._Prentice__money = 50 不允许这种写法(害怕重名)
        self.__money = 10000

    def set_money(self, money):
        self.__money = money

    def get_money(self):
        return self.__money

    def make_cake(self):
        print("按照<%s>制作了一份煎饼果子..." % self.gong_fu)
        super().__init__()
        super().make_cake()


class PrenticePrentice(Prentice):

    def make_all_cake(self):
        # super只支持新式类
        # 使用super() 可以逐一调用所有的父类方法,并且只执行一次
        # 如果某个类继承了多个父类,则只执行第一个父类(super() 目前不支持执行多个父类)
        super().__init__()
        super().make_cake()
pp = PrenticePrentice()
pp.make_all_cake()
# 检测重名属性和方法执行的顺序(先执行子类,后执行子类继承的父类,再执行父类继承的父类)
print(PrenticePrentice.__mro__)

七、类属性、类方法和静态方法

class Person(object):
    # 1、类属性:在类的内部,方法外部定义的属性。
    # 2、类属性归本类的所有对象共享,而且只在内存中保存一份
    # 3、类属性也支持权限访问,__表示私有属性
    __cls_number = 100

    def __init__(self, name, age):
        self.name = name
        self.age = age
    # 1、类方法:@classmethod 装饰器;用来修饰一个函数,这个函数有特殊作用
    # 2、类方法一般用来处理类属性,有个唯一的参数cls,表示当前类
    # 3、类方法可以被类和对象访问
    @classmethod
    def set__cls_number(cls, num):
        cls.__cls_number = num

    @classmethod
    def get__cls_number(cls):
        return cls.__cls_number

    # 静态函数:装饰器 @ staticmethod
    # 静态函数就是类里面的普通函数,不需要传递self或者cls
    # 如果一个函数不需要访问实例属性和实例方法,或者类属性和类方法
    # 就可以定义一个静态函数,可以减少参数传递的开销。
    @staticmethod
    def add(a, b):
        return a + b

    def print_info(self):
        print("name:%s,age:%d" % (self.name, self.age))
        print("类属性__cls_number:%d" % self.__cls_number)
        print("类属性__cls_number:%d" % self.__class__.__cls_number)

    # 6. 析构函数:当对象被删除/程序结束 自动调用
    def __del__(self):
        print("对象已被删除")

p = Person("python", 28)
p.print_info()
p.set__cls_number(5)
print(p.get__cls_number())

# Python 是一个动态类型的语言,先赋值再去定义类型(编译时定义类型)
# Java、C++、C#静态类型语言,先有类型,再去赋值(编译前定义类型)
# 检查对象是否属于一个类
print(isinstance(p, Person))

八、修改和访问私有属性property

class Person(object):
    def __init__(self):
        self.__num = 100

    def get_num(self):
        return self.__num

    def set_num(self, num):
        self.__num = num
    # 先get_num,后set_num
    num = property(get_num, set_num)
p = Person()
# 不用property需要这样访问私有属性
p.set_num(10000)
print(p.get_num())
# 直接可以通过num来访问。而不用用过方法访问
# 如果给p.sum赋值,则调用set方法
p.num = 1000
print(p.num)
(一)方法二
class Person(object):
    def __init__(self):
        self.__num = 100

    # 利用装饰器解决
    @property
    def num(self):
        return self.__num

    @num.setter
    def num(self, num):
        self.__num = num

p = Person()
p.num = 1000
print(p.num)

property的作用:相当于把方法进行封装,开发者在对属性设置数据时更方便
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值