Object-oriented concepts are classes (class) and instances (Instance), and it is important to keep in mind that classes are abstract templates, such as student classes, and instances are specific "objects" that are created from classes, each with the same method, but the data may be different for each object.
Still taking the student class as an example, in Python, the definition class is by class
keyword:
class Student (object): Pass
class
followed by the class name, that is, the Student
class name is usually the beginning of the word , followed by, that is, the class (object)
is inherited from the class, the concept of inheritance we will say later, usually, if there is no appropriate inheritance class, the use of object
classes, This is the class that all classes will eventually inherit.
Once you have defined the Student
class, you can Student
create Student
an instance from the class by using the class name + () to create the instance:
>>> Bart = Student ()>>> Bart<__main__. Student object at 0x10a67a590>>>> Student<class'__main__. Student'>
As you can see, the variable is bart
pointing to an Student
instance, followed by a 0x10a67a590
memory address, each object has a different address, and Student
itself is a class.
You are free to bind attributes to an instance variable, for example, to bart
bind an instance to an name
attribute:
' Bart Simpson '>>> bart.name'Bart Simpson'
Because a class can act as a template, you can force a number of attributes that we think must be bound to be filled in when creating an instance. By defining a special __init__
method, when you create an instance, you bind the name
score
attributes, such as:
class Student (object): def __init__ (self, Name, score): = name = Score
Notice that the __init__
first parameter of the method is always the one that self
represents the created instance itself, so within the __init__
method, you can bind various properties to it self
, because self
it points to the created instance itself.
With the __init__
method, when you create an instance, you cannot pass in an empty argument, you must pass in a __init__
parameter that matches the method, but it self
does not need to be passed, and the Python interpreter will pass the instance variable in itself:
>>> Bart = Student ('Bart Simpson', ')>>> bart.name 'Bart Simpson'>>> bart.score59
A function defined in a class is only a little different than a normal function, that is, the first argument is always an instance variable, self
and when called, it is not passed. In addition, there is no difference between a class method and a normal function, so you can still use default parameters, variable arguments, keyword arguments, and named keyword parameters.
Data encapsulation
An important feature of object-oriented programming is data encapsulation. In the above Student
class, each instance has its own name
and score
this 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) ... >>>59
However, since the Student
instance itself has this data, to access the data, there is no need to go from outside the function to access, you can directly within the Student
class to define the function to access the data, so that the "data" to encapsulate it. The functions of these encapsulated data are Student
associated with the class itself, and we call it the method of the class:
class Student (object): def __init__ (self, Name, score): = name = score def Print_score (self): Print( ' %s:%s ' % (Self.name, Self.score))
To define a method, except for the first argument self
, the other is the same as the normal function. To invoke a method, just call it directly on the instance variable, except that self
it does not pass, and other parameters are passed in normally:
>>>59
In this way, we look at the class from the outside, just need to Student
know that the creation of the instance needs to give name
and score
, and how to print, are Student
defined inside the class, the data and logic is "encapsulated", the call is easy, but do not know the details of the internal implementation.
Another benefit of encapsulation is the ability to Student
add new methods to the class, such as get_grade
:
class Student (object): ... def Get_grade (self): if self.score >= 90: return " a " elif self.score >= 60: return " b " else : return " c "
Similarly, the get_grade
method can be called directly on an instance variable without needing to know the internal implementation details:
>>> Bart.get_grade ()'C'
The class is the template that creates the instance, and the instance is a concrete object, each instance has data that is independent of each other and does not affect each other.
method is the function that binds to the instance, and the normal function is different, the method can directly access the data of the instance;
By invoking the method on the instance, we manipulate the data inside the object directly, but we do not need to know the implementation details inside the method.
Classes (Class) and instances in Python (instance)