程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python_ day13

編輯:Python

preview

object-oriented programming ——OOP, Is an object-oriented programming idea
Process oriented programming ———POP, It is a process centered programming idea

problem : Put the elephant in the refrigerator , A few steps ?
The answer to the sketch : The three step
First step : Open the refrigerator door
The second step : Put the elephant in
The third step : Close the refrigerator door

Process oriented programming

The answer given above is process oriented , Analyze the steps to solve the problem , And then step by step

object-oriented programming

By analyzing the abstract model needed in the problem , Then the more necessary functions are used to create objects , Finally, the model object completes the program

First, object-oriented should solve this problem , You need to build an abstract model first , such as : Open and close the refrigerator , This is the function of a refrigerator , The elephant went in , That's what elephants do . At this point, two abstract models are abstracted , Refrigerator and elephant .
The refrigerator has the function of opening and closing , Elephants have the function of walking .
So that's the analysis , It's object-oriented thinking , If it is completed specifically , Is to create two objects, the refrigerator and the elephant , Finally complete the program , Refrigerator objects — Open door , Elephant objects — Walk into the refrigerator , Refrigerator objects —— close .

summary

Process oriented

The core of process oriented is process , Process means the steps to solve the problem .

Advantages and disadvantages

  • advantage : Process the responsible problems , And then realize the simplification
  • shortcoming : Poor expansibility ( to update 、 maintain 、 iteration )
    summary : To complete some simple procedures , Process oriented can be used to solve , But if there are complex programs or tasks , And it needs constant iteration and maintenance , Then the object-oriented programming idea must be preferred .

object-oriented

The core of object-oriented is object , It is a complex of features and functions

Advantages and disadvantages

  • advantage : High scalability
  • shortcoming : Programming complexity is higher than process oriented , It refers to the performance of a computer when executing object-oriented programs

Some names in object-oriented ( The term )

class : Class is an abstract concept of object
object ( example ): An object is an instance created by a class
The relationship between a class and an instance is Relationship between film and casting

  • Classes are summarized from objects ,, The process of summarizing is called abstraction
  • Objects are implemented by classes , This process is called instantiation
    If there is still some ambiguity , Please think about it. :
    Is fruit an object or a class ?
    Is the car an object or a class ?
    Is the mobile phone an object or a class ?

OOP— Basic implementation

If you need to instance an object , You need to abstract a class first

For example, you need to create a car object
First, you need to abstract a car class , Automobile is equivalent to a design drawing .
From this design drawing to create ( example ) The real car is an object .

How to create a class , adopt class Keyword to define a class
Example :

# Define a car class 
class car():
pass

Class needs to declare the declaration content ?

''' Writing specification of class name , It is suggested to use hump nomenclature Hump :MyCar AoDi Little hump :myCar aoDi A class consists of two concepts: feature and function : A feature is a description : Color : black , brand : audi , displacement :2.4 Function is a capability : cargo , a ride A feature is a variable in programming , In a class, it is called attribute Function is a function in programming , In a class, it is called Method The attributes in the class are generally defined in the front , Methods are defined later '''
# Define a car class 
class Car():
# attribute ==> features ==> Variable 
color = ' white '
brand = ' audi '
pailiang = 2.4
# Method ==> function ==> function 
def lahuo(self):
print(' Audi can pull goods ')
def doufeng(self):
print(' Audi can go for a ride ')
def pianmeimei(self):
print(' Driving an Audi can cheat my sister ')
# How to use this class ?
# Instance an object through a class 
aodiobj = Car()
# print(aodiobj,type(aodiobj))
# <__main__.Car object at 0x0000019516C82FD0> <class '__main__.Car'>
aodiobj.lahuo()
print(aodiobj.brand)

Operation of object members


class Car():
# attribute ==> features ==> Variable 
color = ' white '
brand = ' audi '
pailiang = 2.4
# Method ==> function ==> function 
def lahuo(self):
print(' Audi can pull goods ')
def doufeng(self):
print(' Audi can go for a ride ')
def pianmeimei(self):
print(' Driving an Audi can cheat my sister ')
a = Car()
b = Car()
# print(a)
# print(b)
'''' A class can instantiate multiple objects The above a And variables are objects , It's all through car The object instantiated by this class , however a and b Two objects , The same thing is that it is instantiated from the same class <__main__.Car object at 0x000001AE5E3D2FD0> <__main__.Car object at 0x000001AE5E3D2FA0> '''
# Operation of object members 
# 1. Outside the class , Use objects to manipulate members 
# Access member properties : First access the object's own color attribute , If not, no , Just access the properties of the class of this object 
# res = a.color # Accessing properties in a class through an object 
# Modify the property value of an object : In fact, it is equivalent to creating a a The object is its own calor attribute 
a.color = ' black ' # Modify the properties of an object 
# Add properties of the object : To current a Object creates its own unique properties 
a.name = 'W11111'
# here b The color of the object is declared ?
print(b.color)
# Delete attribute , Only delete the object's own properties , Including objects added and modified 
# res = a.lahuo() # Glory object access class method 
# print(a.color)
# print(a.name)
del a.brand
del a.name
''' # Think about a problem : brand Property cannot be deleted , however name Property can be deleted , Why? ? When deleting an object's properties , You can only delete the properties of the current object , In the above example brand Attributes are not a Object's own properties , It's an attribute Car This class . So you can't delete and name This attribute , It's for you alone a Object added properties , Because you can delete '''
# 2. Outside the class , Methods for manipulating objects 
# How to access objects : In fact, if the object has its own independent method , Then the method of the class that will access this object 
# Methods of modifying objects : Redefine the methods of this object 
def func():
print(' It's a new way ')
a.lahuo = func # Assign a function to a member 
# a.lahuo() # To call this method 
# Add new methods : Create a new method for the object itself 
a.fun2 =func
# a.fun2
# Delete method : You can delete the current object's own methods 
del a.fun2 # It can be deleted 
del a.lahuo # It can be deleted 
del a.doufeng # It can't be deleted 
a.fun2
''' summary : A class defines class member properties and member methods , Then the object instantiated through this class , Do you also have these methods and properties ? actually , When creating objects , The properties and methods in the class are not copied to the object , Instead, it refers to the methods of the parent class in the object So when accessing the properties of an object , Will first find the object's own properties , If not, look for the properties and methods of this class Class method . After an object is created by a class , Is a separate object , It will reference the properties and methods in the parent class If after the object is created , Properties or methods given to objects , Modify or add , At this point, you create your own properties and methods for this object So when deleting , Only members whose objects have been modified or added can be deleted '''

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved