面向对象编程的基础

本文探讨了面向对象编程(OOP)的概念及其在Python编程语言中的实现。OOP通过类、对象、继承、多态性等核心特性,使得编程更贴近现实世界,提高了代码的复用性和模块化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ever since the creation of programming languages, computer scientists have made their efforts to develop several ways to program a computer. Eventually, as the more and more developed programming languages came into existence, the way people used to write programs also changed. The methodology, algorithms, conceptualization, they went on evolving over years. These days, Object Oriented Programming is a popular methodology of programming any application.

自从创建编程语言以来,计算机科学家一直在努力开发几种对计算机编程的方法。 最终,随着越来越多的编程语言的出现,人们用来编写程序的方式也发生了变化。 方法论,算法,概念化,它们持续发展了多年。 如今, 面向对象编程是一种对任何应用程序进行编程的流行方法。

A particular programming language has to be designed in a very specific way so that a particular methodology can be followed. As far as Python is concerned, it is an Object Oriented Programming language. In this section, we will see what are the features in Python that supports or enables it to become an Object-oriented programming language. First of all, let's try to understand What is Object Oriented Programming? (or OOP) and it's concepts.

必须以非常特定的方式设计特定的编程语言,以便可以遵循特定的方法。 就Python而言,它是一种面向对象的编程语言。 在本节中,我们将看到Python支持或使其成为面向对象编程语言的哪些功能。 首先,让我们尝试了解什么是面向对象编程? (或OOP)及其概念。

什么是面向对象编程? (What is Object Oriented Programming?)

Object Oriented programming is a programming style that is associated with the concept of Class, Objects and various other concepts revolving around these two, like Inheritance, Polymorphism, Abstraction, Encapsulation etc.

面向对象编程是一种与类,对象以及围绕这两个主题的各种其他概念相关的编程风格,例如继承,多态性,抽象,封装等。

OOP is designed in such a way that one should focus on an object while programming and not the procedure. An object can be anything that we see around us. It can be a human (that has some properties like - name, address, DOB and so on), a chair (portrayed by size, material, cost etc), a school (depicted by place, student strength, results) etc.

OOP的设计方式是,在编程时应该专注于一个对象,而不是过程。 一个对象可以是我们在周围看到的任何东西。 它可以是人(具有一些属性,例如名称,地址,DOB等),椅子(按大小,材料,成本等描述),学校(按地点,学生实力,成绩显示)等。

Object oriented programming brings programming close to real life, as we are always dealing with an object, performing operations on it, using it's methods and variables etc.

面向对象的编程使编程接近现实生活,因为我们一直在处理对象,对其执行操作,使用其方法和变量等。

OOP的概念 (Concepts of OOP)

Basic OOPS concepts

In the video below, we have explained the basic concepts of Object Oriented Programming with help of a very easy to understand example. If you want to skip the video, everything is covered below as well.

在下面的视频中,我们通过一个非常易于理解的示例解释了面向对象编程的基本概念。 如果您想跳过视频,下面还将介绍所有内容。

演示地址

Let us try to understand a little about all these, through a simple example. Human Beings are living forms, broadly categorized into two types, Male and Female. Right? Its true. Every Human being(Male or Female) has two legs, two hands, two eyes, one nose, one heart etc. There are body parts that are common for Male and Female, but then there are some specific body parts, present in a Male which are not present in a Female, and some body parts present in Female but not in Males.

让我们尝试通过一个简单的示例来稍微了解一下所有这些内容。 人类是生命形式,大致分为男性和女性两种类型。 对? 这是真的。 每个人(男性或女性)都有两条腿,两只手,两只眼睛,一只鼻子,一颗心脏等。男性和女性都有一些共同的身体部位,但男性有一些特定的身体部位雌性中不存在,而雌性中有一些身体部位,雄性中不存在。

All Human Beings walk, eat, see, talk, hear etc. Now again, both Male and Female, performs some common functions, but there are some specifics to both, which is not valid for the other. For example : A Female can give birth, while a Male cannot, so this is only for the Female.

所有人类都走路,吃饭,看,说话,听等。现在,男性和女性都执行一些共同的功能,但是两者都有一些特定的规定,对另一个无效。 例如:女性可以生子,而男性则不能,所以这仅适用于女性。

Human Anatomy is interesting, isn't it? But let's see how all this is related to Python and OOPS. Here we will try to explain all the OOPS concepts through this example and in the next tutorial, we will have the technical definitons for all this.

人体解剖学很有趣,不是吗? 但是,让我们看看所有这些与Python和OOPS有何关系。 在这里,我们将尝试通过该示例解释所有OOPS概念,在下一个教程中,我们将对所有这些进行技术定义。

(Class)

Here we can take Human Being as a class. A class is a blueprint for any functional entity which defines its properties and its functions. Like Human Being, having body parts, and performing various actions.

在这里,我们可以将人类作为一个类。 类是定义其属性和功能的任何功能实体的蓝图。 像人类一样,具有身体部位,并执行各种动作。

遗产 (Inheritance)

Considering HumanBeing a class, which has properties like hands, legs, eyes etc, and functions like walk, talk, eat, see etc. Male and Female are also classes, but most of the properties and functions are included in HumanBeing, hence they can inherit everything from class HumanBeing using the concept of Inheritance.

考虑到HumanBeing是一类,它具有诸如手,腿,眼睛等的属性,并且具有走路,说话,吃饭,看东西等功能。 MaleFemale也是一类,但是HumanBeing中包含了大多数的属性和功能,因此它们可以使用继承概念继承HumanBeing中的所有内容。

对象 (Objects)

My name is Abhishek, and I am an instance/object of class Male. When we say, Human Being, Male or Female, we just mean a kind, you, your friend, me we are the forms of these classes. We have a physical existence while a class is just a logical definition. We are the objects.

我的名字是Abhishek,我是Male类的实例/对象 。 当我们说“人类,男性或女性”时,我们的意思是一种,您,您的朋友,我,我们就是这些课程的形式。 当一个类只是一个逻辑定义时,我们就有了物理存在。 我们是对象。

抽象化 (Abstraction)

Abstraction means, showcasing only the required things to the outside world while hiding the details. Continuing our example, Human Being's can talk, walk, hear, eat, but the details are hidden from the outside world. We can take our skin as the Abstraction factor in our case, hiding the inside mechanism.

抽象意味着在隐藏细节的同时只向外界展示必需的东西。 继续我们的例子, 人类可以说话,走路,听,吃,但是细节对外界是隐藏的。 在我们的案例中,我们可以将皮肤作为抽象因素,隐藏内部机制。

封装形式 (Encapsulation)

This concept is a little tricky to explain with our example. Our Legs are binded to help us walk. Our hands, help us hold things. This binding of the properties to functions is called Encapsulation.

用我们的例子来解释这个概念有些棘手。 我们的双腿被束缚以帮助我们行走。 我们的双手,帮助我们握紧东西。 属性与函数的这种绑定称为封装。

多态性 (Polymorphism)

Polymorphism is a concept, which allows us to redefine the way something works, by either changing how it is done or by changing the parts using which it is done. Both the ways have different terms for them.

多态是一个概念,它使我们可以通过更改完成方式或更改完成使用的部件来重新定义其工作方式。 两种方式都有不同的用语。

翻译自: https://www.studytonight.com/python/oops-in-python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值