面向对象,类的介绍

类:包括 方法(函数)和 属性(相当于参数)
属性:类属性,实例属性

class Name(object):
  eye = 3 #类属性,静态属性
  leg = []
  def __init__(self,name,age):                 #构造方法 类在实例化时调用self相当于指向实例化对象
    self.name=name                                #实例属性
    self.age=age
    pass
  def fun(self):                                        #self是必备参数
    pass           

duixiang=Name()                                        #实例化对象duixiang

'''
属性eye是不可变的数字 int:

duixiang.eye                     # 3

Name.eye=4                   #改值
duixiang.eye                     # 4 类属性改变,实例化对象属性改变 @该属性是不可变的 int

duixiang.eye=5                 #改值
Name.eye                          # 4 实例化对象属性改变,类属性不改变 @该属性是不可变的 int


属性leg是可变的列表 [] :

duixiang.leg                # []
Name.leg                    # []

duixiang.leg.append(1)      #添加   [1]
Name.leg                            # [1] 实例化对象属性改变,类属性改变 @该属性是可变的 []
'''

类的私有变量:
_a 单下划线      __b 双下划线                          # 这2种下划线开头命名的只是一种约定,私有
print(_a)              print(duixiang.__类名__b)     #如果要访问,通过类调用他的 __b

类的特殊方法:
#xxx.__dict__               # 打印实例属性 ,返回字典 {},不包类属性。
class A:
''' 简介....... '''
  a=1
  def __init__(self,length,width):
    self.length = length
    self.width = width
  def play(self):
    return self.length+self.width
  def __str__(self):
    return '提示1'
  def __repr__(self):
    return '提示2'
  def __call__(self):
    return '类不能直接调用,类是调用类的方法'
  def __add__(self,other):
    return self.play()+other.play()
  def __sub__(self,other):
    return self.play()-other.play()
ces = A(10,5)
ces1=A(10,1)
print(ces.__dict__)                  #返回{'length': 10, 'width': 5} 不包括类属性a=1

ces.a=10                                 #实例化后,将类属性修改,变为自己的实例属性
print(ces.__dict__)                  #返回{'length': 10, 'width': 5,'a':10}

#ces.__doc__                         # 导入简介

#__str__                                 # print(ces) -给人看的,字符不会显示引号    没有__str__返回ces的类型及地址,有就默认调用__str__的值 '提示1'

#__repr__                               # ces  -给机器看的,显示所有,字符会显示引号  没有__repr__返回ces的类型及地址 ,有就默认调用__repr__的值 '提示2' 。如果没有__str__方法 ,
                                                   print(ces) 也调用__repr__的值 '提示2'。
#__call__                                 #ces()    没有__call__,ces()不能直接调用,类是调用类的方法ces.play(),有就调__call__的值
#__add__                                 #ces+ces1 没有__add__,ces和ces1不能直接相加,有就调用 __add__能得到 值 26(15+11)
#__sub__                                 #ces-ces1  没有__sub__, ces和ces1不能直接相减,有就调用 __sub__能得到 值 4(15-11)

面向对象三大特征:

1.封装:
'''
def add(name,age):           
  pass
def delate(name,age):          
  pass

#这儿调用函数需要每次传入参数
'''

class Test:
  def __init__(self,name,age):
    self.name=name
    self.age=age
  def add(self):
    pass
  def delate(self):
    pass
#而类只需要在实例化对象是传入参数,在调用类的方法 将同类的的属性(参数?)封装为一个类,提高可读性

2.继承:
单继承

子类(派生类)继承 父类(基类)。
子类可重写父类方法
class Animal:
  def __init__(self,name,age):
    self.name=name               #实例属性

    self.age = age


  def eat(self):
    return '吃饭'
class Dog(Animal):                                                        # 单继承 子类(派生类)继承 父类(基类)。
  def eat(self):                                                           # 子类可重写父类方法
    print(super().eat())                                           # super 调用父类 Animal
    #print(super(Animal,self).eat())                         # super 调用父类Animal的父类(没写Animal的父类)
    return '吃啥呢'
多继承:

super :用法 调用 该类(可以指定) 的父类方法
#继承排序:
xxx().__class__.mro()              #可查看继承顺序
A,B(A在前调用A类):          C-> A-> B-> Base-> object
B,A(B在前调用B类):          C-> B-> A-> Base-> object

class Base:                                #这儿没写构造方法,默认继承基类object的构造方法
  def play(self):
    print('this is Base')
class A:
  def play(Base): #继承 Base 类
    print('this is A')
class B:
  def play(Base): #继承 Base 类
    print('this is B')
class C:
  def play(B,A): #继承 A和B 类

    #A.play(self)       #在C 里调用 A的play方法   |    B.play(self)

    #super().play()        #super调用C类的父类的方法 A,B(A在前调用A 类)    |        B,A(调用B) 

    #super(C,self).play()    #等同上面


    #super(A,self).play()    #调用 Base 类
    #super(B,self).play()    #调用 A 类

    print('this is C')
3.多态
#多态, (子类重写父类方法) ,继承。 (同一个方法,有不同表形式)
class A:
  def play(self):
    return 1

class B(A):
  def play(self):     #子类重写父类play方法
    return 2

原文地址:https://www.cnblogs.com/tangpg/p/8029219.html