如何用Java导入软件包 [英] How to import packages in Java

查看:95
本文介绍了如何用Java导入软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我对Java有一个疑问.
我在Java中有2个软件包.因为我已经将一个包装导入另一个包装
如下.


Hi All,
I have one doubt regarding Java.
I have 2 packages in java. In that i have imported one package into another
as follows.


package package1;
public class Base1{
public void Method1(){
System.out.println("Method1");
}
}

package package2;
import package1;
public class Base2{
public void Method2(){
System.out.println("Method2");
Base1 b1=new Base1();
b1.Method1();
}
}


import package2;
public class clsMain{
public static void main(String[] args){
Base2 b2=new Base2();
b2.Method2();
}
}





谁能告诉我这个
它显示了一些错误,如下所示
编译第二个文件Base2时,出现以下错误





Can any body tell me this
its showing some errors as follows
while compiling the second file Base2 the following error is coming

Base2.java:2: '.' expect
import Package1;



并且在编译clsMain文件时显示以下错误



and While compiling the clsMain file its showing following errors

clsMain.java:1: package package2 does not exist
import package.*;
^
.\Base21.java:2: '.' expected
import Package1;
             ^
Use.java:9: cannot access Base2
bad class file: .\Base2.java
file does not contain class Base2
Please remove or make sure it appears in
th.
                Base2 m = new Base2();



如果这有什么问题,您可以为这种情况提供代码吗?



If anything wrong is there in this can you give code for this scenario please

推荐答案

您不导入软件包.您将类导入包中.

因此,该格式为以下格式之一(方括号用来表示名称,而不是语法.)
You don''t import packages. You import classes in a package.

So the form is one of the following (brackets used to indicate a name not syntax.)
import {package}.*;
import {package}.{class name in package};


所以你会用


So you would use

import Package1.*;


还要注意,对于以后的问题,它有助于指定代码所在的文件.


Also note that for future questions it helps to specify the files in which your code is in.


有两种方法可以使用存储在包中的公共类.
a)声明完全合格的班级名称
...
There are 2 ways in order to use the public classes stored in package.
a) Declare the fully-qualified class name
...
world.HelloWorld helloWorld = new world.HelloWorld();
world.moon.HelloMoon helloMoon = new world.moon.HelloMoon();
String holeName = helloMoon.getHoleName();



b)使用导入"关键字



b) Use an "import" keyword

import world.*;  // we can call any public classes inside the world package
import world.moon.*;  // we can call any public classes inside the world.moon package
import java.util.*;  // import all public classes from java.util package



参考链接:-
http://www.jarticles.com/package/package_eng.html [使用包成员 [



Reference link :-
http://www.jarticles.com/package/package_eng.html[^]
Using Package Members[^]


这篇关于如何用Java导入软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆