JAVA:正确访问静态方法 [英] JAVA : Accessing static method properly

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

问题描述

我是JAVA的新手,我喜欢尝试理解一切。

I am new to JAVA, and I like to try and understand everything.

在JAVA中访问静态方法hero.returnHp()时,我有以下内容:

When accessing a static method "hero.returnHp()" in JAVA, I have the following:

 hero Mike = new hero();

 Mike.returnHp();

程序运行正常,但我注意到Eclipse有一个警告声明,静态方法来自应该以静态方式访问类型英雄。当我接受自动修复时,它会更改Mike.returnHp(); tohero.returnHp();。

The program runs fine, but I notice that Eclipse has a warning stating, "The static method from the type hero should be accessed in a static way." When I accept the auto-fix, it changes "Mike.returnHp();" to "hero.returnHp();".

所以我有两个问题:

1)这有什么好处?

1) What is the advantage of this?

2)如果我创建了两个相同类型的对象,那么在以静态方式访问时如何指定返回哪个对象?

2) If I created two objects of the same type, how would I specify which one to return when accessing in a static way?

谢谢!

推荐答案

我首先想指出一下关键字静态意味着。

I would first like to point out what the keyword static means.

静态变量每个类只存在一次 - 也就是说,如果您创建一个带有静态变量的类,那么所有实例该类将共享该一个变量。此外,如果它是一个公共静态变量,那么任何人都可以访问该变量而无需首先创建该类的实例 - 他们只需调用Hero.staticVariableName;

Static variables only exist once per class – that is, if you create a class with a static variable then all instances of that class will share that one variable. Furthermore, if it’s a public static variable, then anyone can access the variable without having to first create an instance of that class – they just call Hero.staticVariableName;

静态方法/函数是无状态的。也就是说,它们仅作用于传递给方法/函数的参数提供的信息(1),或者(2)静态变量(上面命名),或者(3)硬编码到方法/函数中(例如,创建一个静态函数以返回hello - 然后将hello硬编码到函数中。)

Static method/functions are stateless. That is, they act only on information (1) provided by arguments passed to the method/function, or (2) in static variables (named above), or (3) that is hard-coded into the method/function (e.g. you create a static function to return "hello" – then "hello" is hard-coded into the function).

Eclipse希望您访问静态方法的原因静态方式是因为它允许您和后续程序员看到您访问的方法是静态的(这有助于防止错误)。该函数将以您执行的任一方式运行,但正确的方法是以静态方式访问静态函数。请记住,如果你调用静态方法,无论你从哪个实例变量调用它(Tim.returnHp,Jim.returnHp,Mike.returnHp,无论如何),你都会从英雄类中调用相同的函数,你会看到完全相同的行为,无论你从谁那里调用它。

The reason why Eclipse wants you to access static methods in a static way is because it lets you and subsequent programmers see that the method you’re accessing is static (this helps to prevent mistakes). The function will run either way you do it, but the correct way to do it is to access static functions in a static way. Remember that if you call a static method, no matter what instance variable you call it from (Tim.returnHp, Jim.returnHp, Mike.returnHp, whatever) you will call the same function from the hero class and you will see the exact same behavior no matter who you call it from.

如果你创建了两个相同类型的对象,那么你不能指定在以静态方式访问时要返回的对象;静态函数/方法将引用整个Hero类。

If you created two objects of the same type then you COULD NOT specify which one to return when accessing in a static way; static functions/methods will refer to the entire Hero class.

您能解释一下您正在尝试做什么,以便我们能够提供更具体的反馈吗? returnHp()很可能不是静态的。

Can you explain what you’re trying to do so that we can offer more specific feedback? It’s quite possible that returnHp() shouldn’t be static.

这是回归生命值吗?如果是,那么你不希望它是静态的,因为英雄拥有的生命值的数量是英雄状态的一部分,静态方法是无状态的。 (想想状态就像现在的情况 - 活着,死了,受伤,攻击,防守,前面提到的一些组合等等)我建议进入Hero类并将returnHp改为非静态方法。

Is that "return hit points"? If it is, then you do NOT want it static because the number of hit points that a hero has is part of the hero’s state, and static methods are stateless. (Think of state like the current condition – alive, dead, wounded, attacking, defending, some combination of the aforementioned, etc.) I would recommend going into the Hero class and changing returnHp to a non-static method.

现在......我知道你没有问,但我想告诉你一些事情:

Now… I know you didn’t ask, but I would like to advise you of something:

班级名称(例如作为英雄)应该大写。实例变量名称(例如mike)应为小写。这是一个被广泛接受的命名约定,它将提高代码的可读性。

Class names (such as Hero) should be capitalized. Instance variable names (such as mike) should be lowercase. This is a widely accepted naming convention and it will increase the readability of your code.

Jeff

这篇关于JAVA:正确访问静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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