当前位置: 技术问答>java相关
高手请进,有关打包引用的问题!
来源: 互联网 发布时间:2015-06-25
本文导语: 我写了这么一个类.如下: package showMsg; public class showMessage { showMessage(String Mess) { showMess=Mess; } public void setMessage(String messNew) { showMess=messNew; } public String getMessage() { return s...
我写了这么一个类.如下:
package showMsg;
public class showMessage
{
showMessage(String Mess)
{
showMess=Mess;
}
public void setMessage(String messNew)
{
showMess=messNew;
}
public String getMessage()
{
return showMess;
}
public static void main(String[] args)
{
showMessage test=new showMessage("this is showMessage example");
System.out.println(test.getMessage());
}
private String showMess;
}
我编译并运行过一切正常,然后我就用jar把它打包,命令如下:
d:lzx>jar cvf showMsg.jar showMsg*.class
(showMsg是lzx的一个子目录,showMessage.class在showMsg目录下.)
也正常通过,用jar可以显示内容.如下:
d:lzx>jar tvf showMsg.jar
0 Tue Jun 18 16:35:00 CST 2002 META-INF/
71 Tue Jun 18 16:35:00 CST 2002 META-INF/MANIFEST.MF
690 Tue Jun 18 16:33:26 CST 2002 showMsg/showMessage.class
我将此jar文件拷贝到jdkjrelibext子目录下,
我写了一个很小的类来测试这个包,程序如下:
import showMsg.*;
public class testShow
{
public static void main(String[] args)
{
showMessage test=new showMessage("this is package exmpale");
System.out.println(test.getMessage());
}
}
但编译时产生错误,如下所示:
testShow.java:6: showMessage(java.lang.String) is not public in showMsg.showMessage; cannot be accessed from outside package
showMessage test=new showMessage("this is package exmpale");
^
1 error
我感觉很怪,怎么会产生这种错误呢?请高手指教.
package showMsg;
public class showMessage
{
showMessage(String Mess)
{
showMess=Mess;
}
public void setMessage(String messNew)
{
showMess=messNew;
}
public String getMessage()
{
return showMess;
}
public static void main(String[] args)
{
showMessage test=new showMessage("this is showMessage example");
System.out.println(test.getMessage());
}
private String showMess;
}
我编译并运行过一切正常,然后我就用jar把它打包,命令如下:
d:lzx>jar cvf showMsg.jar showMsg*.class
(showMsg是lzx的一个子目录,showMessage.class在showMsg目录下.)
也正常通过,用jar可以显示内容.如下:
d:lzx>jar tvf showMsg.jar
0 Tue Jun 18 16:35:00 CST 2002 META-INF/
71 Tue Jun 18 16:35:00 CST 2002 META-INF/MANIFEST.MF
690 Tue Jun 18 16:33:26 CST 2002 showMsg/showMessage.class
我将此jar文件拷贝到jdkjrelibext子目录下,
我写了一个很小的类来测试这个包,程序如下:
import showMsg.*;
public class testShow
{
public static void main(String[] args)
{
showMessage test=new showMessage("this is package exmpale");
System.out.println(test.getMessage());
}
}
但编译时产生错误,如下所示:
testShow.java:6: showMessage(java.lang.String) is not public in showMsg.showMessage; cannot be accessed from outside package
showMessage test=new showMessage("this is package exmpale");
^
1 error
我感觉很怪,怎么会产生这种错误呢?请高手指教.
|
你的构造函数没有声明public 。默认是protected
所以包外面不能访问。
showMessage(String Mess)
{
showMess=Mess;
}
所以包外面不能访问。
showMessage(String Mess)
{
showMess=Mess;
}
|
同意楼上的,因为缺省的构造函数是protected,它只能被自已的派生类访问;
所以你必须把它设成public就行了!
public showMessage(String Mess)
{
showMess=Mess;
所以你必须把它设成public就行了!
public showMessage(String Mess)
{
showMess=Mess;
|
同意楼上;
因为缺省的类说明是protected的,所以它只能被自已的派生类访问,
你必须把它设成public 就行了!
public showMessage(String Mess)
{
showMess=Mess;
}
因为缺省的类说明是protected的,所以它只能被自已的派生类访问,
你必须把它设成public 就行了!
public showMessage(String Mess)
{
showMess=Mess;
}
|
补充一点,如果你继承的话当然是可以的。