qiaoliang0302

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

继承

class Animal:
    def eat(self):
        print("-----吃-----")
    def drink(self):
        print("-----喝-----")
    def sleep(self):
        print("-----睡觉-----")
    def run(self):
        print("-----跑步-----")
class Dog(Animal):
    def bark(self):
        print("-----汪汪叫-----")
class Xiaotq(Dog):
    def fly(self):
        print("-----飞-----")
class Cat(Animal):
    def catch(self):
        print("-----抓老鼠-----")

wangcai = Xiaotq()
wangcai.bark()
wangcai.sleep()
wangcai.fly()

 

重写

 

class Animal:
    def eat(self):
        print("-----吃-----")
    def drink(self):
        print("-----喝-----")
    def sleep(self):
        print("-----睡觉-----")
    def run(self):
        print("-----跑步-----")
class Dog(Animal):
    def bark(self):
        print("-----汪汪叫-----")
class Xiaotq(Dog):
    def fly(self):
        print("-----飞-----")
    def bark(self):
        print("-----狂叫-----")


wangcai = Xiaotq()
wangcai.bark()

 

class Animal:
    def eat(self):
        print("-----吃-----")
    def drink(self):
        print("-----喝-----")
    def sleep(self):
        print("-----睡觉-----")
    def run(self):
        print("-----跑步-----")
class Dog(Animal):
    def bark(self):
        print("-----汪汪叫-----")
class Xiaotq(Dog):
    def fly(self):
        print("-----飞-----")
    def bark(self):
        print("-----狂叫-----")
        #Dog.bark(self)          #第一种调用被重写的父类方法
        super().bark()          #第二种调用被重写的父类方法

wangcai = Xiaotq()
wangcai.bark()

 

私有方法、私有属性不会被继承

class A:
    def __init__(self):
        self.num1 = 100
        self.__num2 = 200
    def test1(self):
        print("-----test1-----")
    def __test2(self):
        print("-----test2-----")

class B(A):
    pass
b = B()
b.test1()
#b.__test2()       #私有方法不会被继承
print(b.num1)
#print(b.__num2)     #私有属性不会被继承

 

class A:
    def __init__(self):
        self.num1 = 100
        self.__num2 = 200
    def test1(self):
        print("-----test1-----")
    def __test2(self):
        print("-----test2-----")
    def test3(self):
        self.__test2()
        print(self.__num2)
class B(A):
    def test4(self):
        self.__test2()
        print(self.__num2)    
b = B()
b.test1()
#b.__test2()       #私有方法不会被继承
print(b.num1)
#print(b.__num2)     #私有属性不会被继承
b.test3()
b.test4()        #如果在子类中实现的共有方法,不能调用继承的父类中的私有方法和私有属性

 

多继承

class Base(object):    #  新式类 , 不写object是经典类
    def test(self):
        print("----Base----")
class A(Base):
    def test1(self):
        print("----test1----")
class B(Base):
    def test2(self):
        print("----test2----")

class C(A,B):
    pass
c = C()
c.test1()
c.test2()
c.test()

 

class Base(object):    #  新式类 , 不写object是经典类
    def test(self):
        print("----Base----")
class A(Base):
    def test(self):
        print("----test1----")
class B(Base):
    def test(self):
        print("----test2----")

class C(A,B):
    def test(self):
        print("----test c----")
c = C()
c.test()
print(C.__mro__)  #打印搜索的优先顺序:(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.Base'>, <class 'object'>)

 

 

多态

 

class Dog(object):
    def print_self(self):
        print("大家好,我是×××,希望大家以后多多关照")
class Xiaotq(Dog):
    def print_self(self):
        print("hello everybody,我是你们的老大,我是×××")
def introduce(temp):
    temp.print_self()
dog1 = Dog()
dog2 = Xiaotq()
introduce(dog1)
introduce(dog2)

 

 

类属性、实例属性

class Tool(object):
    # 类属性                  类对象、实例对象、实例属性。类属性在实例对象中共享
    num = 0
    def __init__(self,new_name):
        self.name = new_name
        Tool.num += 1

tool1 = Tool("铁锹")
tool2 = Tool("工兵铲")
tool3 = Tool("水桶")
print(Tool.num)

 

类方法、实例方法

class Game(object):
    #类属性
    num = 0
    #实例方法
    def __init__(self):
        #实例属性
        self.name = "laowang"
    #类方法
    @classmethod
    def add_num(cls):  #保存类的引用,cls 是class的简写
        cls.num = 100
    #静态方法
    @staticmethod
    def print_menu():
        print("-----------------")
        print("穿越火线")
        print("1.开始游戏")
        print("2.结束游戏")
        print("-----------------")
game = Game()
#Game.add_num()    #可以通过类的名字调用类方法
game.add_num()     #还可以通过这个类创建出来的对象 去调用这个类方法
print(Game.num)
Game.print_menu()   #通过类去调用静态方法
game.print_menu()   #通过实例对象调用静态方法

 

posted on 2019-08-02 13:20  qiaoliang0302  阅读(114)  评论(0编辑  收藏  举报