java 构造函数不可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15886514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:10:46  来源:igfitidea点击:

Constructor not visible

javaandroidoverriding

提问by duncanportelli

I am developing an Android application which makes use of the ScanResultobject. This object is in the form of:

我正在开发一个使用该ScanResult对象的 Android 应用程序。该对象的形式为:

[SSID: __mynetwork__, BSSID: 00:0e:2e:ae:4e:85, capabilities: [WPA-PSK-TKIP][ESS], level: -69, frequency: 2457, timestamp: 117455824743]

I am trying to override the equals()method of this class by creating my own class which extends ScanResult:

我试图equals()通过创建我自己的扩展类来覆盖这个类的方法ScanResult

public class MyScanResult extends ScanResult {

    public MyScanResult() {
        super();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (!(o instanceof ScanResult))
            return false;

        ScanResult obj = (ScanResult) obj;
        if (!BSSID.equals(obj.BSSID))
            return false;
        if (!SSID.equals(obj.SSID))
            return false;
        if (!capabilities.equals(obj.capabilities))
            return false;
        if (frequency != obj.frequency)
            return false;
        if (level != obj.level)
            return false;

        return true;
    }

}

However when I try this, I get the following error: The constructor ScanResult() is not visible. How can I solve this please?

但是,当我尝试此操作时,出现以下错误:The constructor ScanResult() is not visible. 请问我该如何解决?

回答by Reimeus

The public constructor signature for ScanResultis:

的公共构造函数签名ScanResult是:

public ScanResult(String SSID, String BSSID, String caps, int level, int frequency) 

You need to invoke the super class with matching parameters

您需要使用匹配的参数调用超类

回答by Korcholis

The apparently good way:

明显的好方法

ScanResultexpects parameters:

ScanResult期望参数:

public ScanResult(  String SSID, 
                    String BSSID,
                    String caps,
                    int level,
                    int frequency)

You can check the class definition here

您可以在此处查看类定义

The ugly way:

丑陋的方式

As you say, apparently ScanResultis private. This answertells you to use reflection to get to the constructor.

正如你所说,显然ScanResult是私人的。这个答案告诉您使用反射来访问构造函数。

The possibly only way:

可能唯一的方法

Nobody knows how this happens to you (it's actually weird). But there's a change to solve it. Hit the herelink, copy the class into your project (change its package for yours, of course), and just make MyScanResultinherit from this one. Android is open source, and, despite this class may change in the future, you ensure this will work right now. Then, you can try casting your new ScanResultusing (android.net.wifi.ScanResult)scanResult, if you need the primitive class.

没有人知道这是怎么发生在你身上的(这实际上很奇怪)。但是有一个改变来解决它。点击这里的链接,将类复制到您的项目中(当然,为您的项目更改其包),然后MyScanResult从这个项目中继承。Android 是开源的,尽管此类将来可能会发生变化,但您要确保它现在可以正常工作。然后,如果您需要原始类,您可以尝试ScanResult使用 来转换新的(android.net.wifi.ScanResult)scanResult