方法和函数有什么区别? [英] What's the difference between a method and a function?

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

问题描述

有人可以在OOP上下文中提供方法功能的简单解释吗?

Can someone provide a simple explanation of methods vs. functions in OOP context?

推荐答案

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

A function is a piece of code that is called by name. It can be passed data to operate on (i.e. 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 a name that is associated with an object. In most respects it is identical to a function except for two key differences:

  1. 方法被隐式传递给调用它的对象.
  2. 一种方法能够对类中包含的数据进行操作(记住,对象是类的实例-类是定义,对象是该数据的实例).

(这是一个简化的说明,忽略了范围等的问题)

(this is a simplified explanation, ignoring issues of scope etc.)

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

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