类和对象:面向对象编程_面向对象的编程概念:如何使对象从零变为一

类和对象:面向对象编程

Object Oriented Programming is one of the most widely used programming paradigms. The name itself defines how it works. “Object Oriented” - the Object plays an important role. Manipulating objects and getting the results is the ultimate goal of Object Oriented Programming.

面向对象编程是最广泛使用的编程范例之一。 名称本身定义了它的工作方式。 “面向对象”-对象扮演重要角色。 操纵对象并获得结果是面向对象编程的最终目标。

The languages that use Object Oriented Programming paradigms are known as Object Oriented Programming Languages. They are mostly high level languages such as

使用面向对象编程范例的语言称为面向对象编程语言。 它们主要是高级语言,例如

  1. Java

    Java
  2. C#

    C#
  3. Python - Python is both a Scripted/Structured & Object Oriented Language

    Python-Python既是脚本化/结构化还是面向对象的语言

To program in Object Oriented Programming, concepts called “Object Oriented Concepts” are used. These concepts simplify & add more value to Object Oriented Programming.

在面向对象编程中进行编程时,使用了称为“ 面向对象概念 ”的概念 。 这些概念简化了面向对象的程序并为其添加了更多价值。

Those concepts are

这些概念是

  1. Encapsulation

    封装形式
  2. Abstraction

    抽象化
  3. Polymorphism

    多态性
  4. Inheritance

    遗产

Before moving into these concepts, we need to know about Class & Objects.

在进入这些概念之前,我们需要了解类和对象。

An Object is the basic run-time entity in OOP. In our day-today life we see a lot of objects like a television, mobile phone, dog, humans, cars & other living and non-living objects. These can be portrayed as objects in OOP.

对象是OOP中的基本运行时实体。 在我们的日常生活中,我们看到许多物体,例如电视,手机,狗,人,汽车以及其他有生命的物体和无生命的物体。 这些可以被描述为OOP中的对象。

A Class is a blueprint or prototype that defines variables/properties and methods/functions common to all objects of a certain kind. It's a logical component.

是定义某种类型的所有对象共有的变量/属性以及方法/功能的蓝图或原型。 这是一个逻辑组件。

Simply said, Class is a user-defined data type. Objects are variables of a Class. Once the Class has been created we can create as many Objects as we want.

简而言之,Class是用户定义的数据类型。 对象是类的变量 。 创建类后,我们可以根据需要创建任意数量的对象。

For example, take a class named Tree. State/properties of the Tree class are:

例如,选择一个名为Tree的类。 Tree类的状态/属性为:

  • Name of the tree

    树名
  • Age of the tree

    树的年龄
  • Type of the tree

    树的类型
  • Height of the tree

    树的高度

State/properties are used to define the attributes of an object.

状态/属性用于定义对象的属性。

That is, State/properties/attributes all represents the same thing.

也就是说, 状态/属性/属性都代表同一件事。

Behaviors of the Tree can be:

树的行为可以是:

  • Giving fruit

    送水果
  • Falling of leaves

    落叶
  • Absorbing water from roots to the upper parts

    从根部吸收水分到上部
  • Creating shadows

    创建阴影

Then, Mango is a variable of Class Tree. We can store and retrieve all the properties & behaviors we defined for the class Tree by creating an object of Mango.

那么,芒果是类树的变量。 我们可以通过创建Mango对象来存储和检索为类Tree定义的所有属性和行为。

Syntax for Creating an object of Mango from class Tree:

从类Tree创建Mango对象的语法:

Tree Mango;

芒果;

封装形式 (Encapsulation)

Have you ever used a tablet/medicine which encapsulated by a colored cover?

您是否曾经使用过由彩色盖包裹的药片/药物?

Inside that medicine is kept safely. We can't find anything with our naked eye. To see what's inside we need to open up that cover....

里面的药是安全保存的。 我们用肉眼找不到任何东西。 要查看里面有什么,我们需要打开盖子。

Similarly, all the data members (variables,attributes/properties) & behaviors(functions/methods) are gathered together and closed. Class is a best example for Encapsulation.

同样,将所有数据成员(变量,属性/属性)行为(函数/方法)收集在一起并关闭。 类是封装的最佳示例。

For an instance,

例如

You are going to the pharmacy to buy prescribed medicines for you. You handover the prescription to the pharmacist and then he/she will take the medicine from the store & give you the bill.

您将去药房为您购买处方药。 您将处方移交给药剂师,然后他/她将从商店取药并给您账单。

In this scenario,

在这种情况下,

Medicines - act as variables or properties or attributes

药物-充当变量或属性或属性

Pharmacist - act as member function/method where he/she helps in giving you the        medicine

药剂师-担任成员职能/方法,帮助他/她为您提供药物

You - external application or another software code

您-外部应用程序或其他软件代码

By using Encapsulation, data can be restricted from the usage of the outside world. The function defined in the class only can access the properties. This can be defined at the implementation stage. Programmers can define and specify the accessibility of the member variables while under development rather than making all the variables global like in procedural languages. This controlling of accessibility is also known as Information hiding.

通过使用封装, 可以限制数据不受外界使用。 类中定义的函数只能访问属性。 这可以在实施阶段定义。 程序员可以在开发过程中定义和指定成员变量的可访问性,而不必像在过程语言中那样使所有变量都具有全局性。 这种对可访问性的控制也称为信息隐藏

Encapsulation allows to expose the necessary things and hiding the important things from the outside world. So, the hidden parts of a class acts like Encapsulation & the exposed parts acts like Abstraction.

封装允许暴露必要的事物并从外界隐藏重要的事物 。 因此,类的隐藏部分的行为类似于Encapsulation ,而暴露的部分的行为类似于Abstraction

抽象化 (Abstraction)

Exposing necessary features of a class without explaining much or details is done by Abstraction.

不解释太多细节的情况下公开类的必要功能是由Abstraction完成的。

Today morning I wanted to make a hot tea and I used a water kettle to boil the water. I just turned on the On button to start boil water. I don’t want to know the inner workflow of the kettle where it has high resistance and that resistance produces heat and boils the water. Instead, I have to fulfill my work easily. Therefore, having this On button to boil the water is known as Abstraction.

今天早上,我想泡一杯热茶,然后用水壶烧开水。 我刚刚打开“ 开”按钮开始烧水。 我不想知道水壶的内部工作流程,因为它具有很高的阻力,而阻力会产生热量并使水沸腾。 相反,我必须轻松完成工作。 因此,使用此“开”按钮将水烧开称为“抽象”。

Similarly, we can take a remote controller which helps in manipulating TV operation using simple keys in the remote.

同样,我们可以使用一个遥控器,该遥控器可使用遥控器中的简单键来帮助操作电视。

Data abstraction is a programming technique that depends on the separation of interface and implementation.

数据抽象是一种编程技术,它依赖于接口实现的分离。

This data abstraction can be archived from using 2 different classes while coding using OOP

使用OOP进行编码时,可以使用2个不同的类来存档此数据抽象

  1. Abstraction class:  (0-100)% abstraction

    抽象类:(0-100)%抽象
  2. Interface class:  100% abstraction

    接口类别:100%抽象

遗产 (Inheritance)

The word itself describe what its functionality. Everyone has their inheritance qualities from their birth. You could have your grandparents/ your parents qualities from your birth. This is what Inheritance does in OOP.

这个词本身描述了它的功能。 每个人从出生起就有继承的素质。 您一出生就可以拥有祖父母/父母的素质。 这就是继承在OOP中的作用。

A class can have properties & methods of its parent class. Additionally the class can have its own properties and methods too. The parent class is called as base class. The class that inherits base class is called as derived class. Inheritance is the most powerful feature of OOP.

一个类可以具有其父类的属性和方法。 另外,该类也可以具有自己的属性和方法。 父类称为基类 。 继承基类的类称为派生类 。 继承是OOP的最强大功能。

By using Inheritance effectively we could save a lot of time and reduce the errors in our program. This causes us to increase the quality of work and productivity.

通过有效地使用继承,我们可以节省大量时间并减少程序中的错误。 这使我们提高了工作质量生产率

There are different types of Inheritance

有不同类型的继承

  1. Single Inheritance

    单继承
  2. Hierarchical Inheritance

    层次继承
  3. Multiple Inheritance

    多重继承
  4. Multi Level Inheritance

    多级继承

多态性 (Polymorphism)

Polymorphism is a Greek term which refers to the ability of taking more than one form / overloading.

多态性是希腊语术语,表示采取多种形式/重载的能力。

Say for example we all know about functions in programming. They take different arguments inside parenthesis. Polymorphism is nothing but with the same function name, different arguments passed to get the result.

举例来说,我们都了解编程中的功能 。 他们在括号内采用了不同的论点。 多态只不过是具有相同的函数名称,传递了不同的参数以获得结果而已。

For e.g :- function called sum can take 2 arguments or 3 arguments.

例如:-名为sum的函数可以使用2个参数或3个参数。

sum(3,4)  sum(10,23,56)Calling these functions by providing suitable number of parameters will give the result according to how the called function has designed.

sum(3,4)sum(10,23,56)通过提供适当数量的参数来调用这些函数,将根据被调用函数的设计得出结果。

How the program distinguish which function need to be executed at the above scenario?

程序如何区分在上述情况下需要执行哪个功能?

There is a feature called Dynamic binding in OOP. This will call the actual function according to the program execution. When the program runs the function with 2 arguments compiler takes the two argument functions to execute the program similarly for 3 arguments too.

OOP中有一个称为动态绑定的功能。 这将根据程序执行调用实际功能。 当程序使用2个参数运行该函数时,编译器也将使用3个参数的两个参数函数来类似地执行该程序。

Until the run-time, compiler won’t know exactly which function need to be invoked. It depends on the way the program calls the function name. This is also be known as Late binding.

直到运行时,编译器才确切知道需要调用哪个函数。 这取决于程序调用函数名称的方式。 这也称为后期绑定。

面向对象概念的使用 (Uses of Object Oriented Concepts)

  • Data can be hidden from outside using Encapsulation (Information hiding)

    可以使用封装从外部隐藏数据(信息隐藏)

  • Code can be reused by using Inheritance

    可以通过使用继承重用代码

  • Operators/methods.functions can be overloaded by using Polymorphism. I.e: same function or operator name can be used for multi-tasking

    可以使用Polymorphism重载Operators / methods.functions。 即:相同的功能或操作员名称可用于多任务

  • Data abstraction can be archived from Abstraction.

    数据抽象可从Abstraction存档。

  • Project migrations are easy (can be converted to larger size from smaller size)

    项目迁移很容易(可以从较小的尺寸转换为较大的尺寸)
  • Partitioning the works for same project

    对同一项目的作品进行分区
  • Manageable software complexity

    可管理的软件复杂性

OOP的应用领域 (Areas of application of OOP)

  1. AI and expert systems

    人工智能和专家系统
  2. Enterprise applications

    企业应用
  3. Neural Network and parallel programming

    神经网络和并行编程
  4. Office automation systems

    办公自动化系统

Hope you enjoyed a brief introduction to Object Oriented Concepts by reading. I hope to write how can we program Object Oriented Programming in my upcoming posts too.

希望您阅读阅读后能轻松了解面向对象的概念。 我希望在以后的文章中也能写出如何编写面向对象程序设计的书。



Please send you feedback about my article to parathan19@gmail.com

请将您对我的文章的反馈意见发送给parathan19@gmail.com

LinkedIn | Twitter

领英 推特

翻译自: https://www.freecodecamp.org/news/object-oriented-concepts/

类和对象:面向对象编程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值