oop几个原则_每个初学者都应该了解OOP

oop几个原则

Object-oriented programming is a programming model based on the concept of “objects”, which can contain data, in the form of fields, and code, in the form of procedures. A feature of objects is that an object’s own procedures can access and often modify the data fields of itself. If you are starting to learn about an OOP then these topics treated here should help you get a better understanding.

面向对象的编程是一种基于“对象”概念的编程模型,该模型可以包含字段形式的数据和过程形式的代码。 对象的一个​​特征是对象自己的过程可以访问并经常修改其数据字段。 如果您开始学习有关OOP的知识,那么这里讨论的这些主题应该可以帮助您更好地理解。

类与对象 (Class vs Object)

In Object-Oriented Programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). Each object is said to be an instance of a particular class, for example, the class ‘car’ here serves as a blueprint for other objects like ‘polo’, ‘mini’ and ‘beetle’. Which all have different values of color but all stick to the blueprint form of a car.

在面向对象的编程中, 是用于创建对象 (特定的数据结构),提供状态(成员变量或属性)的初始值以及行为的实现(成员函数或方法)的蓝图。 每个对象都被称为特定类的实例,例如,“汽车”类在这里充当其他对象(如“马球”,“迷你”和“甲壳虫”)的蓝图。 它们都具有不同的颜色值,但都遵循汽车的蓝图形式。

Objects are an instance of a class. Objects can interact with one another without having to know all the details of their internal code. More than one instance of the same class can exist at any one time, which means there can be multiple objects made from the same class.

对象是类的实例。 对象可以彼此交互,而不必知道其内部代码的所有细节。 同一类可以同时存在多个实例,这意味着同一类可以有多个对象。

类与结构 (Class vs Struct)

The major difference of class is that provides the flexibility of combining data and methods (functions) and it provides the re-usability called inheritance. Structs should typically be used for grouping data. Class is pass-by-reference and Struct is pass-by-copy, it means that Class is a reference type and its object is created on the heap memory whereas structure is a value type and its object is created on the stack memory.

类的主要区别在于,它提供了组合数据和方法(函数)的灵活性,并且提供了称为继承的可重用性。 结构通常应用于对数据进行分组。 Class是传递引用,而Struct是传递副本,这意味着Class是引用类型,其对象在堆内存中创建,而structure是值类型,其对象在堆栈存储器中创建。

Image for post

Classes are still fit for larger or complex objects and also allow to perform cleanup (garbage collector) before an object is deallocated because garbage collector works on heap memory, on the other hand, Structs are good for small, isolated model objects and cannot be garbage collector so no efficient memory management.

类仍然适用于较大或复杂的对象,并且还允许在释放对象之前执行清理(垃圾收集器),因为垃圾收集器可在堆内存上工作,另一方面,结构适用于较小的隔离模型对象,并且不能成为垃圾收集器,因此没有有效的内存管理。

OOP基础:构造函数,解构函数,重载与覆盖 (OOP basics: Constructor, Deconstructor, Overloading vs Override)

In Object-Oriented Programming, a Constructor is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

在面向对象的编程中, 构造函数是一种特殊的子例程类型,称为创建对象。 它准备使用新对象,通常接受构造函数用来设置所需成员变量的参数。

In Object-Oriented Programming, a Destructor gives an object a last chance to clean up any memory it allocated or perform any other tasks that must be completed before the object is destroyed.

在面向对象的编程中, 析构函数为对象提供最后的机会来清理它分配的所有内存,或执行销毁对象之前必须完成的任何其他任务。

In Object-Oriented Programming, Overloading refers to the ability to use a single identifier to define multiple methods of a class that differs in their input and output parameters. Overloaded methods are generally used when they conceptually execute the same task but with a slightly different set of parameters.

在面向对象的编程中, 重载是指使用单个标识符来定义类的输入和输出参数不同的多个方法的能力。 当重载的方法在概念上执行相同的任务但参数集略有不同时,通常会使用重载的方法。

In Object-Oriented Programming, Overriding feature that enables a child class to provide a different implementation for a method that is already defined and/or implemented in its parent class or one of its parent classes. The overridden method in the child class should have the same name, signature, and parameters as the one in its parent class.

在面向对象程序设计中, 重写功能使子类能够为其在其父类或其父类之一中已定义和/或实现的方法提供不同的实现。 子类中的重写方法应与其父类中的方法具有相同的名称,签名和参数。

多态性 (Polymorphism)

In Object-Oriented Programming, Polymorphism refers to a programming language’s ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. It’s often expressed as ‘one interface, multiple functions’.

在面向对象的编程中, 多态性是指编程语言根据对象的数据类型或类以不同方式处理对象的能力。 更具体地说,它是重新定义派生类的方法的能力。 它通常表示为“一个界面,多种功能”。

Image for post

There are two types of polymorphism in C#: compile time polymorphism and runtime polymorphism. Compile-time polymorphism is achieved by method overloading and operator overloading in C#. It is also known as static binding or early binding. Runtime polymorphism is achieved by method overriding which is also known as dynamic binding or late binding.

C#中的多态有两种类型: 编译时多态运行时多态 。 通过C#中的方法重载和运算符重载来实现编译时多态 。 也称为静态绑定或早期绑定。 运行时多态性是通过方法覆盖(也称为动态绑定或后期绑定)实现的。

“是”和“具有”对象关系之间的区别 (Difference between “Is a” and “Has a” object relationship)

In Object-Oriented Programming, IS — A relationship is completely inheritance. The classes which inherit are known as subclasses or child classes. This means, that the child class is a type of parent class. For example, an apple is a fruit. So you will extend fruit to get an apple.

在面向对象编程中,IS —关系是完全继承。 继承的类称为子类或子类。 这意味着子类是父类的一种。 例如,苹果是水果。 因此,您将扩展水果以获得一个苹果。

Image for post

In Object-Oriented Programming, a HAS-A relationship is dynamic (run time) binding while inheritance is a static (compile-time) binding. If you just want to reuse the code and you know that the two are not of same kind use composition. For example, you cannot inherit an oven from a kitchen. A kitchen HAS-A oven. When you feel there is a natural relationship like Apple is a Fruit use inheritance.

在面向对象的编程中,HAS-A关系是动态(运行时)绑定,而继承是静态(编译时)绑定。 如果您只是想重用代码,并且知道两者不是同一种类型,请使用组合。 例如,您不能从厨房继承烤箱。 厨房有HAS-A烤箱。 当您感觉到存在自然关系时,例如Apple是Fruit的使用继承。

Image for post

类与接口 (Class vs Interface)

An Interface provides a contract specifying how to create an Object, without caring about the specifics of how they do the things. An Interface is a reference type and it included only abstract members such as Events, Methods, Properties, etc. and it has no implementations for any of its members.

接口提供了一个合同,该合同指定了如何创建对象,而无需关心它们如何做的细节。 接口是一种引用类型,它仅包含事件,方法,属性等抽象成员,并且没有任何成员的实现。

A Class has both definitions and an implementation whereas Interface only has a definition. A Class can be instantiated but an Interface cannot be instantiated. You can create an instance of an object that implements the Interface. A Class is a full-body entity with members, methods along with there definition and implementation. An Interface is just a set of definitions that you must implement in your Class inheriting that Interface.

类既具有定义又具有实现,而接口仅具有定义。 可以实例化一个类,但是不能实例化一个接口。 您可以创建实现接口的对象的实例。 类是具有成员,方法以及定义和实现的全身实体。 接口只是在继承该接口的类中必须实现的一组定义。

SOLID原则 (SOLID principles)

In object-oriented computer programming, SOLID is a mnemonic acronym for five design principles intended to make software designs more understandable, flexible, and maintainable.

在面向对象的计算机编程中,SOLID是五个设计原则的助记符缩写,旨在使软件设计更易于理解,灵活和可维护。

Single responsibility principle. A class should only have a single responsibility, that is, only changes to one part of the software’s specification should be able to affect the specification of the class.

单一责任原则。 一个类应该只负责一个职责,也就是说,只有对软件规范的一部分进行更改才能影响该类的规范。

Open principle. The open/closed principle states that software entities (classes, modules, functions, etc.) should be open for extensions, but closed for modification.

开放原则。 开放/封闭原则指出,软件实体(类,模块,功能等)应为扩展而开放,但为修改而封闭。

Liskov substitution principle. This one is probably the hardest one to wrap your head around when being introduced for the first time. It goes like this if S is a subtype of T, then objects of type T may be replaced (or substituted) with objects of type S.

里斯科夫替代原则。 第一次被介绍时,这可能是最难缠住你的头的人。 如果S是T的子类型,则类似这样,则可以用S类型的对象替换(或替换)T类型的对象。

Interface segregation principle. No client should be forced to depend on methods it does not use. Put more simply do not add additional functionality to an existing interface by adding new methods.

接口隔离原则。 不应强迫任何客户端依赖其不使用的方法。 简而言之,不要通过添加新方法来向现有接口添加其他功能。

Dependency inversion principle. It’s a way to decouple software modules. This principle states that High-level modules should not depend on low-level modules. Both should depend on abstractions. And abstractions should not depend on details. Details should depend on abstractions.

依赖反转原理。 这是分离软件模块的一种方式。 该原则指出,高级模块不应依赖于低级模块。 两者都应依赖抽象。 而且抽象不应依赖细节。 细节应取决于抽象。

多重继承 (Multiple inheritances)

Multiple inheritances is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class. It is distinct from a single inheritance, where an object or class may only inherit from one particular object or class.

多重继承是某些面向对象的计算机编程语言的功能,其中对象或类可以从多个父对象或父类继承特征和功能。 它不同于单个继承,在单个继承中,一个对象或类只能从一个特定的对象或类继承。

In object-oriented programming (OOP), inheritance describes a relationship between two classes in which one class (the child class) subclasses the parent class. The child inherits the methods and attributes of the parent, allowing for shared functionality.

在面向对象编程(OOP)中,继承描述了两个类之间的关系,其中一个类(子类)将父​​类子类化。 子级继承父级的方法和属性,从而实现共享功能。

经营者 (Operators)

Operators are symbols that are used to perform operations on operands. Operands may be variables and/or constants.

运算符是用于对操作数执行运算的符号。 操作数可以是变量和/或常量。

Basic assignment operator,(=) is used to assign values to variables.

基本赋值运算符 (=)用于为变量赋值。

Arithmetic operators are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc.

算术运算符用于执行算术运算,例如加法,减法,乘法,除法等。

Relational operators are used to check the relationship between the two operands. If the relationship is true the result will be true, otherwise, it will result in false. Relational operators are used in decision making and loops.

关系运算符用于检查两个操作数之间的关系。 如果关系为真,则结果为真,否则为假。 关系运算符用于决策和循环。

Logical operators are used to perform a logical operation such as and, or. Logical operators operate on boolean expressions (true and false) and return boolean values. Logical operators are used in decision making and loops.

逻辑运算符用于执行诸如和或的逻辑运算。 逻辑运算符对布尔表达式(真和假)进行运算,并返回布尔值。 逻辑运算符用于决策和循环。

The unary operators unlike other operators they operate on a single operand.

一元运算符与其他运算符不同,它们在单个操作数上进行运算。

The ternary operator? : operates on three operands. It is a shorthand for the if-then-else statement. The ternary operator works as follows: — If the expression stated by Condition is true, the result of Expression1 is assigned to the variable. — If it is false, the result of Expression2 is assigned to a variable.

三元运算符 ? :对三个操作数进行运算。 这是if-then-else语句的缩写。 三元运算符的工作方式如下:—如果Condition所表示的表达式为true,则将Expression1的结果分配给该变量。 —如果为false,则将Expression2的结果分配给变量。

Bitwise and bit shift operators are used to perform bit manipulation operations.

按位和移位运算符用于执行位操作。

Compound Assignment Operators :

复合分配运算符:

Image for post

列表与数组 (List vs Arrays)

Lists and Arrays are both objects that can be used to hold variables, but they aren’t interchangeable. A list is an object which holds variables in a specific order. The type of variable that the list can store is defined using the generic syntax.

列表和数组都是可用于保存变量的对象,但它们不可互换。 列表是按特定顺序保存变量的对象。 列表可以存储的变量类型是使用通用语法定义的。

An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations.

数组存储相同类型元素的固定大小的顺序集合。 数组用于存储数据集合,但是将数组视为存储在连续内存位置的相同类型变量的集合通常会更有用。

数据结构 (Data structures)

An array is basically a list of objects. Its defining traits are that all the objects are the same type (in most cases) and there is a specific number of them. The nature of an array allows for very fast access to elements based on their position within the list (otherwise known as the index).

数组基本上是对象列表。 它的定义特征是,所有对象都是同一类型(在大多数情况下),并且它们有特定数量。 数组的性质允许根据元素在列表中的位置(也称为索引)非常快速地访问元素。

ArrayList is a dynamic array. What that means is an ArrayList can have any amount of objects and of any type. This data structure was designed to simplify the processes of adding new elements into an array. An ArrayList is an array whose size is doubled every time it runs out of space. Doubling the size of the internal array is a very effective strategy that reduces the amount of element-copying in the long run.

ArrayList是一个动态数组。 这意味着ArrayList可以具有任意数量的对象和任何类型。 该数据结构旨在简化将新元素添加到数组中的过程。 ArrayList是一个数组,每次空间用尽时其大小都会加倍 。 将内部数组的大小加倍是一种非常有效的策略,从长远来看,它可以减少元素复制的数量。

The List data structure was introduced in the .NET Framework 2.0 as part of the new set of generic collections. The list is the generic version of ArrayList, which means that it behaves exactly the same way, but within a specified type.

List数据结构是.NET Framework 2.0中引入的,是新的一组通用集合的一部分。 该列表是ArrayList的通用版本,这意味着它的行为完全相同,但是在指定的type内

A LinkedList is a series of objects which instead of having their references indexed (like an Array), stay together by linking to each other, in Nodes. A LinkedList Node has basically three values: the Object’s Value, a reference to the Next node, and a reference to the Previous Node.

LinkedList是一系列对象,它们通过在节点中相互链接而保持在一起,而不是对它们的引用进行索引(例如,数组)。 一个LinkedList节点基本上具有三个值:对象的值,对下一个节点的引用和对上一个节点的引用。

The Dictionary data structure is an extremely useful data structure since it allows the programmer to handle the index keys.

字典数据结构是一种非常有用的数据结构,因为它允许程序员处理索引键。

The Hashtable data structure is very much like the Dictionary data structure. A Hashtable also takes in a key/value pair, but it does so as generic objects as opposed to typed data.

Hashtable数据结构非常类似于Dictionary数据结构。 哈希表也接受键/值对,但它作为通用对象而不是键入数据。

The HashSet data structure resembles the List<> data structure, the very important difference it does not allow duplicates.

HashSet数据结构类似于List <>数据结构,这是非常重要的区别,它不允许重复。

最后尝试赶上 (Try Catch Finally)

An exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. C# exception handling is performed using the following keywords −

例外是对程序运行时出现的异常情况的响应,例如尝试除以零。 使用以下关键字执行C#异常处理-

try − A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.

try -try块标识为其激活了特定异常的代码块。 随后是一个或多个捕获块。

catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

catch-程序在要处理问题的程序中的位置捕获带有异常处理程序的异常。 catch关键字指示捕获异常。

finally − The ‘finally’ block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.

最终 -“ finally”块用于执行给定的一组语句,无论是否引发异常。 例如,如果打开文件,则无论是否引发异常,都必须关闭该文件。

异常处理(抛出/抛出新异常) (Exception handling (throw/throw new))

Exceptions are used to indicate that an error has occurred while running the program. Exception objects that describe an error are created and then thrown with the throw keyword. The runtime then searches for the most compatible exception handler.

异常用于指示在运行程序时发生了错误。 创建描述错误的异常对象,然后使用throw关键字引发该对象。 然后,运行时搜索最兼容的异常处理程序。

throw rethrows the original exception and preserves its original stack trace.

throw引发原始异常并保留其原始堆栈跟踪。

throw ex throws the original exception but resets the stack trace, destroying all stack trace information until your catch block.

throw ex会引发原始异常,但会重置堆栈跟踪,并破坏所有堆栈跟踪信息,直到您的catch块为止。

throw new Exception is even worse. It creates a brand new Exception instance, losing the original stack trace of the exception, as well as its type.

抛出新异常更加糟糕。 它会创建一个全新的Exception实例,丢失该异常的原始堆栈跟踪及其类型。

for和foreach之间的区别 (Differences between for and foreach)

The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false.

for循环重复执行一个语句或语句块,直到指定的表达式求值为false。

Image for post

The foreach statement repeats a group of embedded statements for each element in an array or an object collection. You do not need to specify the loop bounds minimum or maximum. The following code loops through all items of an array.

foreach语句为数组或对象集合中的每个元素重复一组嵌入式语句。 您无需指定循环边界的最小值或最大值。 以下代码循环遍历数组的所有项目。

Image for post

foreach: Treats everything as a collection and reduces the performance. foreach creates an instance of an enumerator (returned from GetEnumerator()) and that enumerator also keeps state throughout the course of the foreach loop. It then repeatedly calls for the Next() object on the enumerator and runs your code for each object it returns.

foreach:将所有内容视为集合并降低性能。 foreach创建一个枚举器的实例(从GetEnumerator()返回),并且该枚举器还在foreach循环的整个过程中保持状态。 然后,它反复调用枚举器上的Next()对象,并为返回的每个对象运行代码。

访问修饰符 (Access modifiers)

All types and type members have an accessibility level, which controls whether they can be used from other code in your assembly or other assemblies. You can use the following access modifiers to specify the accessibility of a type or member when you declare it:

所有类型和类型成员都有可访问性级别,该级别控制是否可以从程序集中的其他代码或其他程序集中使用它们。 声明类型或成员时,可以使用以下访问修饰符来指定它:

public — The type or member can be accessed by any other code in the same assembly or another assembly that references it.

public —该类型或成员可以由同一程序集中或引用它的另一个程序集中的任何其他代码访问。

private — The type or member can be accessed only by code in the same class or struct.

私有 —类型或成员只能由相同类或结构中的代码访问。

protected — The type or member can be accessed only by code in the same class, or in a class that is derived from that class.

protected —类型或成员只能由同一类或从该类派生的类中的代码访问。

internal — The type or member can be accessed by any code in the same assembly, but not from another assembly.

内部的 —类型或成员可以由同一程序集中的任何代码访问,但不能从另一个程序集中访问。

protected internal — The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.

受保护的内部 —类型或成员可以由声明该类型或成员的程序集中的任何代码访问,也可以从另一个程序集中的派生类中访问。

private protected — The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.

私有受保护的 -该类型或成员只能在其声明程序集中通过同一类或从该类派生的类型的代码进行访问。

翻译自: https://levelup.gitconnected.com/what-every-beginner-should-know-about-oop-e21719634911

oop几个原则

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值