函数

#coding=utf-8

#类和对象皆可访问静态字段和静态方法,我的理解是:方法后加了self的,类都不能访问
class Person:
sex='male' #静态字段
def __init__(self,name,age,salary): #self属于对象,self 代表下方的p1,不属于类
self.name=name #动态字段
self.age=age
self.__salary=salary #私有字段,只能在类内部访问

#动态方法
def actitons(self):
print self.name+u'正在扫地'

#静态方法 加上@staticmethod,去掉self,静态方法属于类,self属于对象,所以去掉self
@staticmethod
def foo():
print u'好好'

#动态方法转为特性(方法的特性)
@property #此特性为只读
def bar(self):
print self.name

#定义私有方法,私有方法只能内部访问(类访问)
def __sha(self):
print u'我是json'

#访问私有字段,私有方法
def show(self):
print self.__salary
self.__sha()

@property
def foo2(self):
return self.__salary

p1=Person('ZHANGSAN',30,1000)
print p1.name
print p1.age
print p1.sex
Person.foo()
p1.bar
p1.show()
p1._Person__sha()
print p1.foo2
p1.foo()
posted @ 2018-04-20 19:59  hyy0927  阅读(82)  评论(0编辑  收藏  举报