Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 反射:如何调用隐藏的超类构造函数?_Java_Android_Reflection_Superclass - Fatal编程技术网

Java 反射:如何调用隐藏的超类构造函数?

Java 反射:如何调用隐藏的超类构造函数?,java,android,reflection,superclass,Java,Android,Reflection,Superclass,重新编辑 我想使用一个超类构造函数,它被“@hide”Android标记(或其他任何标记)隐藏 我将要扩展一个已经扩展了两次的类(在Android操作系统中)。我想创建自己的子类(即,在Android操作系统之外)。示例子类,取自Android源代码: public class WifiP2pDnsSdServiceInfo extends WifiP2pServiceInfo { ... private WifiP2pDnsSdServiceInfo(List<Strin

重新编辑

我想使用一个超类构造函数,它被“@hide”Android标记(或其他任何标记)隐藏

我将要扩展一个已经扩展了两次的类(在Android操作系统中)。我想创建自己的子类(即,在Android操作系统之外)。示例子类,取自Android源代码:

public class WifiP2pDnsSdServiceInfo extends WifiP2pServiceInfo {
    ...
    private WifiP2pDnsSdServiceInfo(List<String> queryList) {
        super(queryList); // <-- this is what I'm trying to do, too
    }

    public static WifiP2pDnsSdServiceInfo newInstance(String instanceName,
            String serviceType, Map<String, String> txtMap) {
        ...
        ArrayList<String> queries = new ArrayList<String>();
        ...
        return new WifiP2pDnsSdServiceInfo(queries);
    }
}
公共类WifiP2pDnsSdServiceInfo扩展了WifiP2pServiceInfo{
...
私有WifiP2pDnsSdServiceInfo(列表查询列表){

super(queryList);//您希望找到该构造函数并将其可用性设置为true

但这是一个危险的操作,你不应该轻率地尝试。隐私不一定意味着隐私,这是一个肮脏的秘密,但我仍然希望你尊重类设计者的意愿,而不是规避

此外,你不需要。如果我理解你的要求,我已经发布了一个例子,可以满足你的要求

这是可行的,因为:

受保护的修改器指定只能访问该成员 在其自己的包内(与包专用一样),并且 它的类在另一个包中的子类

我想你把受保护修饰语的意思弄糊涂了

带受保护ctor的父级:

package foo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Parent with protected constructor
 * User: MDUFFY
 * Date: 3/27/14
 * Time: 5:18 PM
 * @link http://stackoverflow.com/questions/22698501/reflection-how-to-call-superclass-constructor-which-is-hidden/22698543?noredirect=1#comment34586243_22698543
 */
public class Foo {
    private List<String> x;

    protected Foo(List<String> y) {
        this.x = ((y == null) ? new ArrayList<String>() : new ArrayList<String>(y));
    }

    public List<String> getX() {
        return Collections.unmodifiableList(this.x);
    }

    @Override
    public String toString() {
        return "Foo{" +
                "x=" + x +
                '}';
    }
}
package-foo;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
/**
*具有受保护构造函数的父级
*用户:MDUFFY
*日期:2014年3月27日
*时间:下午五时十八分
*@linkhttp://stackoverflow.com/questions/22698501/reflection-how-to-call-superclass-constructor-which-is-hidden/22698543?noredirect=1#comment34586243_22698543
*/
公开课Foo{
私人名单x;
受保护的Foo(列表y){
this.x=((y==null)?new ArrayList():new ArrayList(y));
}
公共列表getX(){
返回集合。不可修改列表(this.x);
}
@凌驾
公共字符串toString(){
返回“Foo{”+
“x=”+x+
'}';
}
}
子级扩展父级:

package bar;

import foo.Foo;

import java.util.Arrays;
import java.util.List;

/**
 * Child of parent with protected ctor
 * User: MDUFFY
 * Date: 3/27/14
 * Time: 5:22 PM
 * @link http://stackoverflow.com/questions/22698501/reflection-how-to-call-superclass-constructor-which-is-hidden/22698543?noredirect=1#comment34586243_22698543
 */
public class Bar extends Foo {

    public static void main(String[] args) {
        Bar bar = new Bar(Arrays.asList(args));
        System.out.println(bar);
    }

    public Bar(List<String> y) {
        super(y);
    }


}
包装条;
进口foo.foo;
导入java.util.array;
导入java.util.List;
/**
*具有受保护的父对象的子对象
*用户:MDUFFY
*日期:2014年3月27日
*时间:下午5:22
*@linkhttp://stackoverflow.com/questions/22698501/reflection-how-to-call-superclass-constructor-which-is-hidden/22698543?noredirect=1#comment34586243_22698543
*/
公共类Bar扩展了Foo{
公共静态void main(字符串[]args){
Bar Bar=新条(Arrays.asList(args));
系统输出打印项次(巴);
}
公共酒吧(名单y){
超级(y);
}
}

这将编译并运行。

经过一些研究,我能够自己回答这个问题

简短回答:我不能这样做,因为如果超类构造函数被保护和隐藏,即使我找到了通过反射调用构造函数的方法,编译器也会抱怨

答案很长:事实证明“隐藏”这些东西并不复杂。接下来,我可以根据自己的需要扩展这个类


看到了吗?很多噪音都是徒劳的,这就是我一直在寻找的答案。

如果编写类A的人将构造函数标记为private,这意味着该类不是为扩展而设计的,您将已经在调用super()在这种情况下。在同一对象上调用两个构造函数的结果没有定义。您可以在同一个包中定义该类。它不必位于同一个位置。我假设隐藏意味着它具有
默认访问修饰符。超类构造函数是否标记为
受保护的
私有的
(这将使包私有)@EJP:我很确定子类不会已经调用super()在这种情况下,我想扩展Android的一个类。操作系统已经包含了两个子类,我想介绍第三个子类,用于我自己的协议。因此,我知道CTOR的签名和所有内容,但我无法找出正确的语法如何做(这里是Java新手).什么类?如果它已经包含两个子类,那么这两个子类显然可以在不使用超类的私有构造函数的情况下工作。因此,有可能不使用它就可以编写另一个子类。----除非构造函数实际上是包私有的(从最初的问题看不清楚)。请看我编辑的问题,我希望现在更清楚。抱歉造成混淆。新手不应该以这种方式使用反射。更喜欢合成而不是继承。不要扩展单例。让您的要求更清楚,也许我们可以建议下一步。
package bar;

import foo.Foo;

import java.util.Arrays;
import java.util.List;

/**
 * Child of parent with protected ctor
 * User: MDUFFY
 * Date: 3/27/14
 * Time: 5:22 PM
 * @link http://stackoverflow.com/questions/22698501/reflection-how-to-call-superclass-constructor-which-is-hidden/22698543?noredirect=1#comment34586243_22698543
 */
public class Bar extends Foo {

    public static void main(String[] args) {
        Bar bar = new Bar(Arrays.asList(args));
        System.out.println(bar);
    }

    public Bar(List<String> y) {
        super(y);
    }


}