• <bdo id='uddt1'></bdo><ul id='uddt1'></ul>

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

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

    2. <tfoot id='uddt1'></tfoot>

        为什么我的子类不能访问其超类的受保护变量,

        时间:2023-09-27

          <tbody id='hnr26'></tbody>

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

              <bdo id='hnr26'></bdo><ul id='hnr26'></ul>

              <legend id='hnr26'><style id='hnr26'><dir id='hnr26'><q id='hnr26'></q></dir></style></legend>
              <i id='hnr26'><tr id='hnr26'><dt id='hnr26'><q id='hnr26'><span id='hnr26'><b id='hnr26'><form id='hnr26'><ins id='hnr26'></ins><ul id='hnr26'></ul><sub id='hnr26'></sub></form><legend id='hnr26'></legend><bdo id='hnr26'><pre id='hnr26'><center id='hnr26'></center></pre></bdo></b><th id='hnr26'></th></span></q></dt></tr></i><div id='hnr26'><tfoot id='hnr26'></tfoot><dl id='hnr26'><fieldset id='hnr26'></fieldset></dl></div>
            • <tfoot id='hnr26'></tfoot>
                1. 本文介绍了为什么我的子类不能访问其超类的受保护变量,当它位于不同的包中时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个抽象类 relation 在包 database.relation 和它的子类 Join 在包 database.操作.relation 有一个名为 mStructure 的受保护成员.

                  I have an abstract class, relation in package database.relation and a subclass of it, Join, in package database.operations. relation has a protected member named mStructure.

                  加入:

                  public Join(final Relation relLeft, final Relation relRight) {
                          super();
                          mRelLeft = relLeft;
                          mRelRight = relRight;
                          mStructure = new LinkedList<Header>();
                          this.copyStructure(mRelLeft.mStructure);
                  
                          for (final Header header :mRelRight.mStructure) {
                          if (!mStructure.contains(header)) {
                              mStructure.add(header);
                          }
                      }
                  }
                  

                  在线

                  this.copyStructure(mRelLeft.mStructure);
                  

                  for (final Header header : mRelRight.mStructure) {
                  

                  我收到以下错误:

                  Relation.mStructure 字段不可见

                  The field Relation.mStructure is not visible

                  如果我将两个类放在同一个包中,这将完美运行.谁能解释一下这个问题?

                  If I put both classes in the same package, this works perfectly. Can anyone explain this issue?

                  推荐答案

                  它有效,但只有你的孩子试图访问它 自己的变量,而不是其他实例的变量(即使它属于相同的继承树).

                  It works, but only you the children tries to access it own variable, not variable of other instance ( even if it belongs to the same inheritance tree ).

                  查看此示例代码以更好地理解它:

                  See this sample code to understand it better:

                  //in Parent.java
                  package parentpackage;
                  public class Parent {
                      protected String parentVariable = "whatever";// define protected variable
                  }
                  
                  // in Children.java
                  package childenpackage;
                  import parentpackage.Parent;
                  
                  class Children extends Parent {
                      Children(Parent withParent ){
                          System.out.println( this.parentVariable );// works well.
                          //System.out.print(withParent.parentVariable);// doesn't work
                      } 
                  }
                  

                  如果我们尝试使用 withParent.parentVariable 进行编译,我们得到:

                  If we try to compile using the withParent.parentVariable we've got:

                  Children.java:8: parentVariable has protected access in parentpackage.Parent
                      System.out.print(withParent.parentVariable);
                  

                  它是可访问的,但只能访问它自己的变量.

                  It is accessible, but only to its own variable.

                  这篇关于为什么我的子类不能访问其超类的受保护变量,当它位于不同的包中时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                      <tbody id='Tznyo'></tbody>
                          <bdo id='Tznyo'></bdo><ul id='Tznyo'></ul>
                          <legend id='Tznyo'><style id='Tznyo'><dir id='Tznyo'><q id='Tznyo'></q></dir></style></legend>

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

                        • <tfoot id='Tznyo'></tfoot>