最近在项目中遇到一个有趣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 style="padding-left: 45px;"><div 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="" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: url(); 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="" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: url(); 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" 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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. 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. new Outter().new Inner1().i);
4. }
5. class Inner1{
6. int i = 5;
7. // static String string = "Hello"; 定义错误!
8. }
9. }
非静态方法访问静态内部类的成员
先“外部类.内部类”访问至内部类。
若访问静态成员,则需先new再访问;若访问非静态成员,则可直接访问。
1. public class Outter {
2. void Test1(){
3. 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即可访问静态成员。