table of Contents
Process-oriented programming
Facing step / process code code.
Features: step by step, a function output is the input of the next function.
Advantages: increased independence of the code; the process clear.
Disadvantages: a mistake, let the program directly bounced off.
Object-Oriented Programming
Against the object (object) code code.
Features: interaction between objects and objects.
Pros: change an object does not affect other objects.
Cons: very complex.
Classes and Objects
Class (template / category): divided objects have the same attributes and skills of objects belonging to the same class (own control).
Object: a series of characteristics (attributes) and skills (Method) combination.
After the first, class of objects.
class
Class name generally used hump body.
Stage class definition: code execution.
Function definition phase: detecting syntax, the code is not performed.
class Student(): # 可加括号也可以不加
school = 'old boy' # 定义变量(给类添加属性)
def choose_course(self): # 定义在类内部的函数一般称为方法(给类增加技能)
pass
Objects
Definition object instance is also referred to as object.
stu1 = Student() # stu1就是一个对象
stu1.choose_course() # 对象使用类中的方法时,不需要加参数,因为实例化对象调用该方法时,python会自动将该实例化对象传给self
Unique features custom objects
- Nature object similar to a class, but also a name space, but the name of the object space to store the object unique name, and class objects are stored in a common name. Therefore, we can directly target custom name alone.
class phone: # 定义一个类
types = 'mobile phone'
def __init__(self,brand,model_number,color,rom): # __init__可以直接初始化对象的属性,使对象可以添加独有的特征。
print(self)
self.brand = brand
self.model_number = model_number
self.color = color
self.rom = rom
def phone_comment(self): # 对象都可以使用这个自定义函数
print('一般货色')
apple = phone('apple','iphone xR','black','128g')
print(apple.types)
print(apple.brand)
print(apple.model_number)
print(apple.color)
print(apple.rom)
apple.phone_comment()
< Main .phone Object AT 0x000001DE78CB8288>
Mobile Phone
Apple
iPhone xR
Black
128g
ships goods
Classes and data types
Everything in python objects are all data types.
The role of the object:
- Reference (x = 10; y = x)
- As the container-earth element (lis = [x, func, Student]
- As a function parameter def func (x, func, Student)
- As a function return values return x, func, Student