classes and Instances :
Define class class name (parent class name): Where object is the ancestor class of all classes
The __int__ method (two underscore) indicates that the attribute is bound to a class, and the name, score, and so on are tied up as follows
Note that the first parameter of all methods in a class is self for the object itself, and it does not need to pass this argument when called
class Student (object): def __init__ (self, Name, score): = name = Score
View Code
restricted access : If you add two underscores to an instance's variable, then this variable is treated as a private variable, only internally accessible and externally accessible by means of a method.
class Student (object): def __init__ (self, Name, score): Self . __name = name self . __score = score def Print_score (self): Print( ' %s:%s ' % (self. __name, self. __score))
>>> bart = Student(‘Bart Simpson‘, 98)>>> bart.__nameTraceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: ‘Student‘ object has no attribute ‘__name‘
In Python, the variable name is similar, that is, starting with a double underscore, and ending with a double underscore, __xxx__ is a special variable, the special variable is directly accessible, not the private variable, so, can not be used __name__ , __score__ such a variable name.
cannot be accessed directly __name because the Python interpreter __name has changed the variable so that it _Student__name can still be accessed by _Student__name accessing the __name variable: But it is highly recommended that you not do this because different versions of the Python interpreter might __name Change to a different variable name.
>>> Bart = Student ('Bart Simpson', 98)>>> bart.get_name () 'Bart Simpson'>>> Bart. __name ' New Name ' # set the __name variable! >>> Bart. __name ' New Name '
View Code
The external code "succeeds" in setting the __name variable, but actually the variable __name and the variable inside the class are __name not a variable! The internal __name variables have been automatically changed by the Python interpreter _Student__name , and the external code bart adds a new __name variable. Try not to believe:
>>> bart.get_name() # get_name()内部返回self.__name‘Bart Simpson‘
继承多态类似与java
获取对象的信息
使用type()函数来判断对象的类型\
如果要获得一个对象的所有属性和方法,可以使用dir()函数,它返回一个包含字符串的list,比如,获得一个str对象的所有属性和方法:
>>> dir ('ABC')['__add__','__class__','__contains__','__delattr__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__getitem__','__getnewargs__','__gt__','__hash__','__init__','__iter__','__le__','__len__','__lt__','__mod__','__mul__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__rmod__','__rmul__','__setattr__','__sizeof__','__str__','__subclasshook__','Capitalize','Casefold','Center','Count','encode','EndsWith','Expandtabs','Find','format','Format_map','Index','isalnum','Isalpha','Isdecimal','IsDigit','Isidentifier','Islower','IsNumeric','isprintable','isspace','Istitle','Isupper','Join','Ljust','Lower','Lstrip','Maketrans','Partition','Replace','RFind','Rindex','Rjust','rpartition','Rsplit','Rstrip','Split','Splitlines','StartsWith','Strip','Swapcase','title','Translate','Upper','Zfill']
It is not enough to simply list properties and methods, mates getattr() , setattr() and hasattr() we can manipulate the state of an object directly:
>>> hasattr (obj, ' x ') # have attribute ' x '? true>>> obj.x9 >>> hasattr (obj, ' y ') # have attribute ' y '? false>>> setattr (obj, ' y ', 19" # set a property ' Y ' >>> hasattr (obj, # have attribute ' y '? true>>> getattr (obj, ' y ') # get property ' y ' 19>>> obj.y # get property ' Y ' Span class= "number" >19
实例属性和类属性
如下定义一个类属性,可以同过实例访问,也可以同类类名.属性名访问,但是如果类属性名和实例属性名相同的话,实例属性名会覆盖了类属性。
class Student(object):
name = ‘Student‘
Python sixth day object-oriented programming