1 Object-Oriented programming
Object-Oriented Programming:
Object-Oriented programming -- Object orientedprogramming
OOP takes objects as the basic unit of a program, and an object contains functions for data and manipulating data.
process-oriented procedures The design treats the computer program as a series of command sets, that is, the sequence of a set of functions executes. In order to simplify the program design, the process will continue to cut the function into sub-function, that is, the large function by cutting into small block function to reduce the complexity of the system.
Object-oriented programming treats a computer program as a set of objects , and each object can receive messages from other objects and process them, and the execution of a computer program is a series of messages passing between objects.
all data types can be treated as objects, as well as custom objects, and custom object data types are object-oriented classes ( class ) Concept of .
When it comes to classes, you naturally think of instances, classes are abstractions of data types, and instances are concrete (personal understanding) of classes.
>>> std1 = {' name ': ' Michael ', ' Score ': 98}
>>> std2 = {' name ': ' Bob ', ' Score ': 81}
>>>
>>> def print_score (STD):-- print student's name and score
... print ('%s:%s '% (std[' name '], std[' score '))
...
>>> Print_score (STD1)
Michael:98
>>> Print_score (STD2)
bob:81
according to the object-oriented program design idea, this student should be regarded as object, abstract into class;
>>> classstudent (object):-- Define class
... def __init__ (self, name, score):
... self.name = name
... self.score = Score
... def print_score (self):-- Define method
... print ('%s:%s '% (Self.name, Self.score))
...
>>> bart = Student (' Bart Simpson ', 59)
>>> Lisa = Student (' Lisa Simpson ', 87)
>>> Bart.print_score () -- method of invoking the object, a concrete object -- instance
Bart simpson:59
>>> Lisa.print_score ()
Lisa simpson:87
the object-oriented design idea is to abstract out Class, create Instancebased on class.
1.1 Classes and Instances1.1.1 Classes and Instances
>>> class Student (object):--class defines the class's keyword,Student class name (beginning with uppercase),(object) which class inherits from
... pass
...
>>> Student
<class ' __main__. Student ' >
>>> Bart = Student () -- Create an instance from a class
>>> Bart
<__main__. Student Object at0x2b7d2fd759e8>
if there is no appropriate inheritance class, use the Object class , which is the class that all classes will eventually inherit .
you are free to bind attributes to an instance variable, for example, Bart binds a name property:
>>> bart.name = ' Bart Simpson '
>>> Bart.name
' Bart Simpson '
because the class acts as a template, when you create a class, you can force the properties that must be bound to be filled in- through the init method
>>> class Student (object):
... def __init__ (self, Name,score):--init method, The first parameter of Init is always self
... self.name = name
... self.score = Score
...
within the __init__ method, it is possible to bind various properties to self , because The individual points to the created instance itself .
The Init method exists , and when you create an instance, you must fill in the specified parameters, but self does not need to be passed in, andPython fills in.
>>> Bart = Student (' Daidai ')-- must fill in the specified parameters, mandatory
>>> Bart.name--name,score becomes an object's property
' Daidai '
>>> Bart.score
99
>>> bart.self-- can't specify self so
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' Student ' object had Noattribute ' self '
>>> bart.grade = ' A '-- new attribute grade
>>> Bart.grade
A
There is no difference between a class method and a normal function, so you can still use default parameters, variable parameters, keyword arguments, and named keyword Parameters .
1.1.2 Data Encapsulation
>>> class Student (object):
... def __init__ (self, Name, score):
... self.name = name
... self.score = Score
... def print_score (self):--Print_score method
... print ('%s:%s '% (Self.name, Self.score))
... def get_grade (self):--get_grade Method
... if Self.score >= 90:
... print (' A ')
... elif self.score >= 70:
... print (' B ')
.. else:
.. print (' C ')
...
define a method, the first argument is self, and the other is consistent with the function.
>>> Daidai = Student (' Daidai ', 99)
>>> Daidai.print_score ()-- call on instance variable
daidai:99
>>> Daidai.get_grade ()
A
This is the definition of the implementation class and the definition of its method, which implements the encapsulation of the class.
A method is a function that is bound to an instance, and unlike a normal function, a method can directly access the data of an instance.
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 variable names you have may be different.
>>> Daidai.age = 8
>>> Daidai.age
8
>>> Xiongxiong.age-- This instance variable is not bound
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' Student ' object had Noattribute ' age '
This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1826203
Python Object-oriented programming-classes and instances