一、继承
class Master(object):
def __init__(self):
self.gong_fu = "古法煎饼果子配方"
def make_cake(self):
print("按照<%s>制作了一份煎饼果子..." % self.gong_fu)
class Prentice(Master):
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)
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)
def make_old_cake(self):
Master.__init__(self)
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()
五、多层继承和私有属性和方法
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.__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.__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().__init__()
super().make_cake()
pp = PrenticePrentice()
pp.make_all_cake()
print(PrenticePrentice.__mro__)
七、类属性、类方法和静态方法
class Person(object):
__cls_number = 100
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def set__cls_number(cls, num):
cls.__cls_number = num
@classmethod
def get__cls_number(cls):
return cls.__cls_number
@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)
def __del__(self):
print("对象已被删除")
p = Person("python", 28)
p.print_info()
p.set__cls_number(5)
print(p.get__cls_number())
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
num = property(get_num, set_num)
p = Person()
p.set_num(10000)
print(p.get_num())
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的作用:相当于把方法进行封装,开发者在对属性设置数据时更方便