Skip to content

Instantly share code, notes, and snippets.

@96998
Created December 14, 2018 13:52
Show Gist options
  • Select an option

  • Save 96998/b4c11f7b0900445c2a1b30db9bd38168 to your computer and use it in GitHub Desktop.

Select an option

Save 96998/b4c11f7b0900445c2a1b30db9bd38168 to your computer and use it in GitHub Desktop.
Python base
class Myfirst(object):
def __init__(self,name):
self.name=name
def __init__(self):
print("no arguments")
def f(self):
print(self.name)
if __name__=='__main__':
# c1 = Myfirst("CJJ") #this will be wrong
c2 = Myfirst()
#在python中一个类可以定义多个构造方法,但实例化类时只能实例化最后的构造方法
class MyClass2(object):
def __init__(self,name):
self.__name=name
def set_name(self,name):
self.__name = name
def get_name(self):
return self.__name
def __str__(self):
return self.__name
__repr__=__str__
if __name__=='__main__':
c1 = MyClass2('CJJ')
print(c1.get_name())
print(c1)
@96998
Copy link
Author

96998 commented Dec 14, 2018

My first try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment