The most important concept of object-oriented is class and instance (Instance), you must keep in mind that classes are abstract templates, such as student classes, while instances are specific "objects" created from classes, each with the same method, but their data may be different.
Still taking the student class as an example, in Python, the definition class is defined by the Class keyword:
Class Student (object): Pass
The class name is followed by the student, which is usually a word that begins with a capital letter, followed by (object), indicating which class inherits from, and the concept of inheritance is later, usually, if there is no suitable inheriting class, the object class is used, This is the class that all classes will eventually inherit.
By defining the student class, you can create an instance of student based on the student class, which is implemented by the class name + ():
>>> Bart = Student ()
>>> Bart
<__main__. Student object at 0x10a67a590>
>>> Student
<class ' __main__. Student ' >
As you can see, the variable Bart is pointing to a student object, followed by a memory address, 0x10a67a590 the address of each object, and student itself a class.
You are free to bind attributes to an instance variable, for example, to the instance Bart to bind a Name property:
>>> bart.name = ' Bart Simpson '
>>> bart.name
' Bart Simpson '
Because a class can act as a template, you can, when creating an instance, force a few attributes that we think must be bound to fill in. By defining a special __init__ method, when creating an instance, bind the attributes such as Name,score:
Class Student (object):
def __init__ (self, Name, score):
self.name = name
Self.score = Score
Note that the first argument to the __init__ method is always self, representing the created instance itself, so that within the __init__ method, you can bind the various properties to self, because self points to the instance itself created.
With the __init__ method, when you create an instance, you cannot pass in an empty argument, you must pass in an argument that matches the __init__ method, but self does not need to pass, and the Python interpreter will pass the instance variable in itself:
>>> bart = Student (' Bart Simpson ', the)
>>> bart.name
' Bart Simpson '
>>> Bart.score
59
The function defined in a class is only a little different than a normal function, that is, the first argument is always the instance variable self and, when invoked, does not pass the argument. In addition, there is no difference between a class method and a normal function, so you can still use default parameters, variable parameters, and keyword parameters.
Data encapsulation
An important feature of object-oriented programming is data encapsulation. In the student class above, each instance has its own name and score data. We can access this data through functions, such as printing a student's score:
>>> def print_score (std):
... Print '%s:%s '% (Std.name, Std.score)
...
>>> Print_score (Bart)
Bart simpson:59
However, since the student instance itself has this data, to access the data, there is no need to access the function from the outside, you can directly within the Student class definition of access to the data function, so that the "data" to the encapsulation. The functions that encapsulate the data are associated with the student class itself, which we call the class 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)
To define a method, except the first argument is self, the other is the same as a normal function. To invoke a method, simply call it directly on the instance variable, except that self does not pass, and the other parameters are passed in normally:
>>> Bart.print_score ()
Bart simpson:59
Thus, when we look at the student class from the outside, we just need to know that the create instance needs to give name and score, and how to print, is defined inside the student class, the data and logic is "encapsulated", the call is easy, but it doesn't have to know the details of the internal implementation.
Another benefit of encapsulation is that you can add new methods to the student class, such as Get_grade:
Class Student (object): ...
def get_grade (self):
if Self.score >=: Return
' A '
elif self.score >=: Return
' B '
else:< C8/>return ' C '
Similarly, the Get_grade method can be invoked directly on an instance variable without needing to know the internal implementation details:
>>> bart.get_grade ()
' C '
Summary
A class is a template that creates an instance, and an instance is a concrete object, and the data owned by each instance is independent of each other.
A method is a function that is bound to an instance, and a method can directly access the data of an instance, unlike a normal function.
By invoking the method on the instance, we directly manipulate the data inside the object, but do not need to know the implementation details within the method.
Unlike static languages, Python allows you to bind any data to an instance variable, that is, for two instance variables, although they are different instances of the same class, the name of the variable may be different:
>>> bart = Student (' Bart Simpson ', the)
>>> Lisa = Student (' Lisa Simpson ', ")
>>> Bart . Age = 8
>>> bart.age
8
>>> lisa.age
traceback (most recent called last):
File " <stdin> ", line 1, in <module>
attributeerror: ' Student ' object has no attribute ' age '