Classes and instances:
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
): ...
class
followed by the class name, that is, the Student
class name is usually the beginning of the word, followed by, the class is (object)
to indicate which class inherits from, the concept of inheritance we will say later, usually, if there is no suitable inheritance class, you use the object
class, this is all classes will eventually inherit the class.
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:
class Student (): ... >>> Bart = Student ()>>> Bart0x7fe28699a098>>>> bart1 = Student ()>>> bart10x7fe28699a128
As you can see, the variable is bart
pointing to a student object, 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.name='lt'>>> bart.age=>>> bart.name 'lt'>>> bart.age
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 ... =
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 () Traceback (most recent): File"<stdin>", line1,inch<module>TypeError: __init__ () takes exactly3Arguments (1given)>>>#传入参数就会正常显示...>>> Bart = Student ('LT', -)>>>Bart.name'LT'>>>Bart.score ->>>
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. Besides, there is no difference between a class's method and a normal function, so you can still use the default, variable, and 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_info (std): ... ' %s:%s ' %>>> print_info (Bart) LT:
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.
1>>>classStudent (Object):2 ... def __init__ (self,name,score,age):3... Self.name =name4... Self.age = Age5... Self.score =score6 ... def print_info (self):7.. print'%s:%s%s'%(self.name,self,score,self.age)8 ... 9>>> Bart = Student ('LT', -, -)Ten>>>Bart.name One 'LT' A>>>Bart.score - - ->>>Bart.age the - ->>> #打印属性
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:
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
:
1 classStudent (Object):2 ...3 4 def get_grade (self):5 ifSelf.score >= -:6 return 'A'7Elif Self.score >= -:8 return 'B'9 Else:Ten return 'C'
---Classes and instances of Python