为什么静态方法被认为是一种方法? [英] Why is a static method considered a method?

查看:180
本文介绍了为什么静态方法被认为是一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写课程代码的解释,并且意外地使用了方法函数可互换。我决定回过头来修复措辞,但在我的理解中遇到了一个漏洞。



根据我的理解,子程序是函数如果它不作用于类的实例(其效果仅限于其显式输入/输出),并且是方法如果它在一个类的实例上运行(它可能会对实例产生副作用,使其不纯)。



有一个关于该主题的良好讨论。请注意,根据接受的答案的定义,静态方法实际上应该是一个函数,因为实例永远不会被隐式传递,并且它无权访问任何实例的成员。 / p>

尽管如此,不应该静态方法实际上是函数吗?



根据他们的定义,他们不会对某一类的特定情况采取行动;由于关系,他们只与班级捆绑。我已经看到一些好看的网站,它们将静态子程序称为方法( Oracle Fredosaurus ProgrammingSimplified ),所以他们要么是都忽略了术语,或者我遗漏了一些东西(我的猜测是后者)。



我想确保使用正确的措辞。 >
有人可以解决这个问题吗?

解决方案

来自 8.4.3.2 的引用可能有所帮助:


声明为 static 的方法称为类已满足hod



未声明 static 的方法称为实例方法 [...]。





  • 类方法:与类关联。

  • 实例方法:与实例相关联。



Java只是希望你思考面向对象 。此外,静态方法可以访问可能包含状态的周围范围。在某种程度上,这个类就像一个对象本身。


I'm writing an explanation for some code for a course, and have been accidentally using the words method and function interchangeably. I decided to go back over and fix the wording, but ran into a hole in my understanding.

From what I understand, a subroutine is a function if it doesn't act on an instance of a class (its effect is restricted to its explicit input/output), and is a method if it operates on an instance of a class (it may carry out side effects on the instance that make it impure).

There's a good discussion here on the topic. Note that by the accepted answer's definitions, a static method should actually be a function because an instance is never implicitly passed, and it doesn't have access to any instance's members.

With this is mind though, shouldn't static methods actually be functions?

By their definition they don't act on particular instances of a class; they're only "tied" to the class because of relation. I've seen a few good looking sites that refer to static subroutines as "methods" though (Oracle, Fredosaurus, ProgrammingSimplified), so either they're all overlooking the terminology, or I'm missing something (my guess is the latter).

I'd like to make sure I am using the correct wording.
Can anybody clear this up?

解决方案

This quote from 8.4.3.2 may help:

A method that is declared static is called a class method.

A method that is not declared static is called an instance method [...].

  • Class methods: associated with a class.
  • Instance methods: associated with an instance.

Java just wants you to "think object-oriented". Also, static methods have access to a surrounding scope which may include state. In a way, the class is like an object itself.

这篇关于为什么静态方法被认为是一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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