Object-Oriented Programming
Examples of classes and
Class is abstract template
class Student (Object): Passclass
is immediately followed by a class name, ieStudent
, the class name is usually the beginning of the word capitalized, followed by(object)
,
represent the class which is inherited from the class down, the concept of inheritance behind us repeat that,
generally, If no suitable inherited classes, useobject
class, which is all classes eventually inherited class
Create an instance of the class is done by name + ()
Bart = >>> Student () >>> bart < __main__ .Student Object AT 0x10a67a590> # indicates the point is an example of bart Student's behind 0x10a67a590 is one example of a memory address for each memory address is not the same > >> Student < class ' in __main __. Student ' >
Free to give an instance variable binding properties such as binding a name to an instance bart property
>>> bart.name = 'Bart Simpson' >>> bart.name 'Bart Simpson'
We believe that some of the property must be bound mandatory to fill in. By defining a special __init__
method
class Student (Object) DEF __init__ (Self, name, Score): self.name = name self.score = Score
Note: special method "__init__" There are two underscores before and after! !
Noted that the __init__
first argument is always self
, in itself represented by an instance is created, therefore, the __init__
internal methods, we can put all kinds of property is bound to self
, because self
it points to create an instance of itself.
With the __init__
method, create an instance in time, you can not pass a null parameter, you must pass the __init__
parameter matching method, but self
do not need to pass, Python interpreter himself will pass in an instance variable:
Data encapsulation
class Student (Object): DEF the __init__ (Self, name, Score): the self.name = name self.score = Score DEF print_score (Self): Print ( ' % S:% S ' % (the self.name, self.score )) to define a method, in addition to the first parameter is outside the self, and other normal functions. To call a method, you only need to call directly on the instance variables, in addition to self do not pass, the other normal parameters passed in:
restriction of visit
Within the class, and can have properties and methods provide the outer code can be cold instance variables inside hey method hides the complexity of the operation of the internal logic
However, the definition of class Student front view, external code is free to modify one example of a name, score attribute
If you want the internal attributes are not externally accessible, you can put before the name of the property plus two underscores __
, in Python, the instance variable names if the __
beginning, it becomes a private variable (Private) , only internal access to, outside can not access, so we altered the Student category:
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))
Note that, in Python, a variable name is similar __xxx__
, and it begins with a double underline, double underline and to the end, is a special variable, special variable that can be accessed directly , not private variables, therefore, can not be used __name__
, __score__
so variable name.