Python---静态方法,类方法,属性方法

1.静态方法

#静态方法:只是名义上归类管理,实际上在静态方法中访问不了类和实例
class Person(object):
    def __init__(self,name):
        self.name=name
    @staticmethod   #截断类与函数关系,不能调用类变量
    def eat():
        print("%s is eating %s"%('ddd','dd'))
d=Person("XiaoMing")
d.eat()

若想调用self,则需自行传实例,如下:

#静态方法:只是名义上归类管理,实际上在静态方法中访问不了类和实例
class Person(object):
    def __init__(self,name):
        self.name=name
    @staticmethod   #截断类与函数关系,不能调用类变量
    def eat(self):
        print("%s is eating %s"%(self.name,'dd'))
d=Person("XiaoMing")
d.eat(d)

2.类方法

#类方法:只能访问类变量,不能访问实例变量
class Person(object):
    A='Xiaoxiong'
    def __init__(self,name):
        self.name=name
    @classmethod
    def eat(self):
        print("%s is eating %s"%(self.A,'ddd'))
d=Person("XiaoMing")
d.eat()

3.属性方法 (作用 隐藏实现细节)

#属性方法
#1.将函数方法变为一个静态属性
class Person(object):
    def __init__(self,name):
        self.name=name
    @property
    def eat(self):
        print("%s is eating %s"%(self.name,'dd'))
d=Person("XiaoMing")
d.eat
#2.修改属性
class Person(object):
    def __init__(self,name):
        self.name=name
        self.__food=None
    @property
    def eat(self):
        print("%s is eating %s"%(self.name,self.__food))
    @eat.setter
    def eat(self,food):
        print("set to food:",food)
        self.__food=food
d=Person("XiaoMing")
d.eat='baozi'
d.eat
#3.删除属性
class Person(object):
    def __init__(self,name):
        self.name=name
        self.__food=None
    @property
    def eat(self):
        print("%s is eating %s"%(self.name,self.__food))
    @eat.setter
    def eat(self,food):
        print("set to food:",food)
        self.__food=food
    @eat.deleter     #删除私有属性self.__food
    def eat(self):
        del self.__food
        print("删完了")
d=Person("XiaoMing")
d.eat
d.eat='baozi'
d.eat
del d.eat  #删除函数方法私有属性后即可删除函数
d.eat

多线程环境下的static静态方法类方法

08-24

今天无意之中看到一篇讨论静态方法和类方法的帖子。于是就想着测一测,遇到了部分问题。希望有大神能给解答一下rnrn我的java代码:rnrn[color=#FF0000]包:[/color][color=#00FF00]isStatic[/color]rnMethod.javarn[code=java]rnpackage isStatic;rnrnimport java.io.FileReader;rnimport java.io.IOException;rnimport java.util.Properties;rnrnpublic class Method rn rn public static void littlelong1(String name) rn long i = 0;rn while (i ++ != 100000000l) rn //do nothing , but not sleeprn rn rnrn public void littlelong2(String name) rn long i = 0;rn while (i ++ != 100000000l) rn //do nothing , but not sleeprn rn rn /*这是为了我测试方便,可以删掉的*/rn public static int getConfig() rn Properties p = new Properties();rn try rn p.load(new FileReader("pro.properties"));rn catch (IOException e) rn e.printStackTrace();rn rn return Integer.parseInt(p.getProperty("LENGTH"));rn rnrn[/code]rnUseLittlelong.javarn[code=java]rnpackage isStatic;rnrnpublic class UseLittlelong rn rn private static int LENGTH = 20;rn private int[] avg_static = new int[LENGTH];rn private int[] avg_nonstatic = new int[LENGTH];rn //dividedrn private Method m = new Method();rn rn public static void main(String[] args) rn UseLittlelong ull = new UseLittlelong();rn ull.staticTest();rn ull.nonstaticTest();rn try rn Thread.sleep(2000);rn catch (InterruptedException e) rn e.printStackTrace();rn rn ull.avg();rn rn /*test static*/rn public void staticTest() rn int i = -1 ;rn /*原谅我选择了再循环中启动线程,只是这样显得更方便一些*/rn while (i ++ < LENGTH - 1) rn new Use(true,i).start();rn rn rn /*test non-static*/rn public void nonstaticTest() rn int i = -1 ;rn while (i ++ < LENGTH - 1) rn new Use(false,i).start();rn rn rn /*average count*/rn public void avg() rn long sta = 0,nonsta = 0;rn rn for (int i = 0 ; i < LENGTH; i++) rn sta += avg_static[i];rn nonsta += avg_nonstatic[i];rn rn rn System.out.println("static avg :" + sta / LENGTH);rn System.out.println("non-static avg :" + nonsta / LENGTH);rn rn rn /**rn * @author qyp199312rn */rn class Use extends Thread rn rn private boolean isStatic;rn private String threadName;rn private int threadId;rn rn public Use(boolean b,int threadId) rn this.isStatic = b;rn this.threadId = threadId;rn this.threadName = (b ? "[static-" : "[nonstatic-")+ threadId +"] ";rn rn rn public void run () rn if (isStatic) rn long begin = System.currentTimeMillis();rn Method.littlelong1(threadName);rn avg_static[threadId] = (int) (System.currentTimeMillis() - begin);rn// System.out.println(threadName + avg_static[threadId] );rn else rn long begin = System.currentTimeMillis();rn// new Method().littlelong2(threadName);rn m.littlelong2(threadName);rn avg_nonstatic[threadId] = (int) (System.currentTimeMillis() - begin);rn// System.out.println(threadName + avg_nonstatic[threadId] );rn rn rn rnrn[/code]rnpro.propertiesrn[code=java]rn#可在源码中置换为实际数字rnLENGTH = 20rn[/code]rn我在本机上(i5-4300U 1.9GHz 8G)跑出来的结果为:rn[code=java]rnstatic avg :602rnnon-static avg :280rn[/code]rn我在服务器上(Intel(R) Xeon(R) CPU E5520 @ 2.27GHz 48G)跑多次出来的结果为:rn[code=java]rn[tt ~]$ java isStatic/UseLittlelongrnstatic avg :438rnnon-static avg :505rn[tt ~]$ java isStatic/UseLittlelongrnstatic avg :439rnnon-static avg :480rn[tt ~]$ java isStatic/UseLittlelongrnstatic avg :486rnnon-static avg :526rn[tt ~]$ java isStatic/UseLittlelongrnstatic avg :337rnnon-static avg :448rn[tt ~]$ java isStatic/UseLittlelongrnstatic avg :460rnnon-static avg :517rn[tt ~]$ java isStatic/UseLittlelongrnstatic avg :369rnnon-static avg :466rn[tt ~]$ java isStatic/UseLittlelongrnstatic avg :402rnnon-static avg :441rn[tt ~]$ java isStatic/UseLittlelongrnstatic avg :462rnnon-static avg :498rn[tt ~]$ java isStatic/UseLittlelongrnstatic avg :372rnnon-static avg :433rn[/code]rnrn疑问:rn[color=#FF0000]1.修改线程数,得到的时间近似正比翻倍。而按理说排除掉多线程分担掉的cpu消耗,不管多少个线程执行平均时间应该是近似相等的。这是我的程序的BUG还是其他什么的原因?rn2.忽略掉本机和服务器性能差异,静态方法调用和非静态方法的调用时间占比应该差不多。而明显的,本机上静态方法调用时间占用较大,而服务器上类方法的调用时间占用较大。这又是为什么。rn服务器jdk:rnjava version "1.7.0_45"rnJava(TM) SE Runtime Environment (build 1.7.0_45-b18)rnJava HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)rn本机jdkrnjava version "1.7.0_40"rnJava(TM) SE Runtime Environment (build 1.7.0_40-b43)rnJava HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)[/color]rnrn集思广益,求大神们踊跃发言。让更多的人看见,让更多的人明白。

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试