Follow us hard-working programmers every day(媛)they shouted:“要面向對象編程”,Are you going to create a new object for me?.你看看,no object,我怎麼面向對象編程嘛.網上關於Java和**C++**There are already many blog posts about the introduction to object-oriented programming.,Then why am I still writing??因為,人生苦短,I am just learningPython的…
Just today when I was studyingPython的OOP時,Just want to ridicule the object-oriented programming content is still a lot,Suddenly, there is a mess——美女“編程”老太婆.???,It's not easy to have a beautiful programming,how to become an old woman.
以上純屬瞎編,話說回來,能有個“對象”面向對象編程,It's good to be a bad old man and an old woman together,你說對不對.
在建築行業,Architects generally do not want to100Add a basement to the building,Because it is undoubtedly too expensive to do so,may even fail.(Just invigilated the first-level architect exam in the first half of the year,3.5Hours of site design and drawing questions are difficult to look at.)
令人吃驚的是,in our software development industry,When users propose similar changes,don't think too much.相反,The boss or product manager might say it's a simple programming problem.(哈哈,Here is unintentionally slandering programmers and product managers…)
但是,Software is inherently complex,隨著項目的迭代,Complexity is often beyond the reach of human intelligence.
in every engineering practice,design is a訓練有素的方法.whether functional programming、泛型編程、並發編程、面向過程、基於對象、面向對象etc. programming,We use a design approach to create a solution to a problem,so as to provide a way for time requirements.
那麼,是否存在**“最好的”**設計方法?
There may be no absolute answer to this question,但《沒有“銀彈”》告訴我們:No mere technical or managerial advance can fool-proof software engineering from requirements to the realization of a complex system.所以,Object orientation is not the last solution to all problems in software development“銀彈”,如今很多High-level programming languages offer multiple programming design paradigms,Python也不例外.
Classes are the primary tool for object-oriented programming,The way we use it to define new kinds of,It reflects real objects in the program domain.And what is object-oriented programming??
面向對象編程是一種實現的方法,在這種方法中,Programs are organized into groups of objects that collaborate with each other,每個對象代表某個類的一個實例,而類則屬於一個通過繼承關系形成的層次結構.
Let's take a look at the three key points in the concept:
Satisfying these three points is called an object-oriented program.Let's simulate it with a simple example
接著,Let's first understand the three core concepts of object orientation:
Object-oriented programming provides an efficient way of programming,利用這種方式,We minimize code redundancy.因此,We can customize the existing code to write new programs than in its place.
在Python中,OOPis not necessary at all,There is also no need to use classes at the beginner level,Using the function structure can also write a lot of useful scripts,Do a lot of fun programming.但是Python OOP也非常有意思,I don't believe you can study with me.
Java雖然也是面向對象編程的語言,但是血統沒有Python純正.比如Java的八種基本數據類型之一int,在持久化的時候,needs to be packagedInteger類對象.
與Java相比,Python的面向對象更徹底.學過Python的朋友可能知道,在Python中,把我們所有能看到的都變成對象——數字、字符串、元組、列表、字典、函數、方法、類、模塊、包括你的代碼.
怎麼理解這個過程呢?Python Everything in can be assigned to a variable or passed as a parameter to a function,我們來看一下代碼:
a = 3b = aprint(a) # 打印:3print(b) # 打印:3def all_is_object(): print("Learing Python OOP") all_is_well = all_is_objectall_is_object() # 打印:Learing Python OOPall_is_well() # 打印:Learing Python OOPclass Person(): def __init__(self, name): print("姓名:", name) A = Personxiaoyu = A("小宇") # 姓名: 小宇
Python All objects of the have three properties:身份(id)、類型(type)、值(value)
a = 3b = aid(a)id(b)id(test_list)
a = 3b = aprint(type(a)) # <class 'int'>print(type(int)) # <class 'type'>test_list = [1, 2, 3, 4, 5]print(type(test_list)) # <class 'list'>print(type(list)) # <class 'type'>test_tuple = (1, 2, 3, 4, 5)print(type(test_tuple)) # <class 'tuple'>print(type(tuple)) # <class 'type'>test_str = "I love python"print(type(test_str)) # <class 'str'>print(type(str)) # <class 'type'>
print(a) # 3print(test_list) # [1, 2, 3, 4, 5]print(test_str) # I love python
“身份”、"類型"和"值"Assigned when all objects are created.只要對象存在,The three characteristics has been around.
事實上,Software Frameworks We Learn(framework)is a collection of parent classes,A framework implements common programming tasks into classes,All we need to do is by writing our own subclass,Combining and customizing debugged code.此外,將常見的OOPStructural classification,is our design pattern(design pattern),to help solve design problems.
These software frameworks may provide some database interfaces、測試協議、GUI工具包等
This chapter provides a conceptual introduction to classes and object-oriented programming,Let's have a general overviewOOPideal country landscape.
Series of articles reference books:
- Python學習手冊(第5版)
- 面向對象分析與設計(第3版)
- Python Cookbook(第3版)