简体   繁体   English

构造函数Session(String,int)不可见

[英]The constructor Session(String,int) is not visible

I have a jar that contains a class file Session.class. 我有一个jar,其中包含类文件Session.class。 I did a import of all the jar and this class that I want to use. 我导入了所有要使用的jar和此类。 In my code as below: 在我的代码如下:

public AgentInfo[] getInfo(){
    try {   
        session = new Session(host,port);
    }
}

Here I am getting error 在这里我得到了错误

 The constructor Session(String,int) is not visible

That suggests that the Session(String, int) constructor isn't public. 这表明Session(String, int)构造函数不是公共的。 For example, it might be declared like this: 例如,可以这样声明:

Session(String host, int port)

instead of 代替

public Session(String host, int port)

(It might be protected or even final, of course.) (当然,它可能受到保护,甚至是最终的。)

If you can change Session and want to , you can make the constructor public. 如果可以更改Session 并希望更改 ,则可以将构造方法公开。 If you can't change it (eg it's a 3rd party class) you should look at what constructors are available, and also check for static methods returning Session , factory classes (eg SessionFactory ) etc. Generally the documentation is a good start to find out how you're meant to get hold of an instance of a class :) 如果你不能改变它(例如,它是一个第三方类),你应该看什么构造具备 ,还要检查静态方法返回Session ,工厂类(如SessionFactory )等。一般的文件是一个良好的开端找到了解如何获取类的实例:)

Very likely the constructor is private or protected or package access only. 构造函数很可能是私有的或受保护的,或者仅是程序包访问。 Are you sure you need to use that class? 您确定需要使用该课程吗? There might be derived classes that are better suited (or the class is supposed to be extended) 可能存在更适合的派生类(或应该扩展该类)

constructor probably is not public, if you have the source make it public or use some other constructor. 构造函数可能不是公共的,如果您有公开其来源或使用其他构造函数的源。 oor if there is no visible constructor there is some factory for creating instances 如果没有可见的构造函数,则可以使用一些工厂来创建实例

Since you have access to the class file inside the zip file, view it in your IDE or run javap on it. 由于您可以访问zip文件中的类文件,因此可以在IDE中查看它或在其上运行javap

Chances are there is a static factory method returning session objects somewhere in that class or in a related class. 可能有一个静态工厂方法返回该类或相关类中某处的会话对象。 This is common in cases where there are many types of sessions available. 这在会话类型很多的情况下很常见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM