博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面向对象进阶
阅读量:4477 次
发布时间:2019-06-08

本文共 4763 字,大约阅读时间需要 15 分钟。

成员

      类,对象=》静态字段,静态方法,普通字段,普通方法

      通过类访问有:静态字段,静态方法

      通过对象访问:普通字段,普通方法

成员修饰符

面向对象中一些常用特殊方法  __init__     __call__     __delitem__

 

反射查找类的成员

1 #-*- coding:utf-8 -*- 2 class abc(object): 3    def __init__(self,name): 4        self.name=name 5    def a1(self): 6        pass 7  8  9 10 f1=abc.__dict__11 #查看类中方法12 #print(f1)13 14 #反射:找类中的方法,__init__和a115 print(hasattr(abc,'a1'))16 17 #反射:对象,既可以找对象还可以找类中的方法18 f1=abc('zhangs')19 print(hasattr(f1,'name'))20 print(hasattr(f1,'a1'))

 

反射:

反射导入模块,在找类然后初始化对象,最后根据对象知道对应的方法

1 #-*- coding:utf-8 -*- 2  3 #导入模块 4 f1=__import__('f2',fromlist=True) 5  6 #获取f1的类 7 class_name=getattr(f1,'F1') 8  9 #创建对象10 obj=class_name("zhangs")11 12 #获取对象中的方法13 result=getattr(obj,'name')14 print(result)15 16 17 结果:18 zhangs
反射模块

 

 

类静态字段

1 #-*- coding:utf-8 -*- 2  3 class F1(object):        #类静态字段 4     country = '中国' 5     def __init__(self,name): 6         #对象方法 7         self.name=name 8         #self.country="中国"  这样写的缺点就是浪费内存,每一个对象都会有这个这个属性 9     def fun(self):10         pass11 12 a1=F1("黑龙江")13 a2=F1("湖北")14 a3=F1("湖南")

其实带self定义的就是对象方法,每生成一个对象都会有这个方法

 

 

类静态方法staticmethod

1 class F1(object): 2  3     def __init__(self): 4         pass 5     #静态方法必须加这个装饰器 6     @staticmethod 7     def static_func(x): 8         print(x) 9 10 a1=F1()11 #通过对象访问类的静态方法,原则上尽量不使用。要想使用也是用类的方法去访问12 a1.static_func(1)13 #通过类访问静态方法14 F1.static_func(12)
静态方法

静态方法和函数差不多,直接调用。不愿意写函数的就是用这个方法。这个方法在c#或java 中就可以实现函数的方式

 

类方法classmethod

1 #-*- coding:utf-8 -*- 2  3 class F1(object): 4  5     def __init__(self): 6         pass 7     #静态方法必须加这个装饰器 8     @staticmethod 9     def static_func(x):10         print(x)11 12     #类方法会自动加一个类名,必须定义cls13     @classmethod14     def classfunc(cls):15         print(cls)16 17 F1.classfunc()18 19 结果:20 
类方法

类方法比静态方法多一个功能就是传递一个类名

 

 

特性property

1 #-*- coding:utf-8 -*- 2  3 class F1(object): 4    #普通方法 5    def s1(self): 6        print("s1") 7  8    @property 9    def s2(self):10        print("s2")11 12 obj1=F1()13 #执行普通方法14 obj1.s1()15 #执行特性方法,特性方法不需要写(),但是也无法加参数16 obj1.s2
property

将方法伪造成字段

 

给property赋值方法,property中还是很少用到的知道就好了

1 #-*- coding:utf-8 -*- 2  3 class F1(object): 4    @property 5    def s2(self): 6        print("s2") 7    #给特性方法赋值就必须这么写,下面的方法名必须和上面一样 8    @s2.setter 9    def s2(self,value):10        print(value)11 obj1=F1()12 #执行特性方法13 obj1.s214 #给特性方法赋值15 obj1.s2=12316 17 18 结果:19 s220 123
View Code

 

 

成员修饰符(公开或私有)

1 class F1(object): 2    abc=123 3    __aaa=4565 4    def __init__(self): 5        self.__name="zhangs" 6    #类的内部可以正常访问私有修饰符成员 7    def s2(self): 8        print(F1.__aaa) 9        print(self.__name)10 11 12 obj1=F1()13 #静态方法或普通方法都不能直接访问私有修饰符成员14 #obj1.__aaa15 #obj1.__name  私有修饰符的普通方法也无法访问16 17 18 #可以访问类公共修饰符变量19 print(obj1.abc)20 21 #内部可以正常访问私有修饰符方法22 print(obj1.s2())

私有对象修饰符和派生类测试

1 #-*- coding:utf-8 -*- 2  3 class F1(object): 4    abc=123 5    __aaa=4565 6    def __init__(self): 7        self.__name="zhangs" 8    def __test(self): 9        print("test")10    def demo(self):11        #内部可以调用私有修饰符成员对象12        print self.__test()13 14 class F2(F1):15     def abc(self):16         print(self.__name)17 18 f2=F2()19 #私有修饰符对象方法也不能直接调用,派生类也无法调用20 # f2.abc()21 # f2.__test()22 f2.demo()
View Code

 

 

访问私有方法,知道就可以基本没人这么用

class f:

     __name=12

f1=f()

f1_f__name

 

 

 

__call__方法

1 class f1: 2     def __init__(self): 3         print("__init__") 4  5     def __call__(self, *args, **kwargs): 6         print("__call__") 7         return 1 8  9 #很特殊的call方法调用10 f=f1()()11 12 #上面的方法就是下面方法的简写,在看源码的时候要注意就这样写的13 #实际上是调用的call方法14 #f=f1()15 #f()16 17 print(f)18 19 结果:20 __init__21 __call__22 1
__call__

 

 

 

类成员魔法参数  __getitem__    __setitem__     __delitem__

1 #-*- coding:utf-8 -*- 2 class f1: 3     def __init__(self): 4         self.dick={} 5     def __getitem__(self, item): 6          if type(item) == dict: 7             if self.dick.has_key(item):print(self.dick[item]) 8             else:print("{}") 9          else:10             print(item)11 12     def __setitem__(self, key, value):13         self.dick[key]=value14         print(key,value)15 16     def __delitem__(self, key):17            del  self.dick[key]18            print(key)19 20     def __delslice__(self, i, j):21         print("__delslice__")22 23     def __setslice__(self, i, j, sequence):24         print("__setslice__")25 26 27 28 f=f1()29 #查找30 f['aa']31 32 #设置33 f['key']="value"34 35 #删除36 del f['key']37 38 #切片39 f[1:2]40 41 #设置切片42 f[1:2]=[3,6]43 44 #删除切片45 del f[1:2]46 47 48 结果:49 aa50 ('key', 'value')51 key52 slice(1, 2, None)53 __setslice__54 __delslice__
View Code

 

 

__iter__为什么for可以循环某些对象?

1 #-*- coding:utf-8 -*- 2 class f1: 3      #对象中包含这个方法就是说明这个对象可以被迭代了 4      def __iter__(self): 5          yield 1 6          yield 2 7          yield 3 8  9 10 11 f=f1()12 #循环可迭代的对象13 for i in f:14     print(i)

 

转载于:https://www.cnblogs.com/menkeyi/p/6768523.html

你可能感兴趣的文章
安防摄像头云端录像计划快捷配置-LiveNVR Onvif/RTSP流媒体服务
查看>>
包装类
查看>>
[转载]Spring学习4-面向切面(AOP)之aspectj注解方式
查看>>
201621123047 《Java程序设计》第10周学习总结
查看>>
no crontab for root 解决方案
查看>>
IAP 协议
查看>>
设计模式
查看>>
CentOS7修改网卡为eth0
查看>>
Best Quality CAT Caterpillar ET Diagnostic Adapter III
查看>>
定向转发和重定向实现 <select >下拉表单数据传送
查看>>
数组与字符串一(概念和基础)
查看>>
用cocos2d-html5做的消除类游戏《英雄爱消除》(4)——游戏结束
查看>>
CNUOJ 535 黑魔法师之门
查看>>
18. Maven 的单模块 / 多模块之 Spring MVC + Spring + Mybatis 项目讲解(重点)
查看>>
vs2017 F5不会自动编译了
查看>>
hdu 1028
查看>>
取消 Win7 驱动数字签名认证 WIN7 X64 系统
查看>>
East Brother Video Indoor Monitor
查看>>
Python基础数据类型2
查看>>
linux Shell脚本编码格式
查看>>