博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
day 4 继承
阅读量:4554 次
发布时间:2019-06-08

本文共 7157 字,大约阅读时间需要 23 分钟。

1.继承引入,减少代码量

 

  1)版本1:

class Animal:    '''定义一个动物类'''       def eat(self):        print("----吃----")    def drink(self):        print("----喝----")    def sleep(self):        print("----睡觉----")    def run(self):        print("----跑----")class Dog:    def eat(self):        print("----吃----")    def drink(self):        print("----喝----")    def sleep(self):        print("----睡觉----")    def run(self):        print("----跑----")    def bark(self):        print("-----汪汪叫---")a = Animal()a.eat()wangcai = Dog()wangcai.eat()
----吃--------吃----

 

  2)版本2:继承动物类

class Animal:    '''定义一个动物类'''       def eat(self):        print("----吃----")    def drink(self):        print("----喝----")    def sleep(self):        print("----睡觉----")    def run(self):        print("----跑----")class Dog(Animal):    '''    def eat(self):        print("----吃----")    def drink(self):        print("----喝----")    def sleep(self):        print("----睡觉----")    def run(self):        print("----跑----")    '''    def bark(self):        print("-----汪汪叫---")a = Animal()a.eat()wangcai = Dog()wangcai.eat()

 

  3)版本3:

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 Cat(Animal):    def catch(self):        print("---抓老鼠---")a = Animal()a.eat()wangcai = Dog()wangcai.eat()tom = Cat()tom.eat()

     

 

2.子类继承父类的父类

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("---飞----")xiaotianquan = Xiaotq()xiaotianquan.fly()xiaotianquan.bark()xiaotianquan.eat()
---飞---------汪汪叫-------吃----

     

 

3.重写

先在自己的类中查找父类的同名方法,没有的话去父类查找

    

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("---重写叫---")xiaotianquan = Xiaotq()xiaotianquan.fly()xiaotianquan.bark()xiaotianquan.eat()

 

---飞-------重写叫-------吃----

 

 

4.调用被重写的方法

  既要调用父类的方法,又要重写该方法

  1)版本1: Dog.bark(self)

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)xiaotianquan = Xiaotq()xiaotianquan.fly()xiaotianquan.bark()xiaotianquan.eat()
---飞-------重写叫--------汪汪叫-------吃----

 

  2)版本2:super().dark()

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)        #第2种        super().bark()    #super()高级的,上级的xiaotianquan = Xiaotq()xiaotianquan.fly()xiaotianquan.bark()xiaotianquan.eat()

 

5.私有属性私有方法,不能被直接继承

私有属性私有方法可以被公有方法调用,公有方法可以被继承

  1)版本1:

class A:     def __init__(self):         self.num1 = 100         self.__num2 = 222      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) #私有属性不会被继承
---test1---Traceback (most recent call last):  File "09-私有在继承中的表现.py", line 17, in 
b.__test2() #私有方法不会被继承AttributeError: 'B' object has no attribute '__test2'

   2)版本2:

class A:    def __init__(self):        self.num1 = 100        self.__num2 = 222    def test1(self):        print("---test1---")    def __test2(self):        print("--test2---")class B(A):    passb = B()b.test1()#b.__test2() #私有方法不会被继承print(b.num1)#print(b.__num2) #私有属性不会被继承

 

---test1---100

 

 

  3)版本3:通过公有方法调用

class A:    def __init__(self):        self.num1 = 100        self.__num2 = 222    def test1(self):        print("---test1---")    def __test2(self):        print("--test2---")    def test3(self):        self.__test2()        print(self.__num2)class B(A):    passb = B()b.test1()#b.__test2() #私有方法不会被继承print(b.num1)#print(b.__num2) #私有属性不会被继承b.test3()   #公有方法调用私有属性方法

 

---test1---100--test2---222

 

 

  4)版本4:

class A:    def __init__(self):        self.num1 = 100        self.__num2 = 222    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()

 

---test1---100--test2---222Traceback (most recent call last):  File "09-私有在继承中的表现.py", line 29, in 
b.test4() File "09-私有在继承中的表现.py", line 18, in test4 self.__test2()AttributeError: 'B' object has no attribute '_B__test2'

 

    

 

 

6.多继承

经典类

class Dog:        pass

 

新式类   object是所有类的父类

class Dog(object):        pass

 

  

多继承

class Base(object):    def base(self):        print("----this is base-")class A(Base):    def test1(self):        print("---this is A---")class B(Base):    def test2(self):        print("----this is B---")class C(A,B):    passc1 = C()c1.test1()c1.test2()c1.base() 
---this is A-------this is B-------this is base-

    

 

7.多继承注意点

  切记:不要出现相同的方法名

  1)版本1:先调用自身的

class Base(object):    def test(self):        print("----this is base-")class A(Base):    def test(self):        print("---this is A---")class B(Base):    def test(self):        print("----this is B---")class C(A,B):    def test(self):        print("----this is C---")    c1 = C()c1.test()

 

----this is C---

 

  2)版本2:调用哪个父类的AorB?? 

class Base(object):     def test(self):         print("----this is base-")  class A(Base):     def test(self):         print("---this is A---")  class B(Base):     def test(self):         print("----this is B---")  class C(A,B):     pass    # def test(self):     #    print("----this is C---")  c1 = C() c1.test()
---this is A---

 

  3)版本3:调用顺序

class Base(object):     def test(self):         print("----this is base-")  class A(Base):     def test(self):         print("---this is A---")  class B(Base):     def test(self):         print("----this is B---")  class C(A,B):     pass    # def test(self):     #    print("----this is C---")  c1 = C() c1.test() print(C.__mro__)

    

 

转载于:https://www.cnblogs.com/venicid/p/7890007.html

你可能感兴趣的文章
hbase redis mysql重要知识点总结
查看>>
取数字(dp优化)
查看>>
web app builder arcgis 自定义弹窗
查看>>
第六天冲刺
查看>>
Golang学习 - strconv 包
查看>>
ERROR util.Shell: Failed to locate the winutils binary in the hadoop binary path
查看>>
imx6 system boot
查看>>
[SDOI2017]硬币游戏
查看>>
Azure 网站、云服务和虚拟机比较
查看>>
Windows 10在2018四月更新中默认安装了OpenSSH客户端
查看>>
jQuery常用函数
查看>>
一个忙着找实习工作的大三在校生的真实感受!!!
查看>>
Linux 下的 scp
查看>>
理解同步,异步和延迟脚本
查看>>
MMS源码中异步处理简析
查看>>
XMind 6 如何画流程图
查看>>
final发布评价
查看>>
DLL远程注入与卸载
查看>>
Jmeter-ForEach控制器
查看>>
Checklist: 2019 05.01 ~ 06.30
查看>>