#encoding:utf-8
'''
Created on 2013-7-29

@author: lixingle
'''
#CLass.......................................................



#定义一个类
class Person():
    #共有属性
    name='lixingle'
    #私有 属性
    __age=22
#当打印对象时自动调用,相党羽头string
    def __str__(self):
        return self.name
#构造方法
    def __init__(self,m='',n=22):
        self.name=m
        self.age=n
#构造方法
    def __del__(self):
        print '释放'
       
#定义共有方法
    def test1(self):#定义一个共有方法
        print '共有方法'
#定义私有方法
    def __test2(self):
        print '私有方法'
#定义类方法 1。加装饰器
#    @classmethod
    def test3(self):
        print '类方法'
        
#定义类方法 2.       
    def test6(self):
        print '类方法'
    classNewTest=classmethod(test6)
#定义静态方法1。加装饰器      
#静态方法没有参数  调用变量要用类名 不能有self,也不能直接写
    @staticmethod       
    def test4():
        print Person.name
        print '静态方法'    
#定义静态方法2。调用函数法     

#    def test5(): #有错和ide有关 运行正常
#        print Person.name
#        print '静态方法22222222'
#    newStaticTest=staticmethod(test5)    
#定义一个内部类
    class Birthday():
        year='1991'    
        
#实例化一个Person对象    
lele=Person()
lele.test1()
 
Person.classNewTest()
Person.test4()
#Person.newStaticTest() 
#实例化一个Person对象的内部类    
#方法1
bir=lele.Birthday()
print bir.year #1991 
#方法2
bir2=Person.Birthday();
print bir2.year

print lele  
zhangsan=Person('zhangsan',10)
print zhangsan.name
    
python学习之路四(类和对象1)python学习之路四(类和对象1)python学习之路四(类和对象1)python学习之路四(类和对象1)

相关文章:

  • 2021-04-20
  • 2022-12-23
  • 2022-12-23
  • 2022-02-15
  • 2021-08-27
  • 2022-12-23
  • 2021-11-30
  • 2021-07-21
猜你喜欢
  • 2022-01-05
  • 2022-01-14
  • 2022-12-23
  • 2021-04-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案