简体   繁体   English

如何从另一个 package 访问受保护的内部 class?

[英]How do I access a protected inner class from another package?

How do I access inner class "Inner" in "file1" in package "secret" from "file2" in package "test".如何从 package“测试”中的“文件 2”访问 package“秘密”中“文件 1”中的内部 class“内部”。 Here is the code for it:这是它的代码:

package secret;

public class file1 {
    protected class Inner {
        public int x = 8;
    }
}
package test;
import secret.file1;

public class file2 {
    public static void main(String[] args) {
        // code
    }
}

I am aware that there is a similar question on this topic ( How to access protected inner class outside package? ) but I didn't seem to understand the answers.我知道关于这个主题有一个类似的问题( 如何在 package 之外访问受保护的内部 class? )但我似乎不明白答案。 Here is what I have tried so far (from reading the answers):这是我迄今为止尝试过的(从阅读答案):

First attempt:第一次尝试:

public class file2 extends file1 {
    public static void main(String[] args) {
        file1 outer = new file1();
        file1.Inner inner = outer.new Inner();
    }
}

Second attempt:第二次尝试:

public class file2 extends file1 {
    public static class file3 extends file1.Inner {
        public static void main(String[] args) {
            file1 outer = new file1();
            file1.Inner inner = outer.new Inner();
        }
    }
}

Attempt 3:尝试 3:

public class file2 extends file1 {
    public static class file3 extends file1.Inner {
        public static void main(String[] args) {
            file2 outer = new file2();
            file2.file3 outerinner = outer.new file3();
            file2.file3.Inner inner = outerinner.new Inner();
        }
    }
}

There definitely seems like I'm missing something, guiding me in the right direction would be much apricated.肯定似乎我错过了一些东西,引导我朝着正确的方向前进将会非常有用。

There's a workaround by using third class:使用第三个 class 有一个解决方法:

package secret;

public class file3 extends file1 {
    public Inner createInner(){
        return new Inner();
    }
}

then simply:然后简单地说:

public class file2 extends file1 {
    public static void main(String[] args) {
        file3 outer = new file3();
        file1.Inner inner = outer.createInner();
    }
}

check your class hierarchy design whether it cannot be simplified.put your main focus on the difference between inner and static nested classes.检查您的 class 层次结构设计是否无法简化。将您的主要重点放在内部和 static 嵌套类之间的区别上。 or come up with something like this file1 x = new inner();或者想出类似这样的东西 file1 x = new inner();

Change the visibility to public.将可见性更改为公开。 Inner classes are mostly for interacting with private fields in its parent class anyway.无论如何,内部类主要用于与其父 class 中的私有字段进行交互。

Other way you can use a protected inner class is if the class you want to use it on is a sub class.您可以使用受保护的内部 class 的其他方式是,如果您要使用的 class 是子 class。 That is,the parent class of the inner class is being inherited by the class you want to use it on.也就是说,内部 class 的父 class 正在被您要使用它的 class 继承。 in your case在你的情况下


    package secret;
    
    public class file1 {
        protected class Inner {
            public int x = 8;
        }
    }


    package test;
    import secret.file1;
    
    public class file2 extends file1 {
        public static void main(String[] args) {
            File1 f = new File1();
            File1.Inner g = f.new Inner();
            System.out.println(g.x);
        }
    }

暂无
暂无

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

相关问题 如何在Maven构建中访问受程序包保护的类? - How do I access a package protected class in Maven build? 受保护的内部类不能从另一个包的子类访问 - Protected inner-class is NOT accessible from subclass of another package 如何从另一个包访问受保护的变量 - How to access a protected variable from another package 如何从派生类访问内部类构造函数? - How do I access an inner class constructor from a derived class? 如何从包(java)中的另一个类访问主类中的变量? - How do I access variables in the main class from another class in the package (java)? 如何访问我无法修改的类中的包保护变量 - How to access package-protected variable in class I cannot modify 我如何从内部类对象访问外部类的方法和全局变量,以及如果我不能拥有内部类的目的 - How do I access the methods and Global variables of outer class from an inner class object, and if I cannot what is purpose of having an Inner class 如何在其他位置包含来自包裹的课程 - How do I include a class from a package in another location 如何在apackage中编译和运行一个类,该类从另一个包中的另一个类继承受保护的成员 - how to compile and run a class in apackage that inherits protected member from another class in another package 如何将数据从一个包中的类发​​送到另一个包中的类? - How do I send data from a class in one package to a class in another package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM