面向对象

面向对象:
站在上帝的视角下,世界万物一切皆对象,把拥有共同
属性的一类进行归类,这个过程叫class

class 定义类的时候,类的首字母必须是大写

面向对象的三个特性
1.封装
a 实例属性
b 数据属性(类里面的变量)
2.继承
3。多态


经典型
新式型

方法
1.普通方法 :方法可以读写
2.特性方法:具备只读属性方法不能有形式参数
3.静态方法(属于类 只能使用类名来调用),一般把数据属性使用静态使方法来进行处理)
创建一个类
class Person(object):
def show(self):
print('hello world')
Person().show()

输出结果为 hello wolrd

class Person(object):
name='china' #类的属性
def show(self):
print('hello world')
abj=Person()
abj.show()
print(abj.name)
输出结果为 hello wolrd
chain

class Person(object):
name="china"
#name,age可以理解为类的属性
#init 初始化
#构造方法
def __init__(self,name,age):
# 实例属性
self.name = name
self.age = age
print('start')
#清零操作
def __del__(self):
print('end')
def show(self):
print('name :{0},age :{1}'.format(self.name,self.age))
#类实例化的过程,也是针对构造方法初始化的过程(等于调用了__init__的方法)
abj=Person(name='刘乐乐',age='18')
abj.show()

 

class Animal(object):
def __init__(self,age):
self.age=age
@staticmethod
def address():
return "地球"
def show(self,name='🐖'):
print('it is cone form {0},and age is {1},and name is {2}'.format(self.address(),self.age,name))
def func(self,**kwargs):
print(kwargs)

@property
def info(self):
print('hello world')
@property
def getAge(self):
return self.age

abjAminal = Animal(age=30)
abjAminal.show()
abjAminal.info
abjAminal.func(name='liu lele',age=18,city='xian')
继承
父类(基类):被继承的类
子类(派生类):继承其他类

子类继承了父类,到底继承了什么?
属性
实际属性
方法
继承顺序
1. 从上到下
前提条件:
单个类继承
子类重写了父类的方法
2. 从左到右
前提条件
子类继承了多个类
 
class Father(object):

address='西安'

def __init__(self,name,age):
self.name=name
self.age=age

def info(self):
print('this is father method')

class Son(Father):
def __init__(self,name,age,score):
#子类继承了父类的属性
Father.__init__(self,name,age)
# super().__init__(name,age)
# super(Son, self).__init__(name,age)
self.score = score
def show(self):
print('name is {0} and age is {1} and score is {2}'.format(self.name, self.age, self.score))

def info(self):
print('this is son mathod')

son=Son(name='liu lele',age=18,score=99)
son.show()
print(son.address)
son.info()

 


 

class Father(object):

def __init__(self,name,age):
self.name=name
self.age=age

def funM(self):
print('father')

class Mother(object):
def funM(self):
print('mother')

class Son(Father,Mother):
def __init__(self,name,age,score):
Father.__init__(self,name,age)
self.score=score
son=Son(name='liu lele',age=18,score=100)
son.funM()
print(Son.mro())
 

 


     所以在Python中,基于MRO的解析顺序规则,就会从左到右开始查找基类,如果找到第⼀个匹配的属性类,就会 停⽌查找,如果没有,那就继续查找,直到查找到符合要求的为⽌。MRO其实就是通过⼀个C3线性化算法来实现 的,它的核⼼思想为:

⼦类会优先于⽗类检查

多个⽗类会根据它们在列表中的顺序被依次检查

如果对下⼀个类存在两个合法的选择,只能选择第⼀个

子类可以继承多个父类 但是多个父类必须是同一级别关系


 


 这是错的 继承不了

 

 继承历史问题:

python3广度优先
python2 深度优先
class A:
def show(self):
print('A')

class B(A):
pass

class C(A):
def show(self):
print('C')

class D(B,C):
pass

if __name__ == '__main__':
obj=D()
obj.show()

 python2会输出A     python3回输出C

多态:

class Animal(object):
def talk(self):
print('动物会叫')

class Cat(Animal):
def talk(self):
print('猫也会叫')

class Dog(Animal):
def talk(self):
print('狗也会叫')

def func(anomal):
anomal.talk()

if __name__ == '__main__':
#对狗进行一个实例化的过程
dog = Dog()
func(anomal=dog)
 
类的内置方法:
class Person(object):
'''定义人的类'''

def __init__(self):
print('初始化')

def __del__(self):
print('清理')

def __str__(self):
print('返回对象字符串')

def __ceil__(self,*args,**kwargs):
print('对象加()返回的内容')

abj=Person
print(abj)
abj()
print(abj.__doc__)

 


 
posted @ 2022-06-23 15:35  刘乐乐liu  阅读(39)  评论(0)    收藏  举报