有些人认为功能和方法相同, 并且可以互换使用这些术语。但是, 这两者略有不同。 “方法属于类, 结构和枚举, 而函数不属于。”
例:
func thisIsAFunction() {
}
struct Employee {
func thisIsAMethod() {
}
}
让我们看看函数和方法之间的一些区别:
Function |
Method |
功能具有独立的存在。你可以在类之外定义它们。 |
方法没有独立的存在。它们总是在类, 结构或枚举中定义。 |
函数是结构化语言(如C, C ++, Pascal)和基于对象的语言(如JavaScript)的属性。注意:Java中没有函数的概念。 |
方法是诸如C#, Java, Swift等面向对象语言的属性。 |
函数没有任何参考变量。 |
使用引用变量调用方法。 |
函数是一段自我描述的代码。 |
方法用于操纵类的实例变量。 |
函数被独立调用。 |
使用实例或对象调用方法。 |
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_47330.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57