Created
December 14, 2018 13:52
-
-
Save 96998/b4c11f7b0900445c2a1b30db9bd38168 to your computer and use it in GitHub Desktop.
Python base
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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中一个类可以定义多个构造方法,但实例化类时只能实例化最后的构造方法 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My first try