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

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

问题描述

我只是在阅读课本中给我的文字,我不确定我是否理解它在说什么.它基本上告诉我静态方法或类方法包括修饰符".关键字静态.但我真的不知道那是什么意思?

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:

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

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

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

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

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

On a deeper level the compiler, when it puts a class together, collects pointers to methods and attaches them to the class. 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天全站免登陆