浅谈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一样。(访问非静态时必须先创建对象)


具体如下:
非静态内部类的非静态方法
* 直接访问

[java]  view plain copy
  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);"><strong>[java]</strong> <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 内部类对象

[java]  view plain copy
  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。

[java]  view plain copy
  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.     }  
  9. } 
非静态方法访问静态内部类的成员
先“外部类.内部类”访问至内部类。
若访问静态成员,则需先new再访问;若访问非静态成员,则可直接访问。

[java]  view plain copy
  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的内部类

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值