静态方法和实例方法之间的区别 [英] Difference between Static methods and Instance methods

查看:164
本文介绍了静态方法和实例方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在阅读教科书中给我的文字,我不确定我是否理解它的含义。它基本上告诉我静态方法或类方法包含modifier关键字static。但我真的不知道这意味着什么?

I was just reading over the text given to me in my textbook and I'm not really sure I understand what it is saying. It's basically telling me that static methods or class methods include the "modifier" keyword static. But I don't really know what that means?

有人可以用非常简单的术语向我解释静态或类方法是什么吗?

Could someone please explain to me in really simple terms what Static or Class Methods are?

另外,我可以得到一个关于实例方法的简单解释吗?

Also, could I get a simple explanation on what Instance methods are?

这是他们在教科书中给我的:

This is what they give me in the textbook:


静态修饰符的存在与否存在重要的实际意义。只要Java处理它所属的类的定义,就可以调用并执行公共类方法。实例方法不是这种情况。在可以调用和执行公共实例方法之前,必须创建它所属的类的实例。要使用公共类方法,您只需要该类。另一方面,在使用公共实例方法之前,必须有一个类的实例。

There are important practical implications of the presence or absence of the static modifier. A public class method may be invoked and executed as soon as Java processes the definition of the class to which it belongs. That is not the case for an instance method. Before a public instance method may be invoked and executed, an instance must be created of the class to which it belongs. To use a public class method, you just need the class. On the other hand, before you can use a public instance method you must have an instance of the class.

在定义中调用静态方法的方式另一种方法根据两种方法是否属于同一类而不同。在上面的示例中,factorial和main都是MainClass类的方法。因此,在main的定义中调用factorial只是引用方法名称factorial。

The manner in which a static method is invoked within the definition of another method varies according to whether or not the two methods belong to the same class. In the example above, factorial and main are both methods of the MainClass class. As a result, the invocation of factorial in the definition of main simply references the method name, "factorial".


推荐答案

Java中的基本范例是您编写类,并且这些类是实例化的。实例化对象(类的实例)具有与它们相关联的属性(成员变量),这些属性会影响它们的行为;当实例执行其方法时,它将引用这些变量。

The basic paradigm in Java is that you write classes, and that those classes are instantiated. Instantiated objects (an instance of a class) have attributes associated with them (member variables) that affect their behavior; when the instance has its method executed it will refer to these variables.

但是,特定类型的所有对象可能具有完全不依赖于成员变量的行为;这些方法最好是静态的。通过静态,运行该方法不需要该类的实例。

However, all objects of a particular type might have behavior that is not dependent at all on member variables; these methods are best made static. By being static, no instance of the class is required to run the method.

您可以执行此操作来执行静态方法:

You can do this to execute a static method:

MyObject.staticMethod();//Simply refers to the class's static code

但是要执行非静态方法,您必须这样做:

But to execute a non-static method, you must do this:

MyObject obj = new MyObject();//Create an instance
obj.nonstaticMethod();//Refer to the instance's class's code

在更深层次上,当编译器将类放在一起时,它包含几个指向方法的指针。当执行这些方法时,它遵循指针并执行远端的代码。如果实例化类,则创建的对象包含指向虚方法表的指针,该指针指向要在继承层次结构中为该特定类调用的方法。但是,如果方法是静态的,则不需要虚方法表:对该方法的所有调用都会到达内存中完全相同的位置以执行完全相同的代码。因此,在高性能系统中,如果不依赖实例变量,最好使用静态方法。

On a deeper level, when the compiler puts a class together, it contains several pointers to methods. When those methods are executed it follows the pointers and executes the code at the far end. If a class is instantiated, the created object contains a pointer to the "virtual method table", which points to the methods to be called for that particular class in the inheritance hierarchy. However, if the method is static, no "virtual method table" is needed: all calls to that method go to the exact same place in memory to execute the exact same code. For that reason, in high-performance systems it's better to use a static method if you are not reliant on instance variables.

这篇关于静态方法和实例方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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