1.可存在同名对象
class A:
def __init__(self):
print('this is class.')
def A():
print('this is method.')
a = A()
# this is method.
由于python是边编译边执行的语言,所以只会使用最近的定义。
def A():
print('this is method.')
class A:
def __init__(self):
print('this is class.')
a = A()
# this is class.
2.不存在重载
构造函数:类中只有最后定义的构造函数才有效
class A:
def __init__(self):
pass
def __init__(self, name):
pass
a = A()
# TypeError: A.__init__() missing 1 required positional argument: 'name'
方法:
def test():
print('None')
def test(name):
print(name)
test()
# TypeError: test() missing 1 required positional argument: 'name'
3.类变量
类中定义的变量既可以作为静态变量使用,也可以作为实例变量使用,两者不干涉,通常不作为实例变量使用。
class A:
Name = '66'
def __init__(self, name):
self.Name = name
a = A('77')
print(a.Name)
print(A.Name)
# 77
# 66
标签:__,name,python,差异,self,C#,print,class,def From: https://www.cnblogs.com/bridgew/p/16792261.html