博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
import_Keyword
阅读量:3922 次
发布时间:2019-05-23

本文共 1828 字,大约阅读时间需要 6 分钟。

import Java Keyword with Examples

The import keyword makes one class or all classes in a package visible in the current Java source file. Imported classes can be referenced without the use of fully−qualified class names.

In simple words, if a class wants to use another class in the same package, the package name does not need to be used. Classes in the same package find each other.

Here, a class named Teacher is added to the com.javaguides.teacher package that already contains Course. The Teacher can then refer to the Course class without using the com.javaguides.teacher.

package com.javaguides.teacher;import com.javaguides.course.Course;public class Teacher {  List
courses = new ArrayList<>(); public void addCourse(Course course) { courses.add(course); }}

The Teacher class used one of the following techniques for referring to a class in a different package.

The fully qualified name of the class can be used. For example:
import com.javaguides.course.Course;

The package can be imported using the import keyword and the wildcard (*). For example:import com.javaguides.course.*;Few more examples are:import java.io.File;import java.net.*;

Note that many Java programmers use only specific import statements (no ‘*’) to avoid ambiguity when multiple packages contain classes of the same name.

Java static import

Java 5 introduced a new feature — static import — that can be used to import the static members of the imported package or class. You can use the static members of the imported package or class as if you have defined the static member in the current class.

Example:

import static java.lang.Math.PI;// class declaration and other memberspublic double area() {    return PI * radius * radius;}

You can also use wildcard character “*” to import all static members of a specified package of class.

转载地址:http://lbgrn.baihongyu.com/

你可能感兴趣的文章
python+opencv礼帽黑帽
查看>>
python链表反转
查看>>
c/c++查询M个数在N数组中出现的次数
查看>>
uva 147 - Dollars(动态规划--完全背包)
查看>>
uva 357 - Let Me Count The Ways(动态规划-注意dp初始化的问题)
查看>>
uva 562 - Dividing coins(注意判断条件,可以转换成01背包做)
查看>>
uva 10404 - Bachet's Game(DP)
查看>>
最优二叉搜索树
查看>>
hdu 1008 Elevator
查看>>
hdu 1005 Number Sequence(数学题目,好好看)
查看>>
zoj 2106 Tick and Tick(比较好的数学题目,代码特麻烦,注意精度)
查看>>
zoj 2107 Quoit Design(最近点对问题,好好思考,分治)
查看>>
zoj 2111 Starship Troopers(树形DP)
查看>>
vector 容器的使用方法
查看>>
hdu 1520 Anniversary party(基本树形DP)
查看>>
poj 1463 Strategic game(树形DP)
查看>>
poj 3342 Party at Hali-Bula(树形DP+判断方式是不是唯一)
查看>>
Problem 2129 子序列个数 (动态规划题目,注意模余的问题)
查看>>
fzu Problem 2138 久违的月赛之一
查看>>
poj 1947 Rebuilding Roads(树形DP)
查看>>