类 动静态方法,动静态字段,特性

#!/usr/bin/env python
# -*- coding:utf-8 -*-

class Provice:
    momo = '中国很好'    #静态字段
    #构造函数
    def __init__(self,name,age,salary,flag):
        self.Name = name
        self.Age = age
        self.Salary = salary
        self.__Flag = flag       #私有字段

    #动态方法
    def sport_meet(self):
        print self.Name + '开会'
    '''
    #########静态方法##########
    1.在函数上添加一个装饰器@staticmethod
    2.取出函数中的self
    '''
    @staticmethod
    def Foo():
        print '都要增加GDP'

    @staticmethod
    def add(sql):
        pass
    @staticmethod
    def delete(sql):
        pass
    @staticmethod
    def select(sql):
        pass
    @staticmethod
    def update(sql):
        pass


    #把方法变成特性
    @property
    def Bar(self):
        print self.Name

    #私有方法:
    def __sha(self):
        pass

#静态字段访问
print Provice.momo   #中国很好

#类的实例化
p1 = Provice('su','23','10000',True)
print p1.Name,p1.Age,p1.Salary   #su 23 10000

#对象可以访问静态字段
print p1.momo

#对象调用动态方法
p1.sport_meet()   ####su开会

#静态方法属于类
Provice.Foo()

#访问特性
p1.Bar
#######################################
#数据库静态方法调用
Provice.add('sql')
Provice.select('sql')
Provice.delete('sql')

p1.__sha
原文地址:https://www.cnblogs.com/fengjian2016/p/5249725.html