📅  最后修改于: 2021-01-11 08:02:44             🧑  作者: Mango
有人认为功能和方法相同,并且可以互换使用这些术语。但是,这两者略有不同。 “方法属于类,结构和枚举,而函数不属于。”
func thisIsAFunction() {
}
struct Employee {
func thisIsAMethod() {
}
}
让我们看一下函数和方法之间的一些区别:
Function | Method |
---|---|
Functions have independent existence. You can define them outside of the class. | Methods do not have independent existence. They are always defined within a class, struct, or enum. |
Functions are the properties of structured languages like C, C++, Pascal and object based language like JavaScript. Note: There is no concept of function in Java. |
Methods are the properties of Object-oriented language like C#, Java, Swift etc. |
Functions don’t have any reference variables. | Methods are called using reference variables. |
Functions are a self describing piece of code. | Methods are used to manipulate instance variable of a class. |
Functions are called independently. | Methods are called using instance or object. |