Python: static methods and class methods

之前,在类中定义的方法都是给对象的方法
实际上我们写在类中的方法不都是对象的方法
例如:
我们定义一个三角形类,通过传入三边的边长来构造三角形,并提供计算周长和面积的方法
但是传入三条边的长度不一定都能构造出三角形,
我们可以先写一个方法来验证三条边能否构成三角形
这个方法肯定不是对象方法,因为这件事情是我在创建三角形对象之前做的
这个方法属于三角形类而不属于三角形对象

Part of the word

triangle    英[ˈtraɪæŋɡl]
美[ˈtraɪæŋɡl]
n. 三角形; 三角形物体; 三角铁(打击乐器); 三角关系;
[例句]This design is in pastel colours with three rectangles and three triangles
这一设计由淡色的3个长方形和3个三角形构成。
[其他]   复数:triangles


static 英[ˈstætɪk]
美[ˈstætɪk]
adj.   静止的; 静态的; 停滞的; 静力的;
n. 天电(干扰); 静电; 静力学;
[例句]The number of young people obtaining qualifications has remained static or decreased
获得资格证的年轻人数量一直维持不变或出现下降。

 

from math import sqrt


class Triangle(object):
    def __init__(self, a, b, c):
        self._a = a
        self._b = b
        self._c = c

    # 定义静态方法
    @staticmethod
    def is_valid(a, b, c):
        return a + b > c and a + c > b and b + c > a

    # 对象方法
    def perimeter(self):
        return self._a + self._b + self._c

    # 海伦公式 面积 = 半周长*(半周长-a)*(半周长-b)*(半周长-c)开平方
    def area(self):
        half = self.perimeter() / 2
        return sqrt(half * (half - self._a) * (half - self._b) * (half - self._c))


def main():
    a, b, c = 3, 4, 5
    if Triangle.is_valid(a, b, c):
        # 创建对象
        t = Triangle(a, b, c)
        print(t.perimeter())
        # 也可以给类发消息来调用对象方法但是要传入接受消息的对象作为参数
        print(Triangle.area(t))
    else:
        print('无法构成三角形')


if __name__ == '__main__':
    main()

sample graph:

Formula used here to Helen, Helen, companies need the Pythagorean theorem, we have to prove it:

Pythagorean theorem:

Icon

Resolve to get:

 

Helen formula:

 

Helen company own the Pythagorean theorem and prove it, the feeling to open a new column say mathematics is also good ~ ~ ~ ha ha ha, Wu Nai genius

 

# Class method 

"" " 
and more similar to the static method, Python can also define a class method in the class, 
first class method parameter convention for cls, it represents the current object-related information 
(class itself is a target , in some places called metadata object class) 
by this parameter we can get and class-related information and can create objects out of class 
. "" "

 

import time


class Clock(object):
    """数字时钟"""

    def __init__(self, hour=0, minute=0, second=0):
        self._hour = hour
        self._minute = minute
        self._second = second

    @classmethod
    def now(cls):
        current_time = time.localtime(time.time())
        return cls(current_time.tm_hour, current_time.tm_min, current_time.tm_sec)

    def run(self):
        """走字"""
        self._second += 1
        if self._second == 60:
            self._second = 0
            self._minute += 1
            if self._minute == 60:
                self._minute = 0
                self._hour = 0
                if self._hour == 24:
                    self._hour = 0

    def show(self):
        """显示时间"""
        # 占位符 留两个位置
        return '%2d:%2d:%2d' % (self._hour, self._minute, self._second)


def main():
    # 通过类方法创建对象并获取系统时间
    clock = Clock.now()
    while True:
        print(clock.show())
        time.sleep(1)
        clock.run()


if __name__ == '__main__':
    main()

sample graph:

"" " 
Relationship between classes 
is - a: inheritance (generalization) relationship such as the relationship between students and people, mobile phones and electronic products belong to the inheritance 
has - a: the relationship between departments and employees of the association, automobiles and engines the association relations are all 
          related and if it is part of the overall association, called aggregation relationship 
          how to further responsible for the overall life-cycle part (indivisible part of the whole and, at the same time and also demise), this relationship is the relationship between the synthesis of 
use - a: dependent on such drivers have a driving method, in which the parameters used in the car, the driver and the car is a dependency 
"" "

 

The completion of certain operations on the basis of existing class, or create a new class on the basis of the existing class, these are an important means to achieve code reuse, reuse existing code can not only reduce the workload of the development , but also conducive to the maintenance and management of the code.

UML good university textbooks

UML object-oriented design basis

Extraction code: jy9x

Links to resources on it here

"" " 
Relationship between classes 
is - a: inheritance (generalization) relationship such as the relationship between students and people, mobile phones and electronic products belong to the inheritance 
has - a: the relationship between departments and employees of the association, automobiles and engines the association relations are all 
          related and if it is part of the overall association, called aggregation relationship 
          how to further responsible for the overall life-cycle part (indivisible part of the whole and, at the same time and also demise), this relationship is the relationship between the synthesis of 
use - a: dependent on such drivers have a driving method, in which the parameters used in the car, the driver and the car is a dependency 
"" "
"""
在已有类的基础上完成某些操作,
也可以在已有类的基础上创建新的类,
这些都是实现代码复用的重要手段,
复用现有的代码不仅可以减少开发的工作量,
也利于代码的维护和管理。
"""
'''
在已有类的基础上创建新类,
这其中的一种做法就是让一个类从另外一个类那里把属性和方法全部继承下来
从而减少重复代码的编写。
提供继承信息的我们称为父类,也叫超类或者基类
得到继承信息的我们称为子类,也叫派生类或者衍生类
子类除了可以继承父类提供的属性和方法,还可以定义自己的属性和方法
子类比父类拥有更多的能力
在实际开发中,我们通常会用子类对象替换一个父类对象,这是面向对象编程中常见行为
对应的原则称为里氏替换原则
'''

 

# 继承和多态

class Person(object):
    """人"""

    def __init__(self, name, age):
        self._name = name
        self._age = age

    @property
    def name(self):
        return self._name

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, age):
        self._age = age

    def play(self):
        print('%s正在快乐的玩耍' % self._name)

    def watch_av(self):
        if self._age >= 18:
            print('%s正在观看爱情动作片' % self._name)
        else:
            print('%s正在画圈圈' % self._name)


class Student(Person):
    """学生"""

    def __init__(self, name, age, grade):
        super().__init__(name, age)
        self._grade = grade

    @property
    def grade(self):
        return self._grade

    @grade.setter
    def grade(self, grade):
        self._grade = grade

    def study(self, course):
        print('%s的%s正在学习%s' % (self._grade, self._name, course))


class Teacher(Person):
    def __init__(self, name, age, title):
        super().__init__(name, age)
        self._title = title

    @property
    def title(self):
        return self._title

    @title.setter
    def title(self, title):
        self._title = title

    def teach(self, course):
        print('%s%s正在讲%s' % (self._name, self._title, course))


def main():
    stu = Student('王大锤', 15, '初三')
    # 使用子类的方法
    stu.study('数学')
    # 使用父类的方法
    stu.watch_av()
    t = Teacher('她与断剑', 22, '叫狮')
    # 使用子类的方法
    t.teach('python')
    # 使用父类的方法
    t.watch_av()


if __name__ == '__main__':
    main()

示例图:

'''

override   英[ˌəʊvəˈraɪd]
美[ˌoʊvərˈraɪd]
v. (以权力) 否决,推翻,不理会; 比…更重要; 凌驾; 超驰控制,超控(使自动控制暂时失效,改用手工控制);
[例句]The welfare of a child should always override the wishes of its parents
孩子的幸福安康应该永远比父母的愿望来得更重要。
[其他]   第三人称单数:overrides 现在分词:overriding 过去式:overrode 过去分词:overridden

poly   英[ˈpɒli]
美[ˈpɑːli]
n. 同 polytechnic; (旧时英国的)理工学院(现在多已改为大学) ;
[例句]He portrays the psyche as poly-centric.
在他的描绘中精神是多中心的。
[其他]   复数:polys

morphology
英[mɔːˈfɒlədʒi]
美[mɔːrˈfɑːlədʒi]
n. 形态学; 形态论; 词法;
[例句]This paper the morphology of gastric glands and mucous surface in the hatchlings of alligator sinensis.
本文观察了初孵扬子鳄胃粘膜表面及胃腺的形态。

After the subclass inherits the parent class can be given a new version of an existing parent class, that is overridden (override) 
by a method that allows rewriting the same behavior in the parent class has a different subclasses version 
when we call this method after a subclass rewrite, different subclasses of objects exhibit different behavior, namely polymorphism (poly-Morphology) 
'' '

 

Polymorphism

"" " 
In the code below, we will Pet treated as an abstract class is not able to create objects of class, 
this class is designed for other classes to inherit it 
Python grammar layer does not provide an abstract class support like java / c # like that 
by abc module ABCMeta metaclasses and abstactmethod wrapper to achieve abstract class 

key: 
the following code 
Dog and Cat are two subclasses of the abstract method make_voice Pet class rewritten 
method call, made in the same manner different things (polymorphic behavior) . 
"" "

 

from abc import ABCMeta, abstractmethod


class Pet(object, metaclass=ABCMeta):
    """宠物"""

    def __init__(self, nickname):
        self._nickname = nickname

    @abstractmethod
    def make_voice(self):
        """发出声音"""
        pass


class Dog(Pet):
    """狗"""

    def make_voice(self):
        print('%s:汪汪汪...' % self._nickname)


class Cat(Pet):
    """猫"""

    def make_voice(self):
        print('%s:喵喵喵...' % self._nickname)


def main():
    pets = [Dog('旺财'), Cat('凯蒂'), Dog('大黄')]
    for pet in pets:
        pet.make_voice()


if __name__ == '__main__':
    main()

sample graph:

Published 52 original articles · won praise 34 · views 2608

Guess you like

Origin blog.csdn.net/weixin_38114487/article/details/103966367