Simon 's Blog
» 做笔记做笔记
Toggle navigation
Simon 's Blog
HOME
总裁介绍
coper
zongcai
what
ARCH
TAGS
navigation
!!! Python3类和类对象
? python3 ?
2017-12-07 09:20:55
309
0
0
simon88
? python3 ?
python同样支持类和对象。在python3.x中没有了经典类,只有新式类,默认创建的就是新式类。具体的新式类与经典类的不同在python3.x中已经没有意义了。 **类的定义和实例化** python定义简单类如下。 ```python class Car(object): pass c1=Car() ``` 我们定义了一个Car类,其中什么方法也没有实现。c1是我们实例化的类Car,成为对象。 **静态变量** 我们可以在类中定义静态变量,静态变量属于类,既可以使用类访问,也可以使用对象访问。 ```python class Car(object): type='car' c1=Car() print(c1.type) print(Car.type) ``` 运行结果是: ```python car car ``` **动态变量** 动态变量属于类,在第一次创建类的时候传入类中。动态变量只属于对象,只能由对象访问。 ```python class Car(object): type='car' def __init__(self, name, colour): self.name = name self.colour = colour print('我被创建了。') c1=Car('Lamborghini','yellow') print(c1.type) print(Car.type) print('name:%s,colour:%s'%(c1.name,c1.colour)) ``` 运行结果是: ```python 我被创建了。 car car name:Lamborghini,colour:yellow ``` 类被创建的时候就会访问__init__()方法,上面的类__init__方法中请求了两个参数,所以创建的时候就必须要传入两个传输,其中self的意思是对象本身,以本例来说,当创建Car的实例c1时,self就是c1。 因为作用域的原因,传入的两个参数只能在__init__()中使用,如果想要在其它地方调用,就需要将值传给self。而这个参数就被称为动态参数。 **动态方法** 类的动态方法是自定义的方法,同时只有对象能够调用。 ```python class Car(object): type='car' def __init__(self, name, colour): self.name = name self.colour = colour print('我被创建了。') def show(self): print("The car's name is %s and colour is %s." % (self.name, self.colour)) c1=Car('Lamborghini','yellow') print(c1.type) print(Car.type) print('name:%s,colour:%s'%(c1.name,c1.colour)) c1.show() ``` 运行结果是: ```python 我被创建了。 car car name:Lamborghini,colour:yellow The car's name is Lamborghini and colour is yellow. ``` show()就是类的动态方法。 **静态方法** 有动态方法必然会有静态方法。静态方法属于类,类和对象都可以进行调用。 ```python class Car(object): @staticmethod def description(): print('This is a very fast car.') c1=Car() c1.description() Car.description() ``` 静态方法的写法就是在动态方法前加入装饰器,并且不传入self参数。 **类的属性** 在方法前面加入@property装饰器,这个方法就变成了类的属性,可以以调用属性的方式进行方法的调用。 ```python class Car(object): def __init__(self, name, colour): self.name = name self.colour = colour @property def show(self): print("The car's name is %s and colour is %s." % (self.name, self.colour)) c1=Car('Lamborghini','yellow') c1.show ``` 运行结果是: ```python The car's name is Lamborghini and colour is yellow. ``` 可见,运行的时候不需要添加括号。 **私有变量** 在变量前面加入两个下划线就将这个变量变为了私有变量,创建的对象没有办法法访问私有变量,私有变量只能内部访问。那么私有变量怎么显示呢? ```python class Car(object): def __init__(self, name, colour): self.__name = name self.__colour = colour def show(self): print("The car's name is %s and colour is %s." % (self.__name, self.__colour)) c1=Car('Lamborghini','yellow') c1.show() ``` 运行结果是: ```python The car's name is Lamborghini and colour is yellow. ``` 私有变量能在类内部定义方法进行读取。 利用类的特性也可以访问私有字段。 ```python lass Car(object): def __init__(self, name, colour): self.__name = name self.__colour = colour def show(self): print("The car's name is %s and colour is %s." % (self.__name, self.__colour)) @property def name(self): return self.__name c1=Car('Lamborghini','yellow') c1.show() print(c1.name) ``` 运行结果是: ```python The car's name is Lamborghini and colour is yellow. Lamborghini ``` 通过设定和私有字段相似的名称的特性名称,从程序外部看来就好像是访问了私有字段一样,这种方法也是可以访问私有字段的。 **私有方法** 将方法前面加上两个下划线就会变成私有方法,外部就无法访问这个方法了。而如果要访问这个方法只能在类的内部才可以访问。 ```python class Car(object): def __init__(self, name, colour): self.__name = name self.__colour = colour def __show(self): print("The car's name is %s and colour is %s." % (self.__name, self.__colour)) def func(self): self.__show() c1=Car('Lamborghini','yellow') c1.func() ``` 运行结果是: ```python The car's name is Lamborghini and colour is yellow. ``` 私有方法也可以使用以下方法进行调用,但是并不建议这样使用。 ```python class Car(object): def __init__(self, name, colour): self.__name = name self.__colour = colour def __show(self): print("The car's name is %s and colour is %s." % (self.__name, self.__colour)) def func(self): self.__show() c1=Car('Lamborghini','yellow') c1._Car__show() ``` 运行结果是: ```python The car's name is Lamborghini and colour is yellow. ``` **私有变量更改** 如果我们必须更改私有变量呢?其实也是有办法的,可以使用以下的方法。 ```python class Car(object): def __init__(self, name, colour): self.__name = name self.__colour = colour def show(self): print("The car's name is %s and colour is %s." % (self.__name, self.__colour)) @property def func(self): return self.__name @func.setter def func(self,value): self.__name=value c1=Car('Lamborghini','yellow') c1.show() c1.func='car' c1.show() ``` 运行结果是: ```python The car's name is Lamborghini and colour is yellow. The car's name is car and colour is yellow. ``` 这样就实现了私有变量的更改的操作 **析构函数** __init__()在对象被创建的时候进行调用,成为构造函数。而对象被销毁的时候就会调用__del__()函数,成为析构函数。 ```python class Car(object): def __init__(self): print('我被创建了') def __del__(self): print('我要被销毁了') c1=Car() ``` 运行结果是: ```python 我被创建了 我要被销毁了 ``` **__call__方法** python中也有一个方法叫做__call__(),这个方法是干什么用的呢?当对象作为一个函数进行执行的时候,就会执行这个方法。 ```python class Car(object): def __call__(self, *args, **kwargs): print('call') c1=Car() c1() ``` 运行结果是: ```python call ``` 可以看到使用这个方法之后,对象就能添加括号直接进行执行了。
上一篇:
Python模块导入
下一篇:
linux网络协议栈内核分析
0
赞
309 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
提交评论
0
条评论
More...
<>