# 一日一技：什么情况使用静态方法和类方法？

``````class People:
def __init__(self, name, age):
self.name = name
self.age = age

def introduce_myself(self):
print(f'大家好，我叫: {self.name}')

def add_two_string_num(self, a, b):
a_int = int(a)
b_int = int(b)
return a_int + b_int

def calc_age_after_n_year(self, n):
age = self.add_two_string_num(self.age, n)
print(f'{n}年以后，我{age}岁')

``````def add_two_string_num(a, b):
a_int = int(a)
b_int = int(b)
return a_int + b_int

class People:
def __init__(self, name, age):
self.name = name
self.age = age

def introduce_myself(self):
print(f'大家好，我叫: {self.name}')

def calc_age_after_n_year(self, n):
age = add_two_string_num(self.age, n)
print(f'{n}年以后，我{age}岁')

kingname = People('kingname', 20)
kingname.introduce_myself()
kingname.calc_age_after_n_year(10)

``````class People:
def __init__(self, name, age):
self.name = name
self.age = age

def introduce_myself(self):
print(f'大家好，我叫: {self.name}')

@staticmethod
a_int = int(a)
b_int = int(b)
return a_int + b_int

def calc_age_after_n_year(self, n):
age = People.add_two_string_num(self.age, n)
print(f'{n}年以后，我{age}岁')

kingname = People('kingname', 20)
kingname.introduce_myself()
kingname.calc_age_after_n_year(10)

``````xxx = People('xxx', 10)

``````import re
content = '我的名字：青南，我的年龄：20，把它提取出来'
name = re.search('名字：(.*?)，', content).group(1)
age = re.search('年龄：(\d+)', content).group(1)
kingname = People(name, age)

``````import re

class People:
def __init__(self, name, age):
self.name = name
self.age = age

def introduce_myself(self):
print(f'大家好，我叫: {self.name}')

@staticmethod
a_int = int(a)
b_int = int(b)
return a_int + b_int

@classmethod
def from_chinese_string(cls, sentence):
name = re.search('名字：(.*?)，', content).group(1)
age = re.search('年龄：(\d+)', content).group(1)
return cls(name, age)

def calc_age_after_n_year(self, n):
age = People.add_two_string_num(self.age, n)
print(f'{n}年以后，我{age}岁')

content = '我的名字：青南，我的年龄：20，把它提取出来'
kingname = People.from_chinese_string(content)
kingname.introduce_myself()
kingname.calc_age_after_n_year(10)

``````import re

class People:
def __init__(self, name, age):
self.name = name
self.age = age

def introduce_myself(self):
print(f'大家好，我叫: {self.name}')

@staticmethod
a_int = int(a)
b_int = int(b)
return a_int + b_int

@classmethod
def from_chinese_string(cls, sentence):
name = re.search('名字：(.*?)，', content).group(1)
age = re.search('年龄：(\d+)', content).group(1)
return cls(name, age)

@classmethod
def from_english_string(cls, sentence):
name = re.search('name: (.*?),', content).group(1)
age = re.search('age: (\d+)', content).group(1)
return cls(name, age)

def calc_age_after_n_year(self, n):
age = People.add_two_string_num(self.age, n)
print(f'{n}年以后，我{age}岁')

content = 'my name: kinganme, my age: 15 please extract them'
kingname = People.from_english_string(content)
kingname.introduce_myself()
kingname.calc_age_after_n_year(10)

``````import datetime

dt0 = datetime.datetime(2021, 10, 9, 19, 10, 5)
now = datetime.datetime.now()
dt = datetime.datetime.fromtimestamp(1633691412)
dt2 = datetime.datetime.fromisoformat('2021-10-08 19:10:05')