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

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

        <bdo id='rZPdb'></bdo><ul id='rZPdb'></ul>
      <tfoot id='rZPdb'></tfoot>
        <i id='rZPdb'><tr id='rZPdb'><dt id='rZPdb'><q id='rZPdb'><span id='rZPdb'><b id='rZPdb'><form id='rZPdb'><ins id='rZPdb'></ins><ul id='rZPdb'></ul><sub id='rZPdb'></sub></form><legend id='rZPdb'></legend><bdo id='rZPdb'><pre id='rZPdb'><center id='rZPdb'></center></pre></bdo></b><th id='rZPdb'></th></span></q></dt></tr></i><div id='rZPdb'><tfoot id='rZPdb'></tfoot><dl id='rZPdb'><fieldset id='rZPdb'></fieldset></dl></div>
      1. 从不同包的另一个实例的子类调用受保护的方法

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

                • <legend id='sBxRG'><style id='sBxRG'><dir id='sBxRG'><q id='sBxRG'></q></dir></style></legend>

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

                  <tfoot id='sBxRG'></tfoot>
                  本文介绍了从不同包的另一个实例的子类调用受保护的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想从提供此受保护方法的类的子类中调用另一个实例的受保护方法.请参阅以下示例:

                  I want to invoke a protected method of another instance from within a subclass of the class providing this protected method. See the following example:

                  public class Nano {
                  
                      protected void computeSize() {
                      }
                  
                  }
                  
                  public class NanoContainer extends Nano {
                  
                      protected ArrayList<Nano> children;
                  
                  }
                  
                  public class SomeOtherNode extends NanoContainer {
                  
                      // {Nano} Overrides
                  
                      protected void computeSize() {
                          for (Nano child: children) {
                              child.computeSize();            // << computeSize() has protected access in nanolay.Nano
                          }
                      }
                  
                  }
                  

                  javac 告诉我 computeSize() 在 Nano 中具有受保护的访问权限.我看不出这是什么原因(我以为我已经在其他代码中这样做了).我想保持这种方法受到保护,我该怎么办?

                  javac tells me that computeSize() has protected access in Nano. I can't see the reason for this (I thought I was already doing this in some other code). I'd like to keep this method being protected, what can I do?

                  javac version "1.7.0_09"
                  

                  编辑

                  我想提供一个精简的版本,但我没有想到这些类位于不同的包中.

                  Edit

                  I wanted to provide a stripped down version, but I didn't think about the fact, that the classes lie in different packages.

                  nanolay.Node
                  nanolay.NanoContainer
                  nanogui.SomeOtherNode
                  

                  推荐答案

                  您可以通过子类化和覆盖来访问受保护的方法;当它们在同一个包中可用时.我会补充一些细节.您可以在此处阅读详细信息.

                  You could access the protected methods either by subclassing and overriding; also when they are available in the same package. I will add some details. You can read details here.

                  您的示例位于 java 中 Object 类中可用的 protected clone() 方法的行上;您不能直接在任何对象上调用它(尽管所有对象都隐式地从 Object 类扩展).

                  The example that you have is on lines of the protected clone() method available in the Object class in java; you cannot directly call it on any object (although all object implicitly extend from the Object class).

                  这篇关于从不同包的另一个实例的子类调用受保护的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Java 中使用 3rd 方包 下一篇:包装和可见性

                  相关文章

                  最新文章

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

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

                    1. <tfoot id='xSEF0'></tfoot>

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