zoukankan      html  css  js  c++  java
  • 【python38--面向对象继承】

    一、继承

    1、语法:class DerivedClassName(BaseClassName):子类继承父类

    >>> class Parent:
        def hello(self):
            print('正在调用父类的方法。。。')
    
            
    >>> class Child(Parent):
        pass
    
    >>> p = Parent()
    >>> p.hello()
    正在调用父类的方法。。。
    >>> c = Child()
    >>> c.hello()
    正在调用父类的方法。。。
    >>> 
    #子类Child继承了父类Parent,所以子类可以调用父类的方法c.hello()

    2、如果子类中定义的方法和属性和父类的属性和方法名字一样,则会自动覆盖父类对应的方法和属性

    import random as r

    class Fish:
    def __init__(self):
    self.x = r.randint(0,10)
    self.y = r.randint(0,10)

    def move(self):
    self.x -=1
    print('我的位置',self.x,self.y)

    class Goldfish(Fish):
    pass

    class Carp(Fish):
    pass

    class Salmon(Fish):
    pass

    class Shark(Fish):
    def __init__(self):
    self.hungry = True

    def eat(self):
    if self.hungry:
    print('吃货的梦想就是天天吃肉!')
    self.hungry = False
    else:
    print('吃撑了!')

    >>> fish = Fish()
    >>> fish.move()
    我的位置 4 7
    >>> goldfish = Goldfish()
    >>> goldfish.move()
    我的位置 6 5
    >>> carp = Carp()
    >>> carp.move()
    我的位置 7 6
    >>> salmon = Salmon()
    >>> salmon.move()
    我的位置 -1 10
    >>> shark = Shark()
    >>> shark.move()
    Traceback (most recent call last):
    File "<pyshell#9>", line 1, in <module>
    shark.move()
    File "/Users/yixia/Desktop/fish.py", line 9, in move
    self.x -=1
    AttributeError: 'Shark' object has no attribute 'x'

    ---报错的原因:AttributeError: 'Shark' object has no attribute 'x'  :Shark没有x的属性,shark继承了Fish,为什么会没有x的属性呢

    原因:Shark重写了__init__的方法,就会覆盖父类Fish的__init__()方法中的x属性,即:子类定义类一个和父类相同名称的方法,则会覆盖父类的方法和属性

    改正的代码:

    两种方法:1,调用未绑定的父类方法  2、使用super()函数

    实现的代码如下:

    第一种:

    import random as r

    class Fish:
    def __init__(self):
    self.x = r.randint(0,10)
    self.y = r.randint(0,10)

    def move(self):
    self.x -=1
    print('我的位置',self.x,self.y)

    class Goldfish(Fish):
    pass

    class Carp(Fish):
    pass

    class Salmon(Fish):
    pass

    class Shark(Fish):
    def __init__(self):
    Fish.__init__(self) #父类名称调用__init__()函数
    self.hungry = True

    def eat(self):
    if self.hungry:
    print('吃货的梦想就是天天吃肉!')
    self.hungry = False
    else:
    print('吃撑了!')

    >>> shark = Shark()
    >>> shark.move()
    我的位置 1 8
    >>>

    第二种:

    import random as r

    class Fish:
    def __init__(self):
    self.x = r.randint(0,10)
    self.y = r.randint(0,10)

    def move(self):
    self.x -=1
    print('我的位置',self.x,self.y)

    class Goldfish(Fish):
    pass

    class Carp(Fish):
    pass

    class Salmon(Fish):
    pass

    class Shark(Fish):
    def __init__(self):
    super().__init__() #采用super()函数
    self.hungry = True

    def eat(self):
    if self.hungry:
    print('吃货的梦想就是天天吃肉!')
    self.hungry = False
    else:
    print('吃撑了!')

    >>> shark = Shark()
    >>> shark.move()
    我的位置 -1 6

    #建议写super(),因为修改的话只要修改一个地方就够了,调用未绑定的父类关系就需要修改很多地方

    3、如何屏蔽父类中的方法

    --覆盖父类方法,子类中定义一个和父类一样的方法,函数内容体写成pass,这样父类的方法就没有任何反应了

    >>> class Brid:
        def fly(self):
            print('翱翔在碧海蓝天之间。')
            
    >>> class Qie(Brid):
        def fly(self):
            pass
    
    >>> bird = Brid()
    >>> bird.fly()
    翱翔在碧海蓝天之间。
    >>> qie = Qie()
    >>> qie.fly()
    
    ---子类屏蔽了父类的fly()方法

    4、定义一个点(Point)类和直线(Line)类,使用getLen方法可以获得直线的长度

    提示:

    *设定点A(X1,Y1),点B(X2,Y2),则亮点构成的直线长度|AB|=√((x1-x2)2+(y1-y2)2)

    *Python中计算开根号可使用math模块中的sqrt函数

    *直线需有亮点构成,因此初始化时需有两个点(Point)对象作为参数

    思路:
    1、直线的长度=两个点的x,y相减的平方和的开平方
    mport math
    
    class Point():
        def __init__(self,x,y):
            self.x = x
            self.y = y
    
        def getX(self):
            return self.x
    
        def getY(self):
            return self.y
    
    class Line():
        def __init__(self,p1,p2):
            self.x = p1.getX() - p2.getX()
            self.y = p2.getY() - p2.getY()
            self.len = math.sqrt(self.x*self.x + self.y*self.y)
    
        def getLen(self):
            return self.len
    
    >>> p1 = Point(1,1)
    >>> p2 = Point(4,5)
    >>> line = Line(p1,p2)
    >>> line.getLen()
    5.0
  • 相关阅读:
    前端页面模拟浏览器搜索功能Ctrl+F实现
    正则表达式中?=和?:和?!的理解
    JRebel激活教程
    BAT脚本一键启动多个程序
    WinInet简介及操作流程
    通过线程传递消息
    两串口收发测试
    获取PC可用串口端口,并将其在combo box中显示
    为MFC应用程序添加登录对话框界面
    Using CInternetFile open an Url
  • 原文地址:https://www.cnblogs.com/frankruby/p/9551473.html
Copyright © 2011-2022 走看看