Java作业帮助(通过实例引用访问静态成员)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18344972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Homework Help (Accessing Static Member via Instance Reference)
提问by Bit Deception
Here is my homework question:
这是我的作业问题:
Write a class declaration for a class “Clock”. It should have instance variables for hours, minutes, seconds (all integers). It should also have a toString() method to show the time in the format shown below. Write a separate “ClockDriver” class to a) create an instance of a clock, b) set the hours, minutes, and seconds for the clock, and c) show the time of the Clock using getTime(). Use the Dog class example on page 36 as a guide. Sample out is shown below:
为“时钟”类编写类声明。它应该有小时、分钟、秒(所有整数)的实例变量。它还应该有一个 toString() 方法以如下所示的格式显示时间。编写一个单独的“ClockDriver”类来 a) 创建一个时钟实例,b) 设置时钟的小时、分钟和秒,以及 c) 使用 getTime() 显示时钟的时间。使用第 36 页上的 Dog 类示例作为指导。样例如下图所示:
The time is 3:45:00
时间是3:45:00
// don't worry if you can't get both zeros in
// 如果你不能同时得到两个零也不要担心
// the second field. That is a formatting issue
// 第二个字段。那是格式问题
// we will deal with later
// 我们稍后会处理
Here is my Clock class:
这是我的时钟课:
class Clock {
int hours;
int minutes;
int seconds;
public String toString() {
String temp = ("");
return temp.format("%02d:%02d:%02d", hours, minutes, seconds);
} //end method toString
public void getTime() {
System.out.print("The time is " + toString());
} //end method getTime
} //end class Clock
And here is my ClockDriver class:
这是我的 ClockDriver 类:
public class ClockDriver {
public static void main (String[] args) {
Clock c = new Clock();
c.hours = 4;
c.minutes = 30;
c.seconds = 00;
c.getTime();
} //end main
} //end class ClockDriver
Even though it compiles fine and works nicely, I get what I think is a warning from IDE saying that my
尽管它编译得很好并且运行良好,但我收到了我认为来自 IDE 的警告,说我的
return temp.format("%02d:%02d:%02d", hours, minutes, seconds);
line is accessing a static member via instance reference. Specifically, the
line 正在通过实例引用访问静态成员。具体来说,
temp.format
bit.
少量。
So my questions are:
所以我的问题是:
1.) Why is accessing a static member via instance reference not necessarily encouraged?
1.) 为什么不一定鼓励通过实例引用访问静态成员?
2.) Is there a better way to put this together so that I'm not accessing a static member via instance reference?
2.) 有没有更好的方法将它们放在一起,这样我就不会通过实例引用访问静态成员?
Thanks in advance!
提前致谢!
采纳答案by rgettman
Static methods belong to the class itself, not any instance. While you can call a static
method from an instance of the class, you don't have to use an instance of the class, and you should not. It can be confusing, because it looks like the method is not static
, even though it is static
.
静态方法属于类本身,而不是任何实例。虽然您可以static
从类的实例调用方法,但您不必使用类的实例,也不应该使用。它可能会令人困惑,因为看起来方法不是static
,即使它是static
。
The best and clearest way to call a static
method is to use the class name itself, not an instance of the class, to call the method:
调用static
方法的最好和最清晰的方法是使用类名本身,而不是类的实例来调用方法:
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
And you don't need the temp
instance at all.
而且您根本不需要该temp
实例。
回答by tbodt
You should actually not use the string temp
. You should call it as String.format
. To answer your question, it is confusing, since the call actually doesn't look at temp
.
您实际上不应该使用 string temp
。您应该将其称为String.format
. 要回答您的问题,这很令人困惑,因为该电话实际上并未查看temp
.
回答by TomF
Keep in mind that if an instance of a certain Class, lets call it c, has a function, void foo()
, and you use the instance c
to call foo
like so:
请记住,如果某个类的实例,我们称之为 c,有一个函数,void foo()
,并且您使用该实例c
来调用,foo
如下所示:
c.foo();
c.foo();
what actually called is:
实际调用的是:
foo(c);
foo(c);
while calling a static function void foo2():
在调用静态函数 void foo2() 时:
Class.foo2();
Class.foo2();
is called just as it looks.
就像它看起来的那样被调用。
Calling a static function using an instance indicates a user that probably is not sure what he is doing.
使用实例调用静态函数表明用户可能不确定他在做什么。