您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python基础学习18----面向对象简述

5b51 2022/1/14 8:24:14 python 字数 24753 阅读 577 来源 www.jb51.cc/python

这里就不再讲面向对象的相关概念知识或者与面向过程的比较了,直接进入类的学习 1.类的创建 2.封装 3.继承 子类可以对父类的方法进行重写,子类调用父类的方法使用super(子类名,self),sel

概述

这里就不再讲面向对象的相关概念知识或者与面向过程的比较了,直接进入类的学习

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self):#构造<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>
    pass

sfencs=people()#类的实例

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,name,age):
    self.name=name
    self.age=age

sfencs=people("sfencs",19)
print("%s is %d"%(sfencs.name,sfencs.age))

__init__(self):
        self.a = 1
        self.b = 2
def me(self):
    print("i am father")
    print(self)#<__main__.son object at 0x000001F7DFA11128>

class son(father):

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self):
    <a href="https://www.jb51.cc/tag/super/" target="_blank" class="keywords">super()</a>.<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__()</a>
    super(son,self).me()#执行<a href="https://www.jb51.cc/tag/fulei/" target="_blank" class="keywords">父类</a>的me<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>,但self是people

def me(self):
    print("i am son")

people=son()
print(people.a)#1
people.me()#i am son

def me(self):
    print("i am father")
    print(self)#<__main__.son object at 0x000001F7DFA11128>
def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self):
    <a href="https://www.jb51.cc/tag/super/" target="_blank" class="keywords">super()</a>.<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__()</a>
    super(son,self).me()#执行<a href="https://www.jb51.cc/tag/fulei/" target="_blank" class="keywords">父类</a>的me<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>,但self是people

def me(self):
    print("i am son")

class son(father):

people=son()
print(people.a)#1
people.me()#i am son

def me(self):
    print("i am father")
    print(self)#<__main__.son object at 0x000001F7DFA11128>

class son(father):

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self):
    <a href="https://www.jb51.cc/tag/super/" target="_blank" class="keywords">super()</a>.<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__()</a>
    super(son,self).me()#执行<a href="https://www.jb51.cc/tag/fulei/" target="_blank" class="keywords">父类</a>的me<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>,但self是people

def me(self):
    print("i am son")

people=son()
print(people.a)#1
people.me()#i am son

子类可以对父类方法进行重写,子类调用父类方法使用super(子类名,self),self永远是执行该方法调用

python支持多继承

__init__(self):
        self.a = 1
        self.b = 2
def me(self):
    print("i am father1")

class father2:
def init(self):
self.c = 3
self.d = 4

def me(self):
    print("i am father2")

class son(father1,father2):

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self):
    father1.<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self)
    father2.<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self)
    super(son,self).me()#i am father1

def me(self):
    print("i am son")

people=son()
print(people.c)#3
people.me()#i am son

def me(self):
    print("i am father1")
def me(self):
    print("i am father2")
def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self):
    father1.<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self)
    father2.<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self)
    super(son,self).me()#i am father1

def me(self):
    print("i am son")

class father2:
def init(self):
self.c = 3
self.d = 4

class son(father1,father2):

people=son()
print(people.c)#3
people.me()#i am son

def me(self):
    print("i am father1")

class father2:
def init(self):
self.c = 3
self.d = 4

def me(self):
    print("i am father2")

class son(father1,father2):

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self):
    father1.<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self)
    father2.<a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self)
    super(son,self).me()#i am father1

def me(self):
    print("i am son")

people=son()
print(people.c)#3
people.me()#i am son

多继承中子类调用父类方法的寻找方法是按照父类声明的顺序从左到右,从下到上查找,一直查找到最高级的父类,但是如果不同的父类继承于同一个父类

那么这个相当于根的父类为最后再去查找

python原生多态,不像java,c++那样必须在方法的形参处申明类型

方法

sex="male"为静态字段,可以通过对象访问 也可以通过类访问

self.name=name    self.age=age

sex="male"
def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,"19")

print(sfencs.name)
print(sfencs.sex)
print(people.sex)

静态方法可以通过对象访问 也可以通过类访问,声明静态方法的方式为@staticmethod

sex="male"
def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.age=age
@staticmethod
def func():
    print("这是静态<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>")

sfencs=people("sfencs","19")
sfencs.func()
people.func()

方法也可以通过对象访问 也可以通过类访问,声明类方法的方式为@classmethod,类方法的参数为类

sex="male"
def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.age=age
@classmethod
def func(cls):
    print("这是类<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>")
    print(cls)#<class '__main__.people'>

sfencs=people("sfencs","19")
sfencs.func()
people.func()

属性

属性定义的时候像方法,使用的时候像字段,使用@property声明,这样就可以使sfencs.func有对应的值

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.age=age
@property
def func(self):
    return 1

sfencs=people("sfencs","19")
print(sfencs.func)#1

既然要伪装成字段,那么不仅仅是能够有对应的值,也应该能够为它赋值,将它删除等操作

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.age=age
@property
def func(self):
    return 1
@func.setter
def func(self,val):
    print(val)

@func.deleter
def func(self):
    print("del")

sfencs=people("sfencs","19")
print(sfencs.func)#1
sfencs.func=123
del sfencs.func

 @func.setter为设置赋值的方法,@func.deleter为删除设置的方法

经过这些设置之后,看似func成立一个字段,有了相应的特点,但其实这些特点都是假的。这三种方式只是3中对应关系,只是使用时是模仿字段操作,但真实操作是由

自己规定的,del并不能真的删除,而只是按照你所写的方法做相应的动作。

除此之外属性还有一种设置方式

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.age=age

def f1(self):
    return 1

def f2(self,val):
    print(val)

def f3(self):
    print("del")

func=property(<a href="https://www.jb51.cc/tag/fget/" target="_blank" class="keywords">fget</a>=f1,fset=f2,fdel=f3,doc="描述")

sfencs=people("sfencs","19")
print(sfencs.func)#1
sfencs.func=123
del sfencs.func

只是方式改变了,效果还一样,不多说了。

这里就指将成员声明为私有的

class people:    def __init__(self,age):        self.name=name        self.__age=age    def getage(self):        return self.__agesfencs=people("sfencs","19")#print(sfencs.__age)私有成员不能直接通过对象来拿print(sfencs.getage())#19

当然也有私有方法

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.__age=age
def getage(self):
    return self.__age
def __fun(self):
    print("这是私有<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>")
def func(self):
    self.__fun()

sfencs=people("sfencs","19")

print(sfencs.getage())

sfencs.func()

在继承当中,私有成员与方法是不能被子类继承的

方法

__init__

构造方法,这个不用过多解释

__del__

析构方法,当对象在内存中被释放时,自动触发执行

__call__

对象后面加括号,触发执行

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.__age=age

def __call__(self,a,b):
    print("__call__")
    print(a,b)

sfencs=people("sfencs",19)
sfencs(1,2)

__int__

调用int(对象)时使用的方法

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.__age=age

def __int__(self):
    return 10

sfencs=people("sfencs",19)
data=int(sfencs)
print(data)#10

__str__

那么在打印 对象 时,输出方法的返回值

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.__age=age

def __str__(self):
    return "hello world"

sfencs=people("sfencs",19)
print(sfencs)#hello world

__add__

两个对象相加执行该方法

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.age=age

def __add__(self,other):
    return self.age+other.age

sfencs=people("sfencs",19)
Tom=people("Tom",20)
print(sfencs+Tom)#39

__dict__  

查看类或对象中的所有成员

sex="male"
def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.age=age

sfencs=people("sfencs",19)
print(sfencs.dict)#{'name': 'sfencs','age': 19}
print(people.dict)

__getitem__、__setitem__、__delitem__

用于索引操作,如字典。以上分别表示获取、设置、删除数据

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,age):
    self.name=name
    self.age=age

def __getitem__(self,key):
    print('__getitem__',key)
    return 2

def __setitem__(self,key,value):
    print('__setitem__',value)

def __delitem__(self,key):
    print('__delitem__',key)

sfencs=people("sfencs",19)
sfencs["one"]=1#setitem one 1
print(sfencs["one"])#getitem one 2
del sfencs["one"]#delitem one

__iter__

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,list):
    self.list=list

def __iter__(self):
    return iter(self.list)

sfencs=people([1,2,3,4,5])

for i in sfencs:
print(i)

__module__ 和  __class__

表示当前操作的对象在那个模块

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,19)

print(sfencs.module)#main
print(sfencs.class)#<class 'main.people'>

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

people = type('people',(object,),{'func':f,'init':init})

sfencs=people("sfencs",19)
sfencs.func()

people = type('people',(object,),{'func':f,'init':init})

sfencs=people("sfencs",19)
sfencs.func()

people = type('people',(object,),{'func':f,'init':init})

sfencs=people("sfencs",19)
sfencs.func()

这种方式可以看出,类也是一种对象,是type类型的对象

我们可以从下面这张图看出类的实际创建过程

反射可以通过字符串的方式来找到对象中的变量或方法

反射有4个方法getattr() hasattr() setattr() delattr()

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,19)

data1=getattr(sfencs,"name")
print(data1)#sfencs
data2=hasattr(sfencs,"age")
print(data2)#True
setattr(sfencs,"age",20)
print(sfencs.age)#20
delattr(sfencs,"age")
data3=hasattr(sfencs,"age")
print(data3)#False

python一切事物皆是对象,模块同样也支持反射来操作

对象也叫实例,单例模式表示该类只有一个对象

obj =ConnectPool.get_instance()
print(obj)#<main.ConnectPool object at 0x000002523F0174E0>
obj1 =ConnectPool.get_instance()
print(obj1)#<main.ConnectPool object at 0x000002523F0174E0>
print(obj1==obj)#True

   

总结

以上是编程之家为你收集整理的python基础学习18----面向对象简述全部内容,希望文章能够帮你解决python基础学习18----面向对象简述所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶