这部分代码是什么

发布于 2024-11-09 10:31:19 字数 1128 浏览 0 评论 0原文

我有一些代码演示了通过使用 Runnable 接口来使用线程。 我从某个网站上的代码开始,然后根据自己的喜好进行修改。它有效,但我不明白其中的一部分。 我试图将代码简化为我所要求的本质,但我可能删除了太多内容。我在 NetBeans 中的代码可以工作,所以这是可以工作的代码,除非我因为删除了错误的内容而搞砸了它。 但让我问我的问题,看看是否可以回答: 我不明白的部分是这一部分:

public String toString()
       {
return "Thread " + Thread.currentThread().getName() + ": " + countDown;
       }

在最长的时间里,这在我看来就像一个成员变量,其名称在运行时动态设置为等于当前线程的名称。但我也在不止一处读到过,你不能在 Java 中动态命名变量,所以我想这不是我所关注的。 然后,我意识到 NetBeans 希望我将 @Override 放在此代码部分之前,因为某些内容正在被覆盖。但我不明白到底什么被覆盖。我是否重写了某些父类的构造函数?如果是的话,是什么班级?

无论如何,这是代码:

package countdown;

public class Counter implements Runnable
{
private int countDown = 5;
public String toString()
       {
return "Thread " + Thread.currentThread().getName() + ": " + countDown;
       }


public void run()
    {
   while(true) {
  System.out.println(this);
  if(--countDown == 0)
                      {
return;
                      }
                }
    }
}
package countdown;

public class Main 
{

public static void main(String[] args)
    {
for(int i = 1; i <= 5; i++)
  new Thread(new Counter(), "" + i).start();
    }

}

I have some code that demonstrates the use of threads by the use of the Runnable interface.
I started with code off a website somewhere, and modified it to my liking. It works, but I don't understand part of it.
I tried to strip the code down to the essence of what I am asking, but I may have taken too much out. The code I have in NetBeans works, so this is working code, unless I messed it up by taking the wrong thing out.
But let me ask my question, and see if it can be answered regardless:
The part I don't understand is this part:

public String toString()
       {
return "Thread " + Thread.currentThread().getName() + ": " + countDown;
       }

For the longest time, this just looked to me like a member variable whose name is dynamically set at runtime equal to the name of the current thread. But I have also read in more than one place that you cannot dynamically name variables in Java, so I guess that isn't what I'm looking at.
Then, I realized that NetBeans wanted me to put @Override right before this code section, because something is being overridden. But I don't understand exactly what is being overridden. Am I overriding the constructor of some parent class? If so, what class?

Anyway, here is the code:

package countdown;

public class Counter implements Runnable
{
private int countDown = 5;
public String toString()
       {
return "Thread " + Thread.currentThread().getName() + ": " + countDown;
       }


public void run()
    {
   while(true) {
  System.out.println(this);
  if(--countDown == 0)
                      {
return;
                      }
                }
    }
}
package countdown;

public class Main 
{

public static void main(String[] args)
    {
for(int i = 1; i <= 5; i++)
  new Thread(new Counter(), "" + i).start();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯℡ 2024-11-16 10:31:19

每个类都实现toString(),因为它是由Object 定义的。然而,toString() 的默认实现很少是足够的。

对于您创建的最重要的类,您将需要重写toString()并提供您自己的字符串表示形式。幸运的是,这很容易做到。 toString( ) 方法具有以下一般形式:

String toString( )   

当在 System.out 中传递对象引用时,会自动调用对象的 toString() 方法.println() 方法。


@Override 充当读者的文档并在编译器中进行双重检查。
使用@Override 注释来指示一个方法正在重写基类中的另一个方法。

例如,请

@Override
public String toString()
       {
return "Thread " + Thread.currentThread().getName() + ": " + countDown;
       }

参阅:你什么时候使用Java的@Override注释以及为什么?

Every class implements toString( ) because it is defined by Object. However, the default implementation of toString( ) is seldom sufficient.

For most important classes that you create, you will want to override toString( ) and provide your own string representations. Fortunately, this is easy to do. The toString( ) method has this general form:

String toString( )   

The toString() method of an object gets invoked automatically, when an object reference is passed in the System.out.println() method.


@Override serves as documentation for the reader and a double check in the compiler.
Use the @Override annotation to indicate that a method is overriding another in the base class.

For e.g

@Override
public String toString()
       {
return "Thread " + Thread.currentThread().getName() + ": " + countDown;
       }

See : When do you use Java's @Override annotation and why?

仲春光 2024-11-16 10:31:19

好的,您正在重写 Object.toString() 方法,该方法始终返回指定类的一些字符串表示形式。它所做的只是组成一个字符串,就像

"Thread XXX: 5"

在 Java 中一样,您始终可以重写父类(非最终)方法; @Override 标签只是对工具的有用提示。但它并没有设置任何东西的名称:它创建一个新的匿名字符串,并返回其引用。

如果您在 Thread 上调用标准 toString(),您将得到一个类似“<#Thread 2348564>”的字符串或同样有用的东西;现在,对于本课程,您将获得有用的信息。

Okay, what's up there is that you are overriding the Object.toString() method, which always returns some string representation of the named class. All it's doing is composing a string like

"Thread XXX: 5"

In Java, you can always override a parent classes (non-final) methods; the @Override tag is just helpful hinting for tools. But it's not setting the name of anything: it creates a new, anonymous string, and returns its reference.

If you called standard toString() on Thread, you'd get a string like "<#Thread 2348564>" or something equally useful; now, for this class, you get useful information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文