有人能简单解释OOP环境中的方法与函数吗?


当前回答

面向对象范例背后的思想是“处理”软件由。。好的“对象”。现实世界中的对象具有财产,例如,如果您有一个Employee,则该员工具有姓名、员工id、职位、他属于某个部门等。

对象还知道如何处理其属性并对其执行一些操作。比方说,如果我们想知道员工现在在做什么,我们会问他。

employe whatAreYouDoing.

“whatAreYouDoing”是发送给对象的“消息”。对象知道如何回答这些问题,据说它有解决问题的“方法”。

因此,对象必须公开其行为的方式称为方法。因此,方法是工件对象必须“做”的事情。

其他可能的方法有

employee whatIsYourName
employee whatIsYourDepartmentsName

etc.

另一方面,函数是编程语言计算某些数据的方式,例如,您可以使用返回16的函数addValues(8,8)

// pseudo-code
function addValues( int x, int y )  return x + y 
// call it 
result = addValues( 8,8 )
print result // output is 16...

由于第一批流行的编程语言(如fortran、c、pascal)没有涵盖OO范式,所以它们只调用这些工件“函数”。

例如,C中的先前函数将是:

int addValues( int x, int y ) 
{
   return x + y;
}

说一个对象有一个“函数”来执行某些操作是不“自然”的,因为函数更多地与数学相关,而雇员几乎没有数学知识,但你可以有与函数完全相同的方法,例如在Java中,这将是等效的addValues函数。

public static int addValues( int x, int y ) {
    return x + y;
}

看起来很眼熟?这是因为Java源于C++,而C++源于C。

最后只是一个概念,在实现中它们看起来可能是一样的,但在OO文档中,它们被称为方法。

下面是Java中以前的Employee对象的示例。

public class Employee {

    Department department;
    String name;

    public String whatsYourName(){
        return this.name;
    }
    public String whatsYourDeparmentsName(){
         return this.department.name();
    }
    public String whatAreYouDoing(){
        return "nothing";
    } 
    // Ignore the following, only set here for completness
    public Employee( String name ) {
        this.name = name;
    }

}

// Usage sample.
Employee employee = new Employee( "John" ); // Creates an employee called John

// If I want to display what is this employee doing I could use its methods.
// to know it.
String name = employee.whatIsYourName():
String doingWhat = employee.whatAreYouDoint();

// Print the info to the console.

 System.out.printf("Employee %s is doing: %s", name, doingWhat );

Output:
Employee John is doing nothing.

那么,区别就在于应用它的“领域”。

AppleScript有一种“自然语言”matphor的概念,在某种程度上OO也有这种想法。例如Smalltalk。我希望在阅读了这篇文章之后,您可以更容易地理解对象中的方法。

注意:代码不是要编译的,只是作为示例。请随意修改文章并添加Python示例。

其他回答

根据我的理解,方法是可以在类上执行的任何操作。它是编程中使用的通用术语。

在许多语言中,方法由函数和子程序表示。大多数语言用于这些的主要区别是,函数可能会返回一个值给调用者,而子程序可能不会。然而,许多现代语言只有函数,但这些函数可以选择不返回任何值。

例如,假设你想描述一只猫,你希望它能够打哈欠。您将使用Yawn方法创建一个Cat类,这很可能是一个没有任何返回值的函数。

方法位于对象上或在类中是静态的。函数独立于任何对象(并且在任何类之外)。

对于Java和C#,只有方法。对于C,只有函数。

对于C++和Python,这取决于您是否在类中。但在基本英语中:

功能:独立功能。方法:做某事的一种方式,它有不同的方法或方法,但与同一方面(也称为类)相关。

简单的记忆方式:

作用→ 免费(免费意味着它可以在任何地方,不需要在对象或类中)方法→ 成员(对象或类的成员)

面向对象范例背后的思想是“处理”软件由。。好的“对象”。现实世界中的对象具有财产,例如,如果您有一个Employee,则该员工具有姓名、员工id、职位、他属于某个部门等。

对象还知道如何处理其属性并对其执行一些操作。比方说,如果我们想知道员工现在在做什么,我们会问他。

employe whatAreYouDoing.

“whatAreYouDoing”是发送给对象的“消息”。对象知道如何回答这些问题,据说它有解决问题的“方法”。

因此,对象必须公开其行为的方式称为方法。因此,方法是工件对象必须“做”的事情。

其他可能的方法有

employee whatIsYourName
employee whatIsYourDepartmentsName

etc.

另一方面,函数是编程语言计算某些数据的方式,例如,您可以使用返回16的函数addValues(8,8)

// pseudo-code
function addValues( int x, int y )  return x + y 
// call it 
result = addValues( 8,8 )
print result // output is 16...

由于第一批流行的编程语言(如fortran、c、pascal)没有涵盖OO范式,所以它们只调用这些工件“函数”。

例如,C中的先前函数将是:

int addValues( int x, int y ) 
{
   return x + y;
}

说一个对象有一个“函数”来执行某些操作是不“自然”的,因为函数更多地与数学相关,而雇员几乎没有数学知识,但你可以有与函数完全相同的方法,例如在Java中,这将是等效的addValues函数。

public static int addValues( int x, int y ) {
    return x + y;
}

看起来很眼熟?这是因为Java源于C++,而C++源于C。

最后只是一个概念,在实现中它们看起来可能是一样的,但在OO文档中,它们被称为方法。

下面是Java中以前的Employee对象的示例。

public class Employee {

    Department department;
    String name;

    public String whatsYourName(){
        return this.name;
    }
    public String whatsYourDeparmentsName(){
         return this.department.name();
    }
    public String whatAreYouDoing(){
        return "nothing";
    } 
    // Ignore the following, only set here for completness
    public Employee( String name ) {
        this.name = name;
    }

}

// Usage sample.
Employee employee = new Employee( "John" ); // Creates an employee called John

// If I want to display what is this employee doing I could use its methods.
// to know it.
String name = employee.whatIsYourName():
String doingWhat = employee.whatAreYouDoint();

// Print the info to the console.

 System.out.printf("Employee %s is doing: %s", name, doingWhat );

Output:
Employee John is doing nothing.

那么,区别就在于应用它的“领域”。

AppleScript有一种“自然语言”matphor的概念,在某种程度上OO也有这种想法。例如Smalltalk。我希望在阅读了这篇文章之后,您可以更容易地理解对象中的方法。

注意:代码不是要编译的,只是作为示例。请随意修改文章并添加Python示例。

函数和方法看起来非常相似。它们也有输入和返回输出。区别在于方法在类内部,而函数在类外部。