ios iOS中的继承、多态、封装?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25236528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:47:23  来源:igfitidea点击:

Inheritance, polymorphism, encapsulation in iOS?

iosobjective-c

提问by Suraj SS

What is exactly inheritance, polymorphism and encapsulation in iOS with example? Do iOS uses all this features? And how?

举个例子,iOS 中的继承、多态和封装到底是什么?iOS 是否使用所有这些功能?如何?

回答by Nitin Gohel

The concept of inheritance brings something of a real-world view to programming. It allows a class to be defined that has a certain set of characteristics (such as methods and instance variables) and then other classes to be created which are derived from that class. The derived class inherits all of the features of the parent class and typically then adds some features of its own.

继承的概念为编程带来了一些现实世界的观点。它允许定义具有特定特征集(例如方法和实例变量)的类,然后创建从该类派生的其他类。派生类继承了父类的所有功能,然后通常会添加一些自己的功能。

Please check This link: An Objective-C Inheritance Example

请检查此链接:Objective-C 继承示例

The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

Objective-C polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

Consider the example, we have a class Shape that provides the basic interface for all the shapes. Square and Rectangle are derived from the base class Shape.

多态这个词意味着有多种形式。通常,当存在类的层次结构并且它们通过继承相关时,就会发生多态性。

Objective-C 多态性意味着对成员函数的调用将导致执行不同的函数,具体取决于调用该函数的对象类型。

考虑这个例子,我们有一个 Shape 类,它为所有形状提供基本接口。Square 和 Rectangle 派生自基类 Shape。

Please check This link: An Objective-C Polymorphism Example

请检查此链接:Objective-C 多态性示例

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.

Data encapsulation is a mechanism of bundling the data and the functions that use them, and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.

封装是一种面向对象的编程概念,它将数据和操作数据的函数绑定在一起,并防止外部干扰和误用。数据封装导致了数据隐藏的重要 OOP 概念。

数据封装是一种将数据和使用它们的功能捆绑在一起的机制,而数据抽象是一种只暴露接口并向用户隐藏实现细节的机制。

Please check This link: An Objective-C Data Encapsulation

请检查此链接:Objective-C 数据封装