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

How Python is Object-Oriented Programming

編輯:Python

how to face“對象”

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,你說對不對.

The program design of object-oriented programming

1. Why do you need programming?

在建築行業,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.

2. 是否存在“最好的”設計?

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也不例外.

3. Why Object Oriented Programming?

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:

  1. Using objects as the basic unit of a program,而不是算法
  2. Every object is an instance of some class
  3. Classes and classes can be linked together by inheritance

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:

  1. 繼承.通俗理解,“what a son inherits from his father”.If you create a new class based on a class,The properties and methods of the parent class will be directly inherited,從而減少重復代碼的編寫.Provide inheritance information(財產)we call the parent class、或者基類、超類;We call the subclass that gets the inheritance information、or derived class or derived class.
  2. 多態.字面意思,“多種形態”,接口的不同實現方式即為多態.通過多態,Subclasses can extend the capabilities of the superclass,Or override the operation of the parent class.
  3. 封裝.My understanding of the packaging,As a black box with certain functions,隱藏一切可以隱藏的實現細節,Then provide a simple programming interface to the outside world.

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中的一切皆對象

在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對象的三個特性

Python All objects of the have three properties:身份(id)類型(type)值(value)

  • 身份(id):每個對象都有一個唯一的身份標識自己,The identity of any object can use built-in functions id() 來得到,You can simply think of this value as the memory address of the object.
a = 3b = aid(a)id(b)id(test_list)
  • 類型(type):對象的類型決定了對象可以保存什麼類型的值,有哪些屬性和方法,可以進行哪些操作,Follow the rules for what.可以使用內建函數 type() 來查看對象的類型.
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'>
  • 值(value):the data represented by the object
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:

  1. Python學習手冊(第5版)
  2. 面向對象分析與設計(第3版)
  3. Python Cookbook(第3版)

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