面对对象之@classmethod@staticmethod用法

Posted patrick-py

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面对对象之@classmethod@staticmethod用法相关的知识,希望对你有一定的参考价值。

@classmethod用法(修饰的函数,第一个参数cls默认是类名,调用方法:实例对象或类对象.方法)

class C_mthod(object):
    name = "f**k"
    def __init__(self,name):
        self.name = name
    @classmethod
    def t_mthod(cls):
        print("hello world",cls.name)

d = C_mthod("F**K")
C_mthod.t_mthod()

————————————————————
hello world f**k

@classmethod调用类静态方法,无法调用类继承方法

分享一个爬虫方法,仅供参考

  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 
  4 import random
  5 
  6 import requests
  7 from lxml import etree
  8 
  9 
 10 class WanfangSpider(object):
 11     @classmethod
 12     def crawl_with_keyword(cls, keyword):
 13         url = \'http://s.wanfangdata.com.cn/Paper.aspx?q=\' + keyword
 14         print url
 15         response = requests.get(url, cls.get_headers())
 16         if response.status_code == 200:
 17             return cls.get_info(response.text)
 18         else:
 19             return None
 20 
 21     @classmethod
 22     def get_headers(cls):
 23         user_agent = [
 24             \'Mozilla / 5.0(compatible;MSIE9.0;Windows NT 6.1;Trident / 5.0\',
 25             \'Mozilla / 4.0(compatible;MSIE6.0;Windows NT 5.1\',
 26             \'Mozilla / 5.0(compatible;MSIE7.0;Windows NT 5.1\',
 27             \'Mozilla / 5.0(compatible;MSIE8.0;Windows NT 6.0\',
 28             \'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/50.0.2661.102 Safari/537.36\'
 29         ]
 30         headers = {
 31             \'User-Agent\': random.choice(user_agent)
 32         }
 33         return headers
 34 
 35     @classmethod
 36     def get_info(cls, content):
 37         tree = etree.HTML(content)
 38         divs = tree.xpath(\'//*[@class="left-record"]\')
 39         result = []
 40         for div in divs:
 41             a_dict = {}
 42             url = div.xpath(\'div/a[@class="title"]/@href\')
 43             title = div.xpath(\'div/a[@class="title"]\')[0].xpath(\'string(.)\')
 44             subtitle = div.xpath(\'div[@class="record-subtitle"]\')[0].xpath(\'string(.)\')
 45             # print url, title, subtitle
 46             if not title:
 47                 title = None
 48 
 49             if url:
 50                 url = url[0]
 51             else:
 52                 url = None
 53 
 54             if subtitle:
 55                 subtitle = subtitle.strip()
 56             else:
 57                 subtitle = None
 58             a_dict[\'url\'] = url
 59             a_dict[\'title\'] = title
 60             a_dict[\'subtitle\'] = subtitle
 61             result.append(a_dict)
 62         return result
 63 
 64 
 65 class It199Spider(object):
 66     @classmethod
 67     def crawl_with_keyword(cls, keyword):
 68         url = \'http://www.199it.com/archives/tag/\' + keyword
 69         print url
 70         response = requests.get(url, cls.get_headers())
 71         if response.status_code == 200:
 72             return cls.get_info(response.text)
 73         else:
 74             return None
 75 
 76     @classmethod
 77     def get_headers(cls):
 78         user_agent = [
 79             \'Mozilla / 5.0(compatible;MSIE9.0;Windows NT 6.1;Trident / 5.0\',
 80             \'Mozilla / 4.0(compatible;MSIE6.0;Windows NT 5.1\',
 81             \'Mozilla / 5.0(compatible;MSIE7.0;Windows NT 5.1\',
 82             \'Mozilla / 5.0(compatible;MSIE8.0;Windows NT 6.0\',
 83             \'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36\'
 84         ]
 85         headers = {
 86             \'User-Agent\': random.choice(user_agent)
 87         }
 88         return headers
 89 
 90     @classmethod
 91     def get_info(cls, content):
 92         tree = etree.HTML(content)
 93         articles = tree.xpath(\'//article\')
 94         result = []
 95         for article in articles:
 96             a_dict = {}
 97 
 98             # 提取正文
 99             img_url = article.xpath(\'div[@class="entry-list-left"]/div/a/img/@src\')
100             title = article.xpath(\'div[@class="entry-list-right"]/h2/a/text()\')
101             url = article.xpath(\'div[@class="entry-list-right"]/h2/a/@href\')
102             post_time = article.xpath(\'div[@class="entry-list-right"]/table/tr/td[2]/text()\')
103             tag = article.xpath(\'div[@class="entry-list-right"]/table/tr/td[4]/a/text()\')
104             summary = article.xpath(\'div[@class="entry-list-right"]/p[@class="post-excerpt"]/text()\')
105             print img_url, url, title, post_time, tag, summary
106 
107             # 构造字典
108             a_dict[\'img_url\'] = img_url[0] if img_url else None
109             a_dict[\'title\'] = title[0] if title else None
110             a_dict[\'url\'] = url[0] if url else None
111             a_dict[\'post_time\'] = post_time[0] if post_time else None
112             a_dict[\'tag\'] = tag[0] if tag else None
113             a_dict[\'summary\'] = summary[0] if summary else None
114             result.append(a_dict)
115         return result if len(result) < 10 else result[0: 10]
classmethod类方法使用

@staticmethod(不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。调用方法:实例对象或类对象.方法 )

class A(object):
    bar = 1
    def foo(self):
        print(\'foo\')

    @staticmethod
    def static_foo():
        print(\'static_foo\')
        print(A.bar)

    @classmethod
    def class_foo(cls):
        print(\'class_foo\')
        print(cls.bar)
        cls().foo()

A.static_foo()
A.class_foo()

——————————————————————
static_foo
1
class_foo
1
foo

 

以上是关于面对对象之@classmethod@staticmethod用法的主要内容,如果未能解决你的问题,请参考以下文章

JAVA面对对象三大特征之继承多态

JAVA阶段五之面对对象应用

面对对象之反射

Java 面对对象之多态

javascript面对对象编程 之继承

设计模式面对面之订阅模式