English 中文(简体)
静态关键字在进口 Java. Lang. System 类中扮演什么角色?
原标题:What is the role of static keyword in importing java.lang.System class?

当我导入 System 类时,我不明白关键词 stistic 的含义:

import static java.lang.System.*

我在读关于爪哇的书 书写在那里:

Any import declaration that doesn t use the word static must start with the name of a package and must end with either of the following:

  • The name of a class within that package
  • An asterisk (indicating all classes within that package)

For example, the declaration import java.util.Scanner; is valid because java.util is the name of a package in the Java API, and Scanner is the name of a class in the java.util package.

Here’s another example. The declaration import javax.swing.*; is valid because javax.swing is the name of a package in the Java API, and the asterisk refers to all classes in the javax.swing package.

我有以下代码:

public class Addition {

   public static void main(String[] args) {
      double num;
      num = 100.53;

      num = num + 1000;

      // So when I want to import the whole package java.lang as written in the book, it doesn t work:
      // import java.lang.*;
      // or like this:
      // import static java.lang.*;
      // NetBeans in both cases doesn t see these abbreviated names `out` and throws errors. Why?
      out.print("The result is ");
      out.print(num);
      out.println(" .");
   }
}

当我这样进口的时候,它就起作用了:

import static java.lang.System.out;
import static java.lang.System.*

但当我尝试这样做时却不管用:

import java.lang.System.out;
import java.lang.System.*

在此特定情况下, stistic 关键字的含义是什么?

为什么 import java.lang.*; 不导入整个包件, 包括 System 类?

最佳回答

stistic 导入允许您写入 :

out.print("The result is ");

而不是此:

System.out.print("The result is ");

例如,见

问题回答

我经常在我的单位测试中使用静态进口, 就像这样:

import static org.junit.Assert.*;

这让我可以写出这个代码:

assertEquals(2, list.size());

代替此代码的 :

Assert.assertEquals(2, list.size());

静态导入是爪哇编程语言中引入的一个特征,它允许在爪哇代码中被定义为公共静态用户类的成员(字段和方法)使用爪哇代码,而没有具体说明字段定义的类别。这个特征被引入了5.0版本的语言中。

特性提供了一个类型安全机制, 将常数包含在代码中, 无需引用最初定义字段的类别。 它还有助于淡化创建常数界面的做法: 仅定义常数的界面, 然后写入执行该界面的分类, 认为该界面不适当地使用 。

当您用静态关键字导入时,它意味着您刚刚将它插入到您的班级中,您可以使用它的方法与您重新称呼自己的班级方法相同。

例如:

import static java.lang.Math.*;
import static java.lang.System.out;

和:

out.println("I have a house with an area of " + (PI * pow(2.5,2)) + " sq. cm");

<强问:

<% 1> 在我以这种方式导入时有效 :

a. import static java.lang.System.out;
b. import static java.lang.System.*

但当我尝试这样做时却不管用:

c. import java.lang.System.out;
d. import java.lang.System.*;

2 在这个特定情况下静态关键字的含义是什么?

<% 1> 3> 和为什么进口 java. lang. * ; 是否导入了带有系统级的整套软件包?

--------------------------------------------------------------------------------

<强 > 答复:

1] & amp; 2) 静态进口 ,例如(进口静态 java.lang.System.out) 用于从系统类中导入其他类别中被宣布为 stistic 的方法或字段。

a. import static java.lang.System.out; //Works because "out" is a static field
b. import static java.lang.System.*; //Works because you are importing ALL static fields and methods inside the System class

c. import java.lang.System.out; //Does NOT work because you are trying to import a static field in a non-static way. (see a.)
d. import java.lang.System.*; //Actually Works because of the * wildcard which allows you to include all imports.

您想要以静态方式导入方法或字段的主要原因是,您可以省略所有调用到这些方法或字段的类规格。 因此, 而不是写入 :

System.out.print("Hello");
System.out.print("World");

仅写

import static java.lang.System.*   //or   import static java.lang.System.out if you only plan on using the  out  field.
out.print("Hello");
out.print("World");

< 坚固 > 3) < /坚固 > 进口 java. lang. * 是多余的! < 坚固 > Java 自动并隐含地为您导入这个软件包!! : < 强/ 强/ 强/ 强/ 强/ 强/ 并且是, 它以它进口系统类, 但不要混淆, 除非您将它作为静态进口导入, 您仍然需要写出很长的路程 :

System.out.print("hi");




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...