zoukankan      html  css  js  c++  java
  • 面向对象编程(一)

    面向对象知识

    一、 类的定义

    基本形式:
    class ClassName(object):
            Statement

    1.class定义类的关键字
    2.ClassName类名,类名的每个单词的首字母大写。
    3.object是父类名,object是一切类的基类。在python3中如果继承类是基类可以省略不写。


    二、类的初始化
    # __init__

    定义类时,这种方法可以使类对象实例按某种特定的模式生产出来。

    后面的参数中第一个参数我们约定俗成的为self参数名,
    self代表的是在类实例化后这个实例对象本身。

    初始化函数除了有self这个参数表示实例对象本身之外,
    其他的参数的定义也遵循函数的必备参数和默认参数一样的原则,
    必备参数就是在实例化时一定要传入的参数,
    默认参数就是在定义时可以给这个参数一个初始值。


    三、类的实例化

    基本形式:实例对象名 = 类名(参数)

    在实例化的过程中,self代表的就是这个实例对象自己。

    实例化时会把类名后面接的参数传进去赋值给实例,
    这样传进去的参数就成为了这个实例对象的属性。

    实例化的过程遵循函数调用的原则。
    在实例化时也必须个数和顺序与定义时相同(使用关键字参数可以改变传参的顺序)。
    当初始化函数定义时使用了默认参数时,在实例化时默认参数可以不传参这时
    这个实例对象就会使用默认的属性,如果传了参数进去则会改变这参数值,
    使实例化对象的属性就为你传进来的这个参数。

    三(1) 类的定义示例

    class Fruits:
          fruits = 'xxxxxx' # 类属性
          def __init__(self,name,color,weight=90):
                self.name = name
                self.color = color
                self.weight = weight
                self.fruits = 'yyyyy' # 实例属性
    
          def show(self):
                print('我的重量是%s'% self.color)
    
    class Apple(Fruits):
         def __init__(self,color,weight,shape):
               Fruits.__init__(self,'apple',color,weight) # 调用父类的初始化函数
               self.shape = shape
         def eat(self):
               print('被吃掉了。。。。')
    
    ap1 = Apple('red',100,'圆的')

     a、必备参数实例:

    class Fruits:
        def __init__(self,name,color):
            self.name = name
            self.color = color
    
    #初始化实例
    fr1=Fruits('apple','red')
    fr2=Fruits('banana','yellow')

    # 类的实例化,当类中有必备参数时,实例化类的时候必须传入参数,比如水果类Fruits中的初始化函数init中的name和color是必备参数,在初始化时候就必须对这两个参数进行赋值,演示操作如下所示:

    fr1 = Fruits('apple','red')   ----->传入参数'apple'和'red'
    fr2 = Fruits('banana','yellow') ------>传入参数'banana'和'yellow'

    如果不赋值,直接初始化就会报错:

    class Fruits:
        def __init__(self,name,color):
            self.name = name
            self.color = color
    
    #fr1=Fruits('apple','red')
    #fr2=Fruits('banana','yellow')
    
    fr4 = Fruits()
    fr5 = Fruits()
    
    输出结果:
    Traceback (most recent call last):
      File "D:/Python36/note/类基础知识.py", line 9, in <module>
        fr4 = Fruits()
    TypeError: __init__() missing 2 required positional arguments: 'name' and 'color'
    >>> 

     b、默认参数示例:

    class Fruits:
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight=weight  ---->初始化时候赋值实例中的weight等于传入的weight值
    
    fr1=Fruits('apple','red')
    fr2=Fruits('banana','yellow')

    输出结果:

    >>> fr1.name
    'apple'
    >>> fr1.color
    'red'
    >>> fr1.weight
    90
    >>> fr2.name
    'banana'
    >>> fr2.color
    'yellow'
    >>> fr2.weight
    90
    >>>

     对于默认参数weight=90而言,这个在类初始化的时候可以不赋值,也可以赋值,但是如果要想实例化后实例有该属性,就必须在初始化中进行初始化赋值一下,即self.weight=weight;

    如果对默认参数不赋值,则实例化之后,示例不具备类的默认属性

    class Fruits:
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
    
    
    fr1=Fruits('apple','red')
    fr2=Fruits('banana','yellow')
    
    输出结果:
    >>> fr1.color
    'red'
    >>> fr1.name
    'apple'
    >>> fr1.weight
    Traceback (most recent call last):
      File "<pyshell#56>", line 1, in <module>
        fr1.weight
    AttributeError: 'Fruits' object has no attribute 'weight'
    >>> fr2.name
    'banana'
    >>> fr2.color
    'yellow'
    >>> fr2.weight
    Traceback (most recent call last):
      File "<pyshell#59>", line 1, in <module>
        fr2.weight
    AttributeError: 'Fruits' object has no attribute 'weight'
    >>> 

     c、修改默认参数值

    class Fruits:
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
    
    
    fr1=Fruits('apple','red',200)
    fr2=Fruits('banana','yellow',150)
    fr3 = Fruits('apple','green')
    
    输出结果:
    >>> fr1.weight
    200
    >>> fr2.weight
    150
    >>> 
    >>> fr3.weight
    90
    >>>

    说明:此处输出weight值已经不是当初默认90,而是我们自己定义的200和150,说明我们定义的值已经覆盖了默认的值,同时如果不传值给weight,则直接输出默认值90(fr3实例所示)

    四、isinstance(实例名,类名)
    判断一个实例是不是这个类的实例。

    >>> a =  'python'
    >>> isinstance(a,str)
    True
    >>> isinstance(fr1,Fruits)
    True
    >>> isinstance(fr2,Fruits)
    True
    >>> isinstance(fr3,Fruits)
    True
    >>> 

    判断是否为真,返回布尔型数据,True or False

    五、 类和实例属性

    类属性
    .类属性是可以直接通过“类名.属性名”来访问和修改。
    .类属性是这个类的所有实例对象所共有的属性,
    任意一个实例对象都可以访问并修改这个属性(私有隐藏除外)。
    .对类属性的修改,遵循基本数据类型的特性:列表可以直接修改,字符串不可以,
    所以当类属性是一个列表时,通过任意一个实例对象对其进行修改。
    但字符串类型的类属性不能通过实例对象对其进行修改。

    五(1)类属性通过实例.属性名来访问:

    class Fruits:
        fruits='xxxxxx'   ------->类的属性fruits
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
    
    
    fr1 = Fruits('apple','red',200)
    fr2 = Fruits('banana','yellow',150)
    fr3 = Fruits('apple','green')
    
    测试结果:
    >>> fr1.fruits
    'xxxxxx'
    >>> fr2.fruits
    'xxxxxx'
    >>> fr3.fruits
    'xxxxxx'
    >>> 

    类的属性可以通过实例.类属性名格式来访问,这里fr1.fruits和fr2.fruits以及fr3.fruits都是通过实例.属性名来访问的

    这里注意区别的是:当类中和初始化方法中的属性名字相同时,则初始化方法中实例属性将会把类的属性覆盖掉,输出的是初始化方法中的类属性

    class Fruits:
        fruits='xxxxxx'     ---------->初始化方法外面定义的类属性fruits
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYYY' ------------->初始化方法里定义的实例属性fruits
    
    
    fr1 = Fruits('apple','red',200)
    fr2 = Fruits('banana','yellow',150)
    fr3 = Fruits('apple','green')
    
    测试结果:
    >>> fr1.fruits
    'YYYYYY'
    >>> fr2.fruits
    'YYYYYY'
    >>> fr3.fruits
    'YYYYYY'
    >>> 

    说明:此时输出初始化方法中的实例的属性值,而类中的属性fruits值被覆盖掉,从上述代码中可以得出,用了self.属性名表示实例属性,直接属性名fruits=‘xxxxx’表示类属性

    五(2)实例属性
    .在属性前面加了self标识的属性为实例的属性。
    .在定义的时候用的self加属性名字的形式,在查看实例的属性时
    就是通过实例的名称+‘.’+属性名来访问实例属性。

    方法属性
    .定义属性方法的内容是函数,函数的第一个参数是self,代表实例本身。

    一些说明:
    .数据属性会覆盖同名的方法属性。减少冲突,可以方法使用动词,数据属性使用名词。
    .数据属性可以被方法引用。
    .一般,方法第一个参数被命名为self,,这仅仅是一个约定,
    self没有特殊含义,程序员遵循这个约定。

    类中方法访问示例:

    class Fruits:
        fruits='xxxxxx'
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYYY'
        def show(self):
            print('我的重量是%s' % self.weight)
    
    
    fr1 = Fruits('apple','red',200)
    fr2 = Fruits('banana','yellow',150)
    fr3 = Fruits('apple','green')
    
    输出结果:
    >>> fr1.show
    <bound method Fruits.show of <__main__.Fruits object at 0x0000000002B384E0>>
    >>> fr1.show()
    我的重量是200
    >>> fr2.show
    <bound method Fruits.show of <__main__.Fruits object at 0x0000000002B386D8>>
    >>> fr2.show()
    我的重量是150
    >>> fr3.show
    <bound method Fruits.show of <__main__.Fruits object at 0x0000000002BDE4A8>>
    >>> fr3.show()
    我的重量是90
    >>> 

    通过实例来访问类中的方法的方式为实例名.方法名(),上述代码中fr1.show()、fr2.show()、fr3.show()都是访问类中的方法

    五(3)查看类中的属性和实例属性可以调用__dict__方法返回属性组成的字典。

    class Fruits:
        fruits='xxxxxx'
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYYY'
        def show(self):
            print('我的重量是%s' % self.weight)
    
    
    fr1 = Fruits('apple','red',200)
    fr2 = Fruits('banana','yellow',150)
    fr3 = Fruits('apple','green')
    
    
    print(fr1.__dict__['name'])
    print(fr2.__dict__['name'])
    print(fr3.__dict__['name'])
    
    输出结果:
    apple
    banana
    apple

     五(4)实例化之后的类中的属性可以用实例.属性名='赋值'的方法来修改实例化时的赋值

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYY'
    
        def show(self):
            print('我的体重为%s' % self.weight)
    
    
    #类的实例化
    fr1 = Fruits('apple','red')
    fr2 = Fruits('banana','yellow')
    fr3 = Fruits('apple','green',100)
    
    输出结果:
    >>> fr1.color
    'red'
    >>> fr1.color = 'green'
    >>> fr1.color
    'green'
    >>> fr2.color
    'yellow'
    >>> fr2.color = 'black'
    >>> fr2.color
    'black'
    >>> fr3.color
    'green'
    >>> fr3.color = 'white'
    >>> fr3.color
    'white'
    >>> 

    从上述代码中可以发现开始实例化类赋值给color属性的值,在实例化之后依然可以修改

    所以为了防止实例中的属性值被修改,这里引入私有变量

    六、 私有变量 & 类本地变量。

    以一个下划线开头的命名(无论是函数,方法或数据成员),
    都会被隐藏起来。但直接将命名完整的写下还是一样可以访问到。
    单下划线只是隐藏了属性,但还是可以从外部访问和修改。

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self._name = name
            self._color = color
            self._weight = weight
            self.fruits = 'YYYYY'
    
        def show(self):
            print('我的体重为%s' % self.weight)
    
    
    #类的实例化
    fr1 = Fruits('apple','red')
    fr2 = Fruits('banana','yellow')
    fr3 = Fruits('apple','green',100)

    测试结果:

    从上述查找发现,实例对应的name、color、weight属性都被隐藏起来,下面演示是否可以访问或者修改

    >>> fr1._name
    'apple'
    >>> fr1._name = 'cell'
    >>> fr1._name
    'cell'
    >>> fr2._name
    'banana'
    >>> fr2._name = 'paopao'
    >>> fr2._name
    'paopao'
    >>> fr3._name
    'apple'
    >>> fr3._name = 'qq'
    >>> fr3._name
    'qq'
    >>> 

    结果发现虽然隐藏的实例的属性,但是属性对应的值仍然可以访问,而且可以修改

    对于以两个下划线开头的命名,一样会被隐藏,只能在内部访问和修改,
    不能在外部访问,因为它变成了“_类名__属性”的形式。
    在外部只能通过这种形式才能访问和修改。
    双下划线开头的属性就变成了私有变量,即使能改也不要在外部外部修改了。
    私有变量的作用就是确保外部代码不能随意修改对象内部的状态。

    示例代码:

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.__name = name
            self.__color = color
            self._weight = weight
            self.fruits = 'YYYYY'
    
        def show(self):
            print('我的体重为%s' % self.weight)
    
    
    #类的实例化
    fr1 = Fruits('apple','red')
    fr2 = Fruits('banana','yellow')
    fr3 = Fruits('apple','green',100)

    属性name和color前面各添加了两个下划线,那么在实例中显示如下:

    此时name和color的属性已经被改名为:_类名_属性名的结构了,那么如果要访问则可以通过如下方法访问:

    在类的外部访问:

    >>> fr1._Fruits__name
    'apple'
    >>> fr2._Fruits__name
    'banana'
    >>> fr3._Fruits__name
    'apple'
    >>>
    
    >>> fr1._Fruits__color
    'red'
    >>> fr2._Fruits__color
    'yellow'
    >>> fr3._Fruits__color
    'green'
    >>>

    如果在类的内部访问则可以直接用__属性名的结构访问:

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.__name = name
            self.__color = color
            self._weight = weight
            self.fruits = 'YYYYY'
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.__name,self.__color))
    
    
    #类的实例化
    fr1 = Fruits('apple','red')
    fr2 = Fruits('banana','yellow')
    fr3 = Fruits('apple','green',100)
    
    #测试输出结果:
    >>> fr1.show()
    我的名字为apple 颜色为red
    >>> fr2.show()
    我的名字为banana 颜色为yellow
    >>> fr3.show()
    我的名字为apple 颜色为green
    >>> 

    注:另外对于单个下划线或者两个下划线的属性虽然可以修改,但是一般不建议去修改,直接使用原来的赋值即可

    七、 数据封装:

    .在类里面数据属性和行为用函数的形式封装起来,
    访问时直接调用,不需知道类里面具体的实现方法。

    八、 继承:

    .在定义类时,可以从已有的类继承,
    被继承的类称为基类(或者父类),新定义的类称为派生类(子类)。

    如果子类中没有自定义任何方法和属性,则子类中所有的方法和属性均来自于父类

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.__name = name
            self.color = color
            self._weight = weight
            self.fruits = 'YYYYY'
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.__name,self.color))
    
    
    
    class Apple(Fruits):
          pass
    
    
    ap1 = Apple('apple','red')

    测试输出结果:

    >>> ap1.color
    'red'
    >>> ap1.show()
    我的名字为apple 颜色为red
    >>> ap1.weight
    90
    >>> ap1._Fruits__name
    'apple'
    >>>

    如果将子类中的init方法重写,那么父类中init方法中的属性都会丢弃掉:

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.__name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYY' #实例属性
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.__name,self.color))
    
    
    
    class Apple(Fruits):
        def __init__(self,color,weight):
            pass
    
    ap1 = Apple('apple','red')

    测试结果:

    从该显示中可以发现,子类中重新了init方法,父类中的init方法中的属性name、color、weight、fruits都会被丢弃。如果要用自定义的属性,则必须在重写的init中对对应的属性进行赋值,具体如下:

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.__name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYY' #实例属性
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.__name,self.color))
    
    
    
    class Apple(Fruits):
        def __init__(self,color,weight):
            self.color = color
            self.weight = weight

    ap1 = Apple('apple','red')

    输出结果为:

    >>> ap1.color
    'apple'
    >>> ap1.weight
    'red'
    >>> 

    但是仔细一看感觉不对劲,color的值怎么是apple,weight的值是red,进一步分析,发现在ap1 = Apple('apple','red')实例化的时候,将'apple'穿给了color,'red'传给了weight,所以这个传参是按照子类自己的init方法来进行传参,如果子类没有写init方法,那么就按照父类的init方法进行传参赋值,所以对上述代码进行修改:

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYY' #实例属性
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.name,self.color))
    
    
    
    class Apple(Fruits):
        def __init__(self,color,weight):
            self.color = color
            self.weight = weight
            self.name = 'apple' #----------->直接苹果的名字赋值写进去
    
    
    ap1 = Apple('red',100)
    
    测试结果:
    >>> ap1.color
    'red'
    >>> ap1.name
    'apple'
    >>> ap1.weight
    100
    >>> 

    但是综合修改和初始的代码,发现修改的部分的跟以前的代码有重复,主要表现在:

            self.name = name
            self.color = color
            self.weight = weight

    所以这里再次对代码做进一步的修改:

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYY' #实例属性
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.name,self.color))
    
    
    
    class Apple(Fruits):
        def __init__(self,color,weight):
            Fruits.__init__(self,'apple',color,weight) #---------->调用父类中的init方法
            
    ap1 = Apple('red',100)
    
    访问属性输出:
    >>> ap1.name
    'apple'
    >>> ap1.color
    'red'
    >>> ap1.weight
    100
    >>> 

    说明:在子类中执行Fruits.__init__(self,'apple',color,weight)之后,相当于在子类中执行了原来父类中的:

    def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight

    这部分语句,并且在执行过程中将'apple'在子类中直接运用Fruits.__init__(self,'apple',color,weight)语句传递给调用父类的init中的name参数。

    如果子类的init中有父类中没有的属性,则必须用self.属性='属性'来做个赋值,这里shape,则用self.shape = shape

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYY' #实例属性
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.name,self.color))
    
    
    
    class Apple(Fruits):
        def __init__(self,color,weight,shape):
            
            Fruits.__init__(self,'apple',color,weight)
            self.shape = shape
           
    
    ap1 = Apple('red',100,'boll')
    
    输出结果:
    >>> ap1.shape
    'boll'
    >>> 

    在子类实例化时,调用父类init方法中属性值会覆盖子类实例化时赋给对应变量的值:

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYY' #实例属性
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.name,self.color))
    
    
    
    class Apple(Fruits):
        def __init__(self,color,weight,shape):
            
            Fruits.__init__(self,'apple','yellow',weight)
            self.shape = shape
         
    ap1 = Apple('red',100,'boll')
    
    输出结果:
    >>> ap1.color
    'yellow'
    >>> 

    注意:在实例化时传入的color参数对应的值为red,但是初始化方法init中直接赋值color为yellow,这里初始化方法init中直接赋值会覆盖掉实例化时传值,在调用父类属性时,要么直接在调用时对属性赋值要么在子类中直接重写,否则就户报错

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYY' #实例属性
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.name,self.color))
    
    
    
    class Apple(Fruits):
        def __init__(self,name,color,weight,shape):
            
            Fruits.__init__(self,'apple','yellow',weight)
            self.shape = shape
            self.name = name    --------------->重写赋值
    
    ap1 = Apple('pear','red',100,'boll')
    
    输出结果:
    >>> ap1.name
    'pear'
    >>> ap1.color
    'yellow'
    >>> ap1.shape
    'boll'
    >>> 

    如果在子类中重写了父类中的方法,那么子类实例化后执行父类方法时,就会失效,此时输出的是子类重写之后的方法

    重写show方法前:

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYY' #实例属性
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.name,self.color))
    
    
    
    class Apple(Fruits):
        def __init__(self,color,weight,shape):
            
            Fruits.__init__(self,'apple','yellow',weight)
            self.shape = shape
            
        
    
    
    ap1 = Apple('red',100,'boll')
    
    调用show方法:
    >>> ap1.show()
    我的名字为apple 颜色为yellow
    >>> 

    重写show方法后:

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYY' #实例属性
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.name,self.color))
    
    
    
    class Apple(Fruits):
        def __init__(self,color,weight,shape):
            
            Fruits.__init__(self,'apple','yellow',weight)
            self.shape = shape
            
        def show(self):
            print('输出子类的show方法')
    
    
    ap1 = Apple('red',100,'boll')
    调用show方法:
    >>> ap1.show()
    输出子类的show方法
    >>> 

    除了重写,子类也可以定义自己的方法:

    class Fruits:
        fruits = 'xxxxx'
        def __init__(self,name,color,weight=90):
            self.name = name
            self.color = color
            self.weight = weight
            self.fruits = 'YYYYY' #实例属性
    
        def show(self):
            print('我的名字为%s 颜色为%s' % (self.name,self.color))
    
    
    
    class Apple(Fruits):
        def __init__(self,color,weight,shape):
            
            Fruits.__init__(self,'apple','yellow',weight)
            self.shape = shape
            
        def show(self):
            print('输出子类的show方法')
    
        def eat(self):
            print('this apple eaten')
    
    
    ap1 = Apple('red',100,'boll')
    
    调用方法:
    >>> ap1.eat()
    this apple eaten
    >>> 

    但是父类的实例fr1、fr2、fr3是没有子类实例中eat方法的,子类中 方法只能在子类中使用,父类使用不了;但是父类的方法如果被子类继承了,则可以被子类继续使用

    .在类中找不到调用的属性时就搜索基类,
    如果基类是从别的类派生而来,这个规则会递归的应用上去。
    反过来不行。

    .如果派生类中的属性与基类属性重名,那么派生类的属性会覆盖掉基类的属性。
    包括初始化函数。

    .派生类在初始化函数中需要继承和修改初始化过程,
    使用’类名+__init__(arg)’来实现继承和私有特性,也可以使用super()函数。

    issubclass(类名1,类名2)
    判断类1是否继承了类2

    # 多态:

    .多态是基于继承的一个好处。

    .当派生类重写了基类的方法时就实现了多态性。

    代码示例:

     1 '''
     2 一、根据课堂上讲的内容写个人的类。
     3   要求:
     4       1. 属性包含姓名,性别,年龄。
     5       2.方法可以自己随便写。
     6 
     7 '''
     8 class Human:
     9     def __init__(self,name,sex,age,hobby,reading):
    10         self.name = name
    11         self.sex = sex
    12         self.age = age
    13         self.hobby = hobby
    14         self.reading = reading
    15     def sleep(self):
    16         print('%s每天晚上在10:00前必须睡觉' % self.name)
    17         
    18     def active(self):
    19         print('%s喜欢%s,不喜欢%s' %(self.name,self.hobby,self.reading))
    20 
    21 print('####################第一题代码输出#########################')
    22 person1 = Human('小明','','21','swimming','story')
    23 person1.sleep()
    24 person1.active()
    25 
    26 person2 = Human('吉米','','20','basketball','picutre')
    27 person2.sleep()
    28 person2.active()
    29 print('
    ')
    30 
    31 '''
    32 二、写一个学生的类,学生的类继承了人的类。
    33    要求:
    34        1.在继承人的属性的基础上增加成绩这一属性。
    35        2.把之前分数等级测试的函数写到这个类里,作为这个类的一种方法。
    36        提示:把input和循环语句都删掉,只留下if判断。
    37 '''
    38 
    39 class Student(Human):
    40     def __init__(self,score):
    41         self.score = score
    42         
    43     def info(self):
    44         print('my name is %s,I am %s, and %s years,I like %s' %(self.name,self.sex,self.age,self.hobby))
    45     def test(self):
    46             if type(self.score) == int or type(self.score)==float:
    47                 self.score = float(self.score)
    48                 if 90 <= self.score <=100:
    49                     print('成绩为:A')
    50                 elif 80<=self.score<90:
    51                     print('成绩为:B')
    52                 elif 60<=self.score<80:
    53                     print('成绩为:C')
    54                 elif 0<=self.score<60:
    55                     print('成绩为:D')
    56                 else:
    57                     print('输入有误!')
    58             else:
    59                 print('输入有误,请重新输入')
    60 
    61 print('####################第二题代码输出#########################')
    62 student1=Student(8.1)
    63 student1.test()
    64 student2=Student(80.5)
    65 student2.test()
    66 student3=Student(91)
    67 student3.test()
    68 
    69 student4= Student(98)
    70 student4.name = 'alex'
    71 student4.sex=''
    72 student4.age=25
    73 student4.hobby='walking'
    74 student4.info()
     1 #类的多继承
     2 class A:
     3     def show(self):
     4         print('AAAAA')
     5 
     6 
     7 class B:
     8     def fun(self):
     9         print('BBBBB')
    10 
    11 
    12 
    13 
    14 class C(A,B):
    15     pass
    16 
    17 x = C()
    18 #输出结果:
    19 >>> x.show()
    20 AAAAA
    21 >>> x.fun()
    22 BBBBB
    23 >>> 

    类多继承,是类从上到下依次继承多个父类的方法和属性,这里类C继承了类A和B的的两个方法show和fun;那么如果两个父类中都有同样名称的方法,应该是哪个优先呢?我们继续往下看:

    class A:
        def __init__(self,name,age,weight):
            self.name = name
            self.age = age
            self.weight = weight
        def show(self):
            print('AAAAA')
    
    
    class B:
        def __init__(self,name,age):
            self.name = name
            self.age = age
            
        def show(self):
            print('BBBBB')
    
    
    
    
    class C(A,B):
        def __init__(self,name,age):
            self.name = name
            self.age = age
            
    
    X=C('nike',18)
    
    #输出结果:
    >>> X.name
    'nike'
    >>> X.age
    18
    >>> X.show()
    AAAAA
    >>> 

    从上面输出发现,多个类继承,方法名相同时,最前面类中的方法优先使用,且覆盖后面类中对应同样的方法

  • 相关阅读:
    微信小程序入门
    webpack
    模块化开发(1)
    HTML5表单
    移动端入门
    MySQL
    js面向对象与PHP面向对象总结
    PHP
    Git指令
    Redux
  • 原文地址:https://www.cnblogs.com/kindnull/p/6762077.html
Copyright © 2011-2022 走看看