Is there an alternative for System.out.println without having to use System?

huangapple go评论64阅读模式
英文:

Is there an alternative for System.out.println without having to use System?

问题

我目前需要创建一个名为 'System' 的类,但我无法使用 'System.out.println',因为 'System' 总是引用到该类。我该如何避免这种情况?

英文:

I am currently having to create a class called 'System'
and I can't put System.out.println because the System is always referring to the class.
How can I avoid this?

答案1

得分: 4

  • 使用完全限定名 java.lang.System
java.lang.System.out.println("Hello World!");

或者;

  • 静态导入 java.lang.System.out 并使用 out.println
// 文件顶部
import static java.lang.System.out;
...
out.println("Hello World!");

或者;

  • 将您的类重命名为其他名称,而不是 System。毫无疑问,您可以找到一个比这更好、更具体的名称。例如,如果您正在制作一个游戏,可以称之为 GameSystem。无论您正在制作什么,尝试将其附加到 System 以创建 XXXSystem
英文:

You can either:

  • use the fully qualified name java.lang.System

    java.lang.System.out.println("Hello World!");
    

    or;

  • statically import java.lang.System.out and use out.println:

    // at the top of the file
    import static java.lang.System.out;
    ...
    out.println("Hello World!");
    

    or;

  • Rename your class to something else, not System. Surely you can find a better, more specific name than that. For example, if you are making a game, call it GameSystem. Whatever you are making, try appending it to System to make XXXSystem.

答案2

得分: 1

这是如何创建自己版本的 System.out,完全不使用 System 类的示例代码:

PrintStream out = new PrintStream(new FileOutputStream(FileDescriptor.out), true);

在尝试其他答案中提供的选项之前,您应该首先探索这些选项(例如使用完全限定的类名 java.lang.System 或重命名您的 System 类)。

英文:

For completeness, here's how you can create your own version of System.out without using the System class at all:

PrintStream out = new PrintStream(new FileOutputStream(FileDescriptor.out), true);

You should explore the options given in other answers first (like use the fully qualified class name java.lang.System or rename your System class)

答案3

得分: 0

如果您正在使用Maven,请在您的pom.xml文件中添加以下依赖项:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

现在,您可以在您的类中添加@Slf4j注解,并使用log.info("我的消息")

有多个日志级别。您可以在这里了解更多关于该注解的信息。

英文:

If you're using Maven, add to your pom.xml the following dependency:

        &lt;dependency&gt;
            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
            &lt;artifactId&gt;lombok&lt;/artifactId&gt;
            &lt;optional&gt;true&lt;/optional&gt;
        &lt;/dependency&gt;

Now you can add the @Slf4j annotation to your class and use log.info(&quot;My message&quot;)

There are several levels of logging. Read more about the annotation here

答案4

得分: 0

我建议不要将你的类命名为那样,因为在Java中已经有一个名为System的类。或者,你可以添加一个静态导入或者使用Java类的完全限定名称。

静态导入
import static java.lang.System.;*

但是静态导入也可能存在歧义,所以我建议你将你的类从System重新命名为与你的项目有一些关联的其他名称。

英文:

I would suggest not to name your class like that, as already in java we have a class named System.
Alternatively, you can add a static import or use the fully qualified name of the Java class.

Static Import
import static java.lang.System.;*

But static imports can have ambiguity too, so I would suggest you to rename your class from System to something else, a bit related to your project.

huangapple
  • 本文由 发表于 2020年7月31日 20:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63192085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定