class A:
@staticmethod
def mystatic():
# 静态方法无法调用普通方法
return '静态方法'
@staticmethod
def mymystatic2():
return A.mystatic() # 需要使用类名
def me(self):
# 普通方法可以调用静态方法
# 小范围可以调用大范围
return self.mystatic()
@classmethod
def myclass(cls): # cls 表示当前类(self 表示当前对象)
return '类方法'
@classmethod
def myclass1(cls):
return A.myclass() # 类方法可以与静态方法相互调用
print(A.mystatic())
print(A.mymystatic2())
print(A.myclass())
print(A.myclass1())
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class Father:
a = 1
_b = 2
__c = 3
d = None
e = None
def __init__(self, d_, e_):
self.d, self.e = d_, e_
def me(self):
pass
@staticmethod
def mystatic():
return '456'
class Son(Father):
one = None
two = None
def __init__(self, one_, two_, d_, e_):
# 调用父类的构造函数
# 父类的方法被重写,但又需要调用父类的方法,可以使用super()函数
super().__init__(d_, e_)
self.one, self.two = one_, two_
def me(self):
return 'hh'
obj = Son(1, 2, 3, 4)
print(obj.a)
print(obj._b)
print(obj._Father__c)
print(obj.me())