面向对象三大特性

目录

1.1面向对象三大特性

1.1.1封装Encapsulation

1.1.2继承 Inheritance

1.1.3多态 Polymorphism

1.1.4小结

1.1.5更多


1.1面向对象三大特性

封装、继承和多态是面向对象程序设计(Object-oriented programming,OOP)的三大特性,那么各自有哪些优势,又在哪应用呢?

1.1.1封装Encapsulation

定义:把客观的事物抽象成类,并将数据和操作数据的函数绑定在一起,确保不受外界的干扰和滥用。

优势:代码复用;模块化,去耦;数据隐藏,隐藏实现细节;安全性,访问权限提供保护机制;

应用:类,方法。

其他:

1、细分封装:

类:封装对象的属性和行为

方法:封装具体逻辑功能

访问:封装对其访问权限进行封装

 

1.1.2继承 Inheritance

定义:不同的抽象类之间存在层级间的所属关系“is-a”,继承类无需重新编码,即可重用现有类的功能或属性

优势:代码复用,重用相同的过程和数据定义;

应用:单继承、多继承。

其他:

  1. 组合、继承和委托。
  2. 继承的权限。

 

1.1.3多态 Polymorphism

定义:相同事物,同一接口不同实现。

优势:接口复用

应用:静态多态函数重载;动态(运行时)多态虚函数;

其他:

  1. 覆盖override,重载overload与重写overwrite

 

1.1.4小结

特性

优势

应用

封装

代码复用

安全性

解耦

方法

继承

代码复用

组合

继承

委托

多态

接口复用

函数重载

虚函数

多思考OOP的三大特性,编程时加以应用,往往能使你的代码变得更易复用和维护。

 

1.1.5更多

再看下维基百科对这三个概念的一些介绍。

Encapsulation 封装

Encapsulation is an object-oriented programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.

If a class does not allow calling code to access internal object data and permits access through methods only, this is a strong form of abstraction or information hiding known as encapsulation. Some languages (Java, for example) let classes enforce access restrictions explicitly, for example denoting internal data with the private keyword and designating methods intended for use by code outside the class with the public keyword. Methods may also be designed public, private, or intermediate levels such as protected (which allows access from the same class and its subclasses, but not objects of a different class). In other languages (like Python) this is enforced only by convention (for example, private methods may have names that start with an underscore). Encapsulation prevents external code from being concerned with the internal workings of an object. This facilitates code refactoring, for example allowing the author of the class to change how objects of that class represent their data internally without changing any external code (as long as "public" method calls work the same way). It also encourages programmers to put all the code that is concerned with a certain set of data in the same class, which organizes it for easy comprehension by other programmers. Encapsulation is a technique that encourages decoupling.

封装是一种面向对象的编程概念,它将数据和操作数据的函数绑定在一起,并且可以确保不受外界干扰和滥用。 数据封装引入了重要的OOP概念:数据隐藏。

如果类不允许调用代码访问内部对象数据,并且仅允许通过方法进行访问,则这是一种抽象形式或信息隐藏的强大形式,称为封装。某些语言(例如Java)允许类明确实施访问限制,例如,使用private关键字表示内部数据,并使用public关键字指定打算供类外代码使用的方法。方法也可以设计为公共,私有或中间级别,例如受保护的(允许从相同的类及其子类进行访问,但不允许不同类的对象进行访问)。在其他语言(如Python)中,这仅是由约定强制执行的(例如,私有方法的名称可能以下划线开头)。封装可以防止外部代码与对象的内部逻辑有关联。这有助于代码重构,例如,允许类的创建者更改该类的对象在内部表示其数据的方式,而无需更改任何外部代码(只要“公共”方法调用以相同的方式工作)。它还鼓励程序员将与特定数据集有关的所有代码放在同一类中,从而将其组织起来以便于其他程序员轻松理解。封装是一种鼓励去耦的技术。

 

Composition, inheritance, and delegation组合,继承和委托

Objects can contain other objects in their instance variables; this is known as object composition. For example, an object in the Employee class might contain (either directly or through a pointer) an object in the Address class, in addition to its own instance variables like "first_name" and "position". Object composition is used to represent "has-a" relationships: every employee has an address, so every Employee object has access to a place to store an Address object (either directly embedded within itself, or at a separate location addressed via a pointer).

对象可以在其实例变量中包含其他对象。 这称为对象组合。 例如,除了其自己的实例变量(例如“ first_name”和“ position”)之外,Employee类中的对象还可能包含(直接或通过指针)Address类中的对象。 对象组成用于表示“具有”关系:每个员工都有一个地址,因此每个Employee对象都可以访问一个存储Address对象的位置(可以直接嵌入其内部,也可以位于通过指针寻址的单独位置)。

Languages that support classes almost always support inheritance. This allows classes to be arranged in a hierarchy that represents "is-a-type-of" relationships. For example, class Employee might inherit from class Person. All the data and methods available to the parent class also appear in the child class with the same names. For example, class Person might define variables "first_name" and "last_name" with method "make_full_name()". These will also be available in class Employee, which might add the variables "position" and "salary". This technique allows easy re-use of the same procedures and data definitions, in addition to potentially mirroring real-world relationships in an intuitive way. Rather than utilizing database tables and programming subroutines, the developer utilizes objects the user may be more familiar with: objects from their application domain.

Subclasses can override the methods defined by superclasses. Multiple inheritance is allowed in some languages, though this can make resolving overrides complicated.

Abstract classes cannot be instantiated into objects; they exist only for the purpose of inheritance into other "concrete" classes which can be instantiated. In Java, the final keyword can be used to prevent a class from being subclassed.

The doctrine of composition over inheritance advocates implementing has-a relationships using composition instead of inheritance. For example, instead of inheriting from class Person, class Employee could give each Employee object an internal Person object, which it then has the opportunity to hide from external code even if class Person has many public attributes or methods. Some languages, like Go do not support inheritance at all.

The "open/closed principle" advocates that classes and functions "should be open for extension, but closed for modification".

Delegation is another language feature that can be used as an alternative to inheritance.

支持类的语言几乎总是支持继承。 这允许将类按表示“是某类型”关系的层次结构进行排列。 例如,Employee类可以从Person类继承。 父类可用的所有数据和方法也以相同的名称出现在子类中。 例如,类Person可能使用方法“ make_full_name()”定义变量“ first_name”和“ last_name”。 这些也可以在Employee类中使用,该类可以添加变量“ position”和“ salary”。 除了潜在地以直观方式镜像现实世界的关系之外,该技术还允许轻松重用相同的过程和数据定义。 开发人员不是使用数据库表和编程子例程,而是使用用户可能更熟悉的对象:来自其应用程序域的对象。

子类可以覆盖超类定义的方法。 在某些语言中允许多重继承,尽管这会使解析重写变得复杂。

抽象类不能实例化为对象。 它们的存在仅出于继承到其他可以实例化的“具体”类的目的。 在Java中,可以使用final关键字来防止类被子类化。

关于继承的组成学说主张使用组合而不是继承来实现关系。例如,不是从类Person继承,而是将Employee类赋予每个Employee对象一个内部Person对象,然后,即使Person类具有许多公共属性或方法,它也有机会从外部代码中隐藏。 某些语言(例如Go)根本不支持继承。

“开放/封闭原则”主张类和功能“应该开放以进行扩展,而封闭以进行修改”。

委托是可以用作继承的另一种语言功能。

 

Polymorphism 多态

Subtyping – a form of polymorphism – is when calling code can be agnostic as to which class in the supported hierarchy it is operating on – the parent class or one of its descendants. Meanwhile, the same operation name among objects in an inheritance hierarchy may behave differently.

For example, objects of type Circle and Square are derived from a common class called Shape. The Draw function for each type of Shape implements what is necessary to draw itself while calling code can remain indifferent to the particular type of Shape is being drawn.

This is another type of abstraction which simplifies code external to the class hierarchy and enables strong separation of concerns.

子类型化(一种多态形式)是指调用代码时无法确定其在支持的层次结构中的哪个类(父类或其后代之一)上进行操作。 同时,继承层次结构中对象之间相同的操作名称可能会表现不同。

例如,圆形和正方形类型的对象是从称为Shape的通用类派生的。每种Shape的Draw函数实现了绘制自身所需的功能,同时调用代码可以与正在绘制的Shape的特定类型无关。

这是另一种抽象类型,它简化了类层次结构外部的代码并实现了关注点的强烈分离。

你可能感兴趣的:(C++,编程语言)