python入门第二十三天-----静态字段和静态方法

 
 1 #!/usr/bin/env python3
 2 #-*- coding:utf-8 -*-
 3 '''
 4 Administrator 
 5 2018/7/24 
 6 '''
 7 
 8 class Foo:
 9     def bar(self): #普通方法需要创建对象,通过对象调用
10         print('bar')
11     @staticmethod
12     def sta():#普通方法,保存在类中,通过对象调用
13         print('123')
14     @staticmethod#静态方法,可以不用调用对象,保存在类中,直接调用
15     def sts(a,b):
16         print(a,b)
17     @classmethod
18     def classmd(cls):#类方法必须有一个参数,保存在类中,直接调用
19         print(cls)
20         #cls 类名
21         print('classmd')
22 # Foo.sta()
23 # Foo.sts(3,6)
24 Foo.classmd()
25 
26 # class Province:
27 #     country='中国' #静态字段,属于类
28 #     def __init__(self,name):
29 #         self.name=name  #这个是普通字段,属于对象
30 #
31 #
32 # print(Province.country)
33 # jiangsu=Province('江苏')
34 # print(jiangsu.country)
 1 class Foo:
 2     def bar(self): #普通方法需要创建对象,通过对象调用
 3         print('bar')
 4     @staticmethod
 5     def sta():#普通方法,保存在类中,通过对象调用
 6         print('123')
 7     @staticmethod#静态方法,可以不用调用对象,保存在类中,直接调用
 8     def sts(a,b):
 9         print(a,b)
10     @classmethod
11     def classmd(cls):#类方法必须有一个参数,保存在类中,直接调用
12         print(cls)
13         #cls 类名
14         print('classmd')
15     @property
16     def per(self):  #定义像方法,访问像字段。可以有返回值。  叫属性或者特性
17         print('123')
18 # Foo.sta()
19 # Foo.sts(3,6)
20 # Foo.classmd()
21 obj=Foo()
22 obj.per
原文地址:https://www.cnblogs.com/Mengchangxin/p/9360241.html