问题描述
大家好,
我对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
推荐答案
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.
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导入软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!