python基础-----面向对象编程

来源:互联网 发布:程序员月度工作总结 编辑:程序博客网 时间:2024/05/16 07:29
class Animal(object):    mao = 'mao'   #静态字段   --》》属于类        #在__init__ 方法中的属于动态字段  --->>属于对象  相当于构造函数    def __init__(self, name, age, sex, feet):        self.name = name        self.age = age                #定义一个私有字段        self.__sex = sex                self.__feet = feet            #动态方法    def sport_meet(self):        print self.name + 'run....'            def FwSex(self):        print "访问私有字段"+self.__sex            #静态方法    @staticmethod    def Foo():        print 'fly...'         #把方法访问的形式变成字段访问的形式    @property    def voice(self):        print self.name+"voice..."                return 'voice' #用property一般都带返回值        #定义一个私有方法    def __PrivateMethod(self):        print 'privateMethod....'        #本身共有方法,可以访问私有方法    def PublicMethod(self):        self.__PrivateMethod()        @property #只读    def Feet(self):        return self.__feet        @Feet.setter  #只写    def Feet(self, feet):        self.__feet = feet    animal = Animal('天气', 34, '__sex','1111')# print animal.name# print Animal.Foo()# print animal.voice# # # print animal.__sex #访问报错(Animal instance has no attribute '__sex')# print animal.FwSex()# # animal.__PrivateMethod()  #访问不了私有方法报错(Animal instance has no attribute '__PrivateMethod')print animal.Feetanimal.Feet = '12222'print animal.Feet

0 0
原创粉丝点击