java静态导入_Java子包和静态导入

java静态导入

In this tutorial, we will learn about sub-packages in Java and also about the concept of static import and how it is different from normal import keyword.

在本教程中,我们将学习Java中的子包,以及静态导入的概念以及它与常规import关键字的区别。

Java子包 (Subpackage in Java)

Package inside the package is called the subpackage. It should be created to categorize the package further.

包内的包称为子包。 应该创建它以对软件包进行进一步分类。

Let's take an example, Sun Microsystem has definded a package named java that contains many classes like System, String, Reader, Writer, Socket etc. These classes represent a particular group e.g. Reader and Writer classes are for Input/Output operation, Socket and ServerSocket classes are for networking etc and so on. So, Sun has subcategorized the java package into subpackages such as lang, net, io etc. and put the Input/Output related classes in io package, Server and ServerSocket classes in net packages and so on.

让我们举个例子,Sun Microsystem定义了一个名为java的包,其中包含许多类,例如System,String,Reader,Writer,Socket等。这些类表示一个特定的组,例如Reader和Writer类用于输入/输出操作,Socket和ServerSocket。类用于网络等。 因此,Sun将Java包细分为lang,net,io等子包,并将与Input / Output相关的类放在io包中,将Server和ServerSocket类放在net包中,等等。

Note: The standard of defining package is domain.company.package e.g. LearnJava.full.io

注意:定义包的标准是domain.company.package,例如LearnJava.full.io

例: (Example:)

In this example, we created a package LearnJava and a sub package corejava in the Simple.java file.

在此示例中,我们在Simple.java文件中创建了一个包LearnJava和一个子包corejava

package LearnJava.corejava;
class Simple{
  public static void main(String args[]){
   System.out.println("Hello from subpackage");
  }
}

To compile the class, we can use the same command that we used for package. Command is given below.

要编译该类,我们可以使用与包相同的命令。 命令如下。

javac -d . Simple.java

To run the class stored into the created sub package, we can use below command.

要运行存储在创建的子包中的类,我们可以使用以下命令。

java LearnJava.corejava.Simple

After successful compiling and executing, it will print the following output to the console.

成功编译并执行后,它将以下输出打印到控制台。

Hello from subpackage

你好从子包

用Java静态导入 (Static import in Java)

static import is a feature that expands the capabilities of import keyword. It is used to import static member of a class. We all know that static member are referred in association with its class name outside the class. Using static import, it is possible to refer to the static member directly without its class name. There are two general form of static import statement.

静态导入是一项功能,可扩展导入关键字的功能。 它用于导入类的静态成员。 我们都知道,静态成员是在类外与其类名相关联地引用的。 使用static import ,可以直接引用静态成员而不使用其类名。 静态导入语句有两种通用形式。

We can import single or multiple static members of any class. To import single static member we can use statement like the below.

我们可以导入任何类的单个或多个静态成员。 要导入单个静态成员,我们可以使用如下语句。

import static java.lang.Math.sqrt;   //importing static method sqrt of Math class

The second form of static import statement, imports all the static member of a class.

静态导入语句的第二种形式,导入类的所有静态成员。

import static java.lang.Math.*;   //importing all static member of Math class

不使用静态导入的示例 (Example without using static import)

This is alternate way to use static member of the class. In this case, we don’t need to use import statement rather we use direct qualified name of the class.

这是使用类的静态成员的另一种方法。 在这种情况下,我们不需要使用import语句,而是使用类的直接限定名称。

public class Test
{
    public static void main(String[] args)
    {
        System.out.println(Math.sqrt(144));
    }
}

12

12

使用静态导入的示例 (Example using static import)

In this example, we are using import statement to import static members of the class. Here, we are using * to import all the static members.

在此示例中,我们使用import语句导入类的静态成员。 在这里,我们使用*导入所有静态成员。

import static java.lang.Math.*;
public class Test
{
    public static void main(String[] args)
    {
        System.out.println(sqrt(144));
    }
}

12

12

翻译自: https://www.studytonight.com/java/subpackage-and-static-import.php

java静态导入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值