<legend id='NQGkX'><style id='NQGkX'><dir id='NQGkX'><q id='NQGkX'></q></dir></style></legend>

      <small id='NQGkX'></small><noframes id='NQGkX'>

      1. <i id='NQGkX'><tr id='NQGkX'><dt id='NQGkX'><q id='NQGkX'><span id='NQGkX'><b id='NQGkX'><form id='NQGkX'><ins id='NQGkX'></ins><ul id='NQGkX'></ul><sub id='NQGkX'></sub></form><legend id='NQGkX'></legend><bdo id='NQGkX'><pre id='NQGkX'><center id='NQGkX'></center></pre></bdo></b><th id='NQGkX'></th></span></q></dt></tr></i><div id='NQGkX'><tfoot id='NQGkX'></tfoot><dl id='NQGkX'><fieldset id='NQGkX'></fieldset></dl></div>

        <tfoot id='NQGkX'></tfoot>
        • <bdo id='NQGkX'></bdo><ul id='NQGkX'></ul>

        为什么另一个包中的子类不能访问受保护的方法

        时间:2023-09-27
        • <bdo id='fpvNp'></bdo><ul id='fpvNp'></ul>
        • <i id='fpvNp'><tr id='fpvNp'><dt id='fpvNp'><q id='fpvNp'><span id='fpvNp'><b id='fpvNp'><form id='fpvNp'><ins id='fpvNp'></ins><ul id='fpvNp'></ul><sub id='fpvNp'></sub></form><legend id='fpvNp'></legend><bdo id='fpvNp'><pre id='fpvNp'><center id='fpvNp'></center></pre></bdo></b><th id='fpvNp'></th></span></q></dt></tr></i><div id='fpvNp'><tfoot id='fpvNp'></tfoot><dl id='fpvNp'><fieldset id='fpvNp'></fieldset></dl></div>
            • <small id='fpvNp'></small><noframes id='fpvNp'>

                <tbody id='fpvNp'></tbody>

                1. <legend id='fpvNp'><style id='fpvNp'><dir id='fpvNp'><q id='fpvNp'></q></dir></style></legend>
                  <tfoot id='fpvNp'></tfoot>
                  本文介绍了为什么另一个包中的子类不能访问受保护的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在两个不同的包中有两个类:

                  package package1;公共类 Class1 {公共无效tryMePublic(){}受保护的无效tryMeProtected(){}}包包2;导入包1.Class1;公共类 Class2 扩展 Class1 {现在做() {Class1 c = new Class1();c.tryMeProtected();//错误:tryMeProtected() 在 Class1 中具有受保护的访问权限tryMeProtected();//没有错误}}

                  我可以理解为什么调用 tryMeProtected() 没有错误,因为 Class2 认为这个方法继承自 Class1.p>

                  但是为什么 Class2 的对象不能使用 c.tryMeProtected(); 访问 Class1 的对象上的这个方法;?

                  解决方案

                  受保护的方法只能通过在包外的子类中继承来访问.因此,第二种方法 tryMeProtected(); 有效.

                  下面的代码不会编译,因为我们没有调用受保护方法的继承版本.

                   Class1 c = new Class1();c.tryMeProtected();//错误:tryMeProtected() 在 Class1 中具有受保护的访问权限

                  关注这个 stackoverflow 更多解释的链接.

                  I have two classes in two different packages:

                  package package1;
                  
                  public class Class1 {
                      public void tryMePublic() {
                      }
                  
                      protected void tryMeProtected() {
                      }
                  }
                  
                  
                  package package2;
                  
                  import package1.Class1;
                  
                  public class Class2 extends Class1 {
                      doNow() {
                          Class1 c = new Class1();
                          c.tryMeProtected(); // ERROR: tryMeProtected() has protected access in Class1
                          tryMeProtected();  // No error
                      }    
                  }
                  

                  I can understand why there is no error in calling tryMeProtected() since Class2 sees this method as it inherits from Class1.

                  But why isn't it possible for an object of Class2 to access this method on an object of Class1 using c.tryMeProtected(); ?

                  解决方案

                  Protected methods can only be accessible through inheritance in subclasses outside the package. And hence the second approach tryMeProtected(); works.

                  The code below wont compile because we are not calling the inherited version of protected method.

                   Class1 c = new Class1();
                   c.tryMeProtected(); // ERROR: tryMeProtected() has protected access in Class1
                  

                  Follow this stackoverflow link for more explaination.

                  这篇关于为什么另一个包中的子类不能访问受保护的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                2. <legend id='y8nuI'><style id='y8nuI'><dir id='y8nuI'><q id='y8nuI'></q></dir></style></legend>
                  • <small id='y8nuI'></small><noframes id='y8nuI'>

                      <i id='y8nuI'><tr id='y8nuI'><dt id='y8nuI'><q id='y8nuI'><span id='y8nuI'><b id='y8nuI'><form id='y8nuI'><ins id='y8nuI'></ins><ul id='y8nuI'></ul><sub id='y8nuI'></sub></form><legend id='y8nuI'></legend><bdo id='y8nuI'><pre id='y8nuI'><center id='y8nuI'></center></pre></bdo></b><th id='y8nuI'></th></span></q></dt></tr></i><div id='y8nuI'><tfoot id='y8nuI'></tfoot><dl id='y8nuI'><fieldset id='y8nuI'></fieldset></dl></div>
                      <tfoot id='y8nuI'></tfoot>

                          <tbody id='y8nuI'></tbody>
                        • <bdo id='y8nuI'></bdo><ul id='y8nuI'></ul>