什么是方法,属性和功能? [英] What is Method, Property and Function?

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

问题描述

是的,我正在为此而苦恼。我无法区分它们,因为我读到的每一个解释都是如此不清楚,而且足够哲学。有人能为我清除这些定义吗?谢谢你们。



这些定义同样适用于程序编程和oop?感谢。

解决方案

功能是指令的组合,以实现某些结果。它可能需要参数并返回结果。如果一个函数没有返回结果,它通常称为一个过程。例子:

$ p $ 函数drawLine(x1,y1,x2,y2):
//使用Bresenham算法绘制一条线从x1,y1到x2,y2。
//不返回任何内容

函数< number>添加(a,b):
//将a添加到b并将结果作为数字返回
返回a + b

所以函数需要做一些特殊的工作。例如,当您需要绘制一个3行的多边形作为矢量图像的一部分时,调用 drawLine 比将所有用于绘制线程的例程放置三次更方便内联。

方法(成员函数)与函数类似,它们属于类或对象,通常表示对象的动词/类。例如, Window 类型的对象通常会有方法 open close 它们对它们所属的对象进行相应的操作。

属性与日常语言一样,并且技术上是对象/类的字段有专用的getter / setter例程(可以认为是方法,有些语言没有属性,这种行为是通过私有字段+ get / set方法实现的。)。


Yeah, I'm struggling with that. I cannot distinguish among them because every explanation I read is so unclear and philosophical enough. Can someone clear up these definitions for me ? Thanks guys.

These definitions apply as much to procedural-programming as oop ? Thanks.

解决方案

Function is a combination of instructions coupled together to achieve some result. It may take arguments and return result. If a function doesn't return a result it is usually called a procedure. Examples:

function drawLine(x1, y1, x2, y2):
  // draws a line using Bresenham's algorithm from x1,y1 to x2,y2.
  // doesn't return anything

function <number> add(a, b):
  // adds a to b and returns the result as a number
  return a + b

So functions are to do some particular work. For example, when you need to draw a polygon of 3 lines as a part of a vector image it is more convenient to call drawLine thrice than to put all the routine for line drawing inline.

Methods ("member functions") are similar to functions, they belongs to classes or objects and usually expresses the verbs of the objects/class. For example, an object of type Window usually would have methods open and close which do corresponding operations to the object they belong.

Properties are as in everyday language and technically are fields of objects/classes with dedicated getter/setter routines (which can be considered as methods. There are languages that don't have properties and this behavior is achieved using a private field+get/set methods.).

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

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