“静态”是什么意思? “导入”后的修饰符 意思是?

发布于 2024-07-07 07:18:38 字数 206 浏览 8 评论 0原文

当这样使用时:

import static com.showboy.Myclass;

public class Anotherclass{}

import static com.showboy.Myclassimport com.showboy.Myclass有什么区别?

When used like this:

import static com.showboy.Myclass;

public class Anotherclass{}

what's the difference between import static com.showboy.Myclass and import com.showboy.Myclass?

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

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

发布评论

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

评论(8

靖瑶 2024-07-14 07:18:38

请参阅文档

静态导入声明是
与正常导入类似
宣言。 哪里可以正常导入
声明从以下位置导入类
包,允许使用它们
没有包装资质的,
静态导入声明导入
类中的静态成员,允许
它们无需类即可使用
资质。

那么什么时候应该使用静态导入呢?
非常节俭! 仅当您需要时才使用它
否则很想声明本地
常量的副本,或滥用
继承(常量接口
反模式)。 换句话说,使用它
当您需要频繁访问时
来自一两个的静态成员
类。 如果你过度使用静态
导入功能,它可以让您
程序不可读且不可维护,
用所有的污染它的命名空间
您导入的静态成员。 读者
你的代码(包括你,几个月
你写完之后)不会知道
静态成员来自哪个类
从。 导入所有静态
一个类的成员可以是
特别损害可读性;
如果您只需要一两个成员,
单独导入它们。 用过的
适当地,静态导入可以使
你的程序更具可读性,通过
删除重复的样板
类名。

See Documentation

The static import declaration is
analogous to the normal import
declaration. Where the normal import
declaration imports classes from
packages, allowing them to be used
without package qualification, the
static import declaration imports
static members from classes, allowing
them to be used without class
qualification.

So when should you use static import?
Very sparingly! Only use it when you'd
otherwise be tempted to declare local
copies of constants, or to abuse
inheritance (the Constant Interface
Antipattern). In other words, use it
when you require frequent access to
static members from one or two
classes. If you overuse the static
import feature, it can make your
program unreadable and unmaintainable,
polluting its namespace with all the
static members you import. Readers of
your code (including you, a few months
after you wrote it) will not know
which class a static member comes
from. Importing all of the static
members from a class can be
particularly harmful to readability;
if you need only one or two members,
import them individually. Used
appropriately, static import can make
your program more readable, by
removing the boilerplate of repetition
of class names.

滿滿的愛 2024-07-14 07:18:38

您所说的这两种进口之间没有区别。 但是,您可以使用静态导入来允许对其他类的静态成员进行非限定访问。 我曾经必须这样做的地方:

import org.apache.commons.lang.StringUtils;
      .
      .
      .
if (StringUtils.isBlank(aString)) {
      .
      .
      .

我可以这样做:

import static org.apache.commons.lang.StringUtils.isBlank;
      .
      .
      .
if (isBlank(aString)) {
      .
      .
      .

您可以在 文档

There is no difference between those two imports you state. You can, however, use the static import to allow unqualified access to static members of other classes. Where I used to have to do this:

import org.apache.commons.lang.StringUtils;
      .
      .
      .
if (StringUtils.isBlank(aString)) {
      .
      .
      .

I can do this:

import static org.apache.commons.lang.StringUtils.isBlank;
      .
      .
      .
if (isBlank(aString)) {
      .
      .
      .

You can see more in the documentation.

不回头走下去 2024-07-14 07:18:38

静态导入用于导入类的静态字段/方法,而不是:

package test;

import org.example.Foo;

class A {

 B b = Foo.B_INSTANCE;

}

您可以编写:

package test;

import static org.example.Foo.B_INSTANCE;

class A {

 B b = B_INSTANCE;

}

如果您经常在代码中使用来自另一个类的常量并且静态导入明确,那么它会很有用。

顺便说一句,在您的示例中“import static org.example.Myclass;” 不起作用: import 用于类, import static 用于类的静态成员。

Static import is used to import static fields / method of a class instead of:

package test;

import org.example.Foo;

class A {

 B b = Foo.B_INSTANCE;

}

You can write :

package test;

import static org.example.Foo.B_INSTANCE;

class A {

 B b = B_INSTANCE;

}

It is useful if you are often used a constant from another class in your code and if the static import is not ambiguous.

Btw, in your example "import static org.example.Myclass;" won't work : import is for class, import static is for static members of a class.

晨光如昨 2024-07-14 07:18:38

静态导入的基本思想是,每当您使用静态类、静态变量或枚举时,您都可以导入它们并节省一些输入。

我将用例子来阐述我的观点。

import java.lang.Math;

class WithoutStaticImports {

 public static void main(String [] args) {
  System.out.println("round " + Math.round(1032.897));
  System.out.println("min " + Math.min(60,102));
 }
}

相同的代码,具有静态导入:

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

class WithStaticImports {
  public static void main(String [] args) {
    out.println("round " + round(1032.897));
    out.println("min " + min(60,102));
  }
}

注意:静态导入可能会使您的代码难以阅读。

The basic idea of static import is that whenever you are using a static class,a static variable or an enum,you can import them and save yourself from some typing.

I will elaborate my point with example.

import java.lang.Math;

class WithoutStaticImports {

 public static void main(String [] args) {
  System.out.println("round " + Math.round(1032.897));
  System.out.println("min " + Math.min(60,102));
 }
}

Same code, with static imports:

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

class WithStaticImports {
  public static void main(String [] args) {
    out.println("round " + round(1032.897));
    out.println("min " + min(60,102));
  }
}

Note: static import can make your code confusing to read.

━╋う一瞬間旳綻放 2024-07-14 07:18:38

“import static com.showboy.Myclass”和“import com.showboy.Myclass”之间的区别?

第一个应该生成编译器错误,因为静态导入仅适用于导入字段或成员类型。 (假设 MyClass 不是内部类或来自 showboy 的成员)

我认为您的意思

import static com.showboy.MyClass.*;

是使 MyClass 中的所有静态字段和成员在实际编译单元中可用,而无需限定它们...如上所述

the difference between "import static com.showboy.Myclass" and "import com.showboy.Myclass"?

The first should generate a compiler error since the static import only works for importing fields or member types. (assuming MyClass is not an inner class or member from showboy)

I think you meant

import static com.showboy.MyClass.*;

which makes all static fields and members from MyClass available in the actual compilation unit without having to qualify them... as explained above

☆獨立☆ 2024-07-14 07:18:38

import 允许 java 程序员无需包限定即可访问包的类。

静态导入功能允许在没有类限定的情况下访问类的静态成员。

import 提供对类和接口的可访问性,而static import 则提供对类的静态成员的可访问性。

示例:

使用导入

import java.lang.System.*;    
class StaticImportExample{  
    public static void main(String args[]){  

       System.out.println("Hello");
       System.out.println("Java");  

  }   
} 

使用静态导入

import static java.lang.System.*;    
class StaticImportExample{  
  public static void main(String args[]){  

   out.println("Hello");//Now no need of System.out  
   out.println("Java");  

 }   
} 

另请参阅:什么是 Java 5 中的静态导入

The import allows the java programmer to access classes of a package without package qualification.

The static import feature allows to access the static members of a class without the class qualification.

The import provides accessibility to classes and interface whereas static import provides accessibility to static members of the class.

Example :

With import

import java.lang.System.*;    
class StaticImportExample{  
    public static void main(String args[]){  

       System.out.println("Hello");
       System.out.println("Java");  

  }   
} 

With static import

import static java.lang.System.*;    
class StaticImportExample{  
  public static void main(String args[]){  

   out.println("Hello");//Now no need of System.out  
   out.println("Java");  

 }   
} 

See also : What is static import in Java 5

樱桃奶球 2024-07-14 07:18:38

假设您在名为 myPackage 的包内名为 MyClass 的类中有静态字段和方法,并且您希望通过键入 myStaticField 或 < code>myStaticMethod,无需每次都键入 MyClass.myStaticFieldMyClass.myStaticMethod

注意:你需要做一个
导入 myPackage.MyClassmyPackage.*
用于访问其他资源

Say you have static fields and methods inside a class called MyClass inside a package called myPackage and you want to access them directly by typing myStaticField or myStaticMethod without typing each time MyClass.myStaticField or MyClass.myStaticMethod.

Note : you need to do an
import myPackage.MyClass or myPackage.*
for accessing the other resources

北陌 2024-07-14 07:18:38

import 之后的 static 修饰符用于检索/使用类的静态字段。 我使用 import static 的一个领域是从类中检索常量。
我们还可以在静态方法上应用import static。 确保输入 import static,因为 static import 是错误的。

什么是 Java 中的 static import - JavaRevisited - 一个非常好的资源,可以了解更多关于 import static 的信息。

The static modifier after import is for retrieving/using static fields of a class. One area in which I use import static is for retrieving constants from a class.
We can also apply import static on static methods. Make sure to type import static because static import is wrong.

What is static import in Java - JavaRevisited - A very good resource to know more about import static.

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