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

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

问题描述

方法和函数有什么区别?是一种方法返回值而函数没有返回值吗?

What is the difference between a method and a function? Is it that a method returns a value and a function doesn't?

推荐答案

方法实际上是在类/对象的上下文中使用的函数.

Method is actually a function used in the context of a class/object.

当您在类/对象之外创建函数时,可以将其称为 function ,但是当您在类内部创建函数时,可以将其称为 method .

When you create a function outside of a class/object, you can call it a function but when you create a function inside a class, you can call it a method.

class foo {
   public function bar() { // a method
     ........
   }
}


function bar() {  // a function not part of an object
}


因此,对象可以具有方法(函数)和属性(变量).


So an object can have methods (functions) and properties (variables).

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

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