功能和方法之间的区别 [英] Difference between function and methods

查看:272
本文介绍了功能和方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数和方法之间有什么区别?

in C#.net What is the difference between functions and methods?

推荐答案

一些历史背景:在某些语言中,将类与非类(或结构)结合在一起在函数中,术语方法"用于将以类(或允许的结构)编写的方法与其他函数和过程区分开.还有一个传统(从Algol到Pascal甚至更远)都有两个不同的关键字:过程"和函数",该过程等效于C/C ++ void函数.

在.NET中,所有方法都称为方法,因为所有方法总是在类或结构中声明,因此术语函数"被视为古体.仅仅因为没有语义上的差异,将方法称为函数"并不是一个大错误.

并不是说该术语不会触及运行时语义,也就是说,该方法是否为静态(另一个术语类方法")与否无关紧要.对于运行时语义,静态方法扮演着遗留的非类"功能或过程的角色.

—SA
Some historical background: in some languages combining classes with non-class (or structure) functions, a term "method" is used to distinguish methods written in a class (or structure where allowed) from other functions and procedures. There is also a tradition (from Algol to Pascal and beyond) to have two distinct key words: "procedure" and "function" a procedure being the equivalent to C/C++ void function.

In .NET all is called method, because all methods are always declared in classes or structures, so the term "function" is considered as a archaism. This is not a big mistake to call a method "function", simply because there is not semantic difference.

Not that this terminology does not touch run-time semantic, that is, it does not matter is the method is static (another term "class method") or not. As to run-time semantic, a static method plays the role of the legacy "non-class" function or procedure.

—SA


它们是同一事物的2个字.您可以将它们称为方法或函数-没关系.

请注意,函数是指全局方法,而方法是指类成员,这是一个不正确的想法.我认为这个错误的想法得到了传播,因为Java和C#通常是指方法,而函数"一词通常是C ++背景的人们所使用的.

我认为这有点像文件夹vs目录.同样的东西,但是年轻人通常会说文件夹.
They are 2 words for the same thing. You can call them methods or functions - it does not matter.

Note that it''s an incorrect idea that a function refers to a global method and a method refers to a class member. I reckon this wrong idea was propagated because Java and C# typically refer to methods and the word function is more typically used by folks from a C++ background.

I reckon it''s a little like folder vs directory. The same thing, but younger people usually say folder.


函数是一段被名称调用的代码.可以传递数据以对其进行操作(即参数),还可以选择返回数据(返回值).

传递给函数的所有数据都被显式传递.

方法是一段与对象关联的名称调用的代码.在大多数方面,它与一个功能相同,但有两个主要区别.
1.它被隐式传递为其调用的对象
2.能够对类中包含的数据进行操作(记住对象是类的实例-类是定义,对象是该数据的实例)


IMP:
函数只是执行代码并可以返回
的代码的一部分 某物.在OOP中,方法是绑定到类的函数.作为
C#中没有独立的函数,C#中的每个函数都是一个方法
A function is a piece of code that is called by name. It can be passed data to operate on (ie. the parameters) and can optionally return data (the return value).

All data that is passed to a function is explicitly passed.

A method is a piece of code that is called by name that is associated with an object. In most respects it is identical to a function except for two key differences.
1. It is implicitly passed the object for which it was called
2. It is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data)


IMP :
A function is just a part of code that executes code and can return
something. A method is, in OOP, a function that is bound to a class. As
in C# there are no stand-alone functions, every function in C# is a method


这篇关于功能和方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆