资讯详情

浅谈c++和java的内部类

最近在项目中遇到一个有趣c 比较问题Java,整理如下。

 class AUBCRoll : public VWFC::VWSceneController         {                  public:             struct tstBCButtonInfo              {                 bool m_boVisible;                 uint8 m_u8SubDisplayID;                 uint8 m_u8Layer;                 uint16 m_u16BCRollMappedIndex;                 uint16 m_u16BCRollRealIndex;                  const static uint8 m_u8BigBCRollNumbers;                 const static uint8 m_u8SmallBCRollNumbers;                 const static uint8 m_u8NaviRealIndex;                 const static uint8 m_u8AccRealIndex;                 const static uint8 m_u8LGRealIndex;              };  protected:             /**             * Miscellaneous values             */             enum enMisc              {                 AUWFC_BCROLL_WHEEL_TICKS =  VWFC_WHEEL_TICKS,           // System defined event                 AUWFC_BCROLL_ACTIVATE_NEXT_CHILD =   1,                          // Next child offset                 AUWFC_BCROLL_ACTIVATE_PREVIOUS_CHILD =   -1,                         // Previous child offset                 AUWFC_BCROLL_ACTIVATE_FIRST_VISIBLE_CHILD       =   0,                          // Indicate show first visible child                 AUWFC_BCROLL_INVALID_CHILD_INDEX =   WFC::nMAX_RANGE_OF_CHILDREN,     // Invalid child real index                 AUWFC_BCROLL_MAXIMUM_POSSIBLE_INPUT_MFL_VARIANTE =   7,                          // Maximum MFL wheel tick                 AUWFC_BCROLL_MINIMUM_POSSIBLE_INPUT_MFL_VARIANTE =   -7,                         // Minimum MFL wheel tick                 AUWFC_BCROLL_NOT_POSSIBLE_INPUT_MFL_VARIANTE  =   0,                          // Invalid MFL wheel tick                 AUWFC_BCROLL_CHANGE_DIRECTION  =   -1,                         // Change direction indication                 AUWFC_BCROLL_NUM_SPECIAL_BUTTONS                  =   3,                          // The number of special button types( ACC, Navi, LG )                 AUWFC_BCROLL_8BIT_INVALID                         =   0xFF,                       // Invalid value for 8 bit fields                 AUWFC_BCROLL_16BIT_INVALID                        =   0xFFFF,                     // Invalid value for 16 bit fields                 AUWFC_BCROLL_32BIT_INVALID                        =   0xFFFFFFFF,                 // Invalid value for 32 bit fields                 AUWFC_BCROLL_INVALID_MAPPING                      =   AUWFC_BCROLL_8BIT_INVALID,  // Invalid child mapped index                 AUWFC_BCROLL_INVALID_LANEGUIDANCEONOFF              =   0xFE                        // Invalid value for LaneGuidanceOnO              };  };
tstBCButtonInfo是BCRoll的一个public的in-class,我们可以用tstBCButtonInfo定义新的类对象: AUBCRoll::tstBCButtonInfo stButtonInfo = AUBCRoll::tstBCButtonInfo (); 但是,外部类AUBCRoll和内部类tstBCButtonInfo两者之间的访问权是什么?

        static AUBCRoll::tstBCButtonInfo stSmallBCRollChildBCButtonInfoTable[AUBCRoll::tstBCButtonInfo::m_u8SmallBCRollNumbers] =         {             /*m_boVisible, m_u8SubDisplayID, m_u8Layer, m_u16BCRollMappedIndex, m_u16BCRollRealIndex*/             {true,  11, 0, 0, 0},//__cK15ON__cNO_SCP__vREF_FELD3_BC__REF_FELD3_BC__F3_Container_prioDF3__BCRoll__Date             {false, 19, 0, AUBCRoll::AUWFC_BCROLL_INVALID_MAPPING, 1},//__cK15ON__cNO_SCP__vREF_FELD3_BC__REF_FELD3_BC__F3_Container_prioDF3__BCRoll__Tota             {false, 17, 0, AUBCRoll::AUWFC_BCROLL_INVALID_MAPPING, 2}   }

此时,编译器将报告错误,表明内部类不能访问外部类public成员; 同样,外部类也不能访问内部类的非public成员。 对于c ,内外类之间的访问完全取决于成员自身的访问修饰符。 再看看Java,这个比c 更复杂有趣。 另一个类可以在一个类的内部定义,称为嵌套类(nested classes),它有两种类型:静态嵌套和非静态嵌套。非静态嵌套也被称为内部类(inner class)。嵌套是其外部成员,可以声明为private, public, protected, 或者package private。 (此时,外部类只能声明为public 或者package private。) 嵌套类 从JDK1.1开始引入。内部类别。(inner可分为三类: 内部类直接定义在一个类(外部类)中; 内部类在一种方法(外部类方法)中定义; 匿名内部类。 内部类访问外部类 规则和内部可以自由访问外部static一样。(访问非静态时必须先创建对象)

具体如下: * 直接访问

  1. public class Outter {  
  2.     int i = 5;  
  3.     static String string = "Hello";  
  4.       
  5.     class Inner1{  
  6.         void Test1 (){  
  7.             System.out.println(i);  
  8.             System.out.println(string);  
  9.         }  
  10.     }  
  11. }
因为静态方法访问非静态外部成员需先创建实例,所以访问i时必须先new外部类。

<div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: rgb(231, 229, 220); width: 693px; overflow: auto; padding-top: 1px; color: rgb(54, 46, 43); line-height: 26px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108);"><label>[java]</label> <a target=_blank target="_blank" href="http://blog.csdn.net/jueblog/article/details/13434551#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-color: inherit; border: none; padding: 1px; margin: 0px 10px 0px 0px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;">view plain</a><a target=_blank target="_blank" href="http://blog.csdn.net/jueblog/article/details/13434551#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-color: inherit; border: none; padding: 1px; margin: 0px 10px 0px 0px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;">copy</a><div style="position: absolute; left: 522px; top: 1134px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1" class="dp-j" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Outter {  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> i = </span><span class="number" style="margin: 0px; padding: 0px; border: none; color: rgb(192, 0, 0); background-color: inherit;">5</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">;  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> String string = </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"Hello"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">;  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">      </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Inner2{  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Test1 (){  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">            System.out.println(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Outter().i);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">            System.out.println(string);  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        }  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li><li></li></ol></div>

大方向:因为将内部类理解为外部类的一个普通成员,所以外面的访问里面的需先new一个对象。 new 内部类对象

  1. public class Outter {  
  2.     void Test1(){  
  3.         System.out.println(new Inner1().i);  
  4.     }  
  5.     class Inner1{  
  6.         int i = 5;  
  7. //      static String string = "Hello";  定义错误!  
  8.     }  
静态方法Test2访问非静态Inner1需先new外部类; 继续访问非静态成员i需先new 内部类 所以访问规则为:new Outter().new Inner1().i。

  1. public class Outter {  
  2.     static void Test2(){  
  3.         System.out.println(new Outter().new Inner1().i);  
  4.     }  
  5.     class Inner1{  
  6.         int i = 5;  
  7. //      static String string = "Hello";  定义错误!  
  8.     }  
先“外部类.内部类”访问至内部类。 若访问静态成员,则需先new再访问;若访问非静态成员,则可直接访问。

  1. public class Outter {  
  2.     void Test1(){  
  3.         Outter.Inner2 inner2 = new Outter.Inner2();  
  4.         System.out.println(inner2.i);  
  5.         System.out.println(inner2.string);  
  6.         System.out.println(Outter.Inner2.string);  
  7.     }  
  8.     static class Inner2{  
  9.         int i = 5;  
  10.         static String string = "Hello";   
  11.     }  
先“外部类.内部类”访问至内部类,再new即可访问静态成员。

浅谈c++和java的内部类

标签: 开关三极管3ck15a

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台