- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章举例讲解Python面向对象编程中对象的属性与类的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
python 对象的属性 进入正题,来看一个实例来了解python中类,对象中公有属性,私有属性及局部变量,全局变量的区别. 。
1
|
root@10.1.6.200:~
# cat object.py
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/usr/bin/env python
#coding:utf8
class
Dave():
var1
=
"class atribute,public atrribute var1"
#类属性,公有属性var1
__var2
=
"class self atribute __var2"
#类的私有属性__var2
def
fun(
self
):
self
.var2
=
"object public atrribute var2"
#对象的公有属性var2
self
.__var3
=
"object self atrribute __var3"
#对象的私有属性__var3
var4
=
"Function of the local variable var4"
#函数fun的局部变量
def
other(
self
):
print
self
.__var3
|
。
根据上面代码后面加入以下代码可以实例化一个对象及获取类公有属性. 。
1
2
3
|
he
=
Dave()
#实例化一个对象he
print
he.var1
#从实例中获取类的公有属性
print
Dave.var1
#直接从类中获取公有属性
|
1
|
root@10.1.6.200:~
# ./object.py
|
1
2
|
class atribute,public atrribute var1
class atribute,public atrribute var1
|
类的私有属性不能被类或对象直接调用 。
1
2
3
|
he
=
Dave()
print
Dave.__var2
print
he.__var2
|
1
|
root@10.1.6.200:~
# ./object.py
|
1
2
3
4
|
Traceback (most recent call last):
File
"./object.py"
, line
19
,
in
<module>
print
Dave.__var2
AttributeError:
class
Dave has no attribute
'__var2'
|
但可以通过方法间接调用. 。
1
2
3
4
5
6
7
8
9
|
class
Dave():
var1
=
"class atribute,public atrribute var1"
#类属性,公有属性var1
__var2
=
"class self atribute __var2"
#类的私有属性__var2
def
other(
self
):
print
Dave.__var2
he
=
Dave()
he.other()
|
1
|
root@10.1.6.200:~
# ./object.py
|
1
|
class self atribute __var2
|
获取类方法中的对象的公有属性,需要先通过对象执行类中的方法.并通过对象调用该属性. 。
1
2
3
4
5
|
he
=
Dave()
liu
=
Dave()
he.fun()
print
he.var2
print
liu.var2
|
1
|
root@10.1.6.200:~
# ./object.py
|
1
2
3
4
5
|
object
public atrribute var2
Traceback (most recent call last): <span><
/
span>
#对象liu由于没有调用fun方法所有就没有该属性.
File
"./object.py"
, line
20
,
in
<module>
print
liu.var2
AttributeError: Dave instance has no attribute
'var2'
|
对象的私有属性和类的私有属性类似,也不能被类或对象直接调用 。
1
2
3
|
he
=
Dave()
he.fun()
print
he.__var3
|
1
|
root@10.1.6.200:~
# ./object.py
|
1
2
3
4
|
Traceback (most recent call last):
File
"./object.py"
, line
18
,
in
<module>
print
he.__var3
AttributeError: Dave instance has no attribute
'__var3'
|
局部变量也不能被对象直接调用,可以在函数内部使用. 。
1
2
3
|
he
=
Dave()
he.fun()
print
he.var4
|
1
|
root@10.1.6.200:~
# ./object.py
|
。
1
2
3
4
|
Traceback (most recent call last):
File "./object.py", line 18, in <module>
print he.var4
AttributeError: Dave instance has no attribute 'var4'
|
1
2
3
4
5
6
7
8
9
|
def
fun(
self
):
self
.var2
=
"object public atrribute var2"
#对象的公有属性var2
self
.__var3
=
"object self atrribute __var3"
#对象的私有属性__var3
var4
=
"Function of the local variable var4"
#函数fun的局部变量
print
var4
#可以在函数内部直接打印,只在该函数内有用
print
self
.__var3
he
=
Dave()
he.fun()
|
1
|
root@10.1.6.200:~
# ./object.py
|
1
2
|
Function of the local variable var4
object self atrribute __var3
|
那么var4和self._var3有什么区别呢.目前看2个都在外部使用不了.下面在定义一个函数other调用. 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
def
fun(
self
):
self
.var2
=
"object public atrribute var2"
#对象的公有属性var2
self
.__var3
=
"object self atrribute __var3"
#对象的私有属性__var3
var4
=
"Function of the local variable var4"
#函数fun的局部变量
print
var4
#一个函数的局部变量在另外一个函数是访问不到的
print
self
.__var3
def
other(
self
):
print
var4
print
self
.__var3
he
=
Dave()
he.fun()
print
"#"
*
100
he.other()
|
1
|
root@10.1.6.200:~
# ./object.py
|
1
2
3
4
5
6
7
8
9
|
Function of the local variable var4
object self atrribute __var3
####################################################################################################
Traceback (most recent call last): #会认为var4是全局变量打印.定义全局变量可在class 头加入 var4 = "global"
File "./object.py", line 22, in <module>
he.other()
File "./object.py", line 16, in other
print var4
NameError: global name 'var4' is not defined
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/usr/bin/env python
#coding:utf8
var4
=
"global"
#定义var4为全局变量
class
Dave():
var1
=
"class atribute,public atrribute var1"
#类属性,公有属性var1
__var2
=
"class self atribute __var2"
#类的私有属性__var2
def
fun(
self
):
self
.var2
=
"object public atrribute var2"
#对象的公有属性var2
self
.__var3
=
"object self atrribute __var3"
#对象的私有属性__var3
var4
=
"Function of the local variable var4"
#函数fun的局部变量
print
var4
print
self
.__var3
def
other(
self
):
print
var4
print
self
.__var3
#可调用私有属性,前提是先调用fun
he
=
Dave()
he.fun()
print
"#"
*
100
he.other()
|
1
|
root@10.1.6.200:~
# ./object.py
|
1
2
3
4
5
|
Function of the local variable var4
object
self
atrribute __var3
####################################################################################################
global
object
self
atrribute __var3
|
python 类的方法 python类中的方法:公有方法,私有方法,类方法,静态方法. 。
下面通过一个实例了解它们之间的区别
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/usr/bin/env python
#coding:utf8
class
Dave():
name
=
"python"
def
fun1(
self
):
#定义公有方法
print
self
.name
print
"i am public method"
def
__fun2(
self
):
#定义私有方法
print
self
.name
print
"i am self method"
|
先来看公有方法和私有方法,加入以下代码输出 。
1
|
root@10.1.6.200:~
# ./method.py #直接调用对象公有方法没有问题
|
1
2
|
python
i am public method
|
私有方法和私有属性一样是被保护起来,不能直接调用对象的私有方法,但可以间接调用. 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#!/usr/bin/env python
#coding:utf8
class
Dave():
name
=
"python"
def
fun1(
self
):
#定义公有方法
print
self
.name
print
"i am public method"
self
.__fun2()
def
__fun2(
self
):
#定义私有方法
print
self
.name
print
"i am self method"
he
=
Dave()
he.fun1()
|
1
|
root@10.1.6.200:~
# ./method.py
|
1
2
3
4
|
python
i am public method
python
i am
self
method
|
公有属性是可以被类调用,但是公有方法是不可以被类直接调用.需要实例化对象调用.如果想一个方法被类直接调用的话,就需要转换,变成一个类方法.变成类方法有2种,比较简单的可以加装饰器. 。
1
2
3
4
5
6
|
@classmethod
def
classFun(
self
):
#定义类方法
print
self
.name
print
"i am class method"
Dave.classFun()
|
1
|
root@10.1.6.200:~
# ./method.py
|
1
2
|
python
i am class method
|
另一个方法比较麻烦,需要定义一个新的函数,以及使用classmethod方法转换函数为类方法.当然调用也需要使用新的该函数名字. 。
1
2
3
4
5
6
7
8
|
def
classFun(
self
):
#定义类方法
print
self
.name
print
"i am class method"
classnewFun
=
classmethod
(classFun)
Dave.classnewFun()
#被转换后的是一个类方法,原来classfun还是一个普通方法
|
1
|
root@10.1.6.200:~
# ./method.py
|
1
2
|
python
i am class method
|
静态方法在使用中和类方法一样,也是为了让类中直接调用,区别定义时不加self. 。
1
2
3
4
5
6
|
@staticmethod
def
staticFun():
#d定义静态方法
print
Dave.name
#注意不加self,直接打name也不行,会认为调用全局变量,需要使用类型加属性.
print
"i am static method"
Dave.staticFun()
|
1
|
oot@10.1.6.200:~
# ./method.py
|
1
2
|
python
i am static method
|
同样也可以通过一个函数调用 。
1
2
3
4
5
6
7
|
def
staticfun():
#定义静态方法
print
Dave.name
print
"i am static method"
staticnewFun
=
staticmethod
(staticFun)
Dave.staticnewFun()
|
1
|
root@10.1.6.200:~
# ./method.py
|
1
2
|
python
i am static method
|
最后此篇关于举例讲解Python面向对象编程中对象的属性与类的方法的文章就讲到这里了,如果你想了解更多关于举例讲解Python面向对象编程中对象的属性与类的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
1、流程控制语句主要有if、ii...else、elseif(有时也可以写成else if)、switch四种。 PHP中语句格式为: if(条件满足) {执行语句} if(条件满足) {执行
目录 DFS初步概念 DFS例题-迷宫游戏 题目描述 输入输出格式 输入输出样例
This question两年前被问到,但它提到的资源要么不是很有帮助(恕我直言),要么链接不再有效。 必须有一些很好的教程才能理解 Phaser .我已经阅读了 javadoc,但我的眼睛呆滞了,因
This question两年前被问到,但它提到的资源要么不是很有帮助(恕我直言),要么链接不再有效。 必须有一些很好的教程才能理解 Phaser .我已经阅读了 javadoc,但我的眼睛呆滞了,因
这个正则出自这个网站 http://www.regexlab.com/zh/regref.htm 正向预搜索:"(?=xxxxx)","(?!xxxxx)"
chr(9)、chr(10)、chr(13)、chr(32)、chr(34) 所有关于 ASCII码的表格:[url]http://www.asciitable.com/[/url] chr(13)
我是一名优秀的程序员,十分优秀!