java中内部类中还有内部类请给实例!

2.当内部类中还有一个内部类,下面给出了一个实例。【新手可忽略不影响继续学习】(以下多出代码, 用蓝色标记)

例2.2:---
class ShellMark_to_win {
    int shell_x = 100;//马克-to-win:既然每个内部类实例都可以改变这里的外层类静态属性或实例属性,这里成为内部类实例们可以交互的地方
    static int n;
    void visitCore() {
        Core core = new Core();
        core.y=8;
        core.display();
    }
    // 下面是个实例内部类,必须有个外层类实例,才能有这个内部类实例。所以就有了this这个概念。
    class Core {
/* 下一句错误,根据语法:马克-to-win:静态的域或方法只能出现在静态类或最外层类上。The field m cannot be declared static; static fields can only be declared in static inner class or top level classes,*/       
  //    static int m=9;
        int y = 10; // y is local to core
        void display() {
            shell_x=shell_x+20;
            n=n+1;//轻松访问外层类的静态变量
            System.out.println("n is "+n+" display: shell_x and y " + shell_x + " "+ShellMark_to_win.this.shell_x+ " " + y+ " "+this.y);
        }
        class CoreCore{
            void displayDis() {
                Core.this.display();
            }
        }

    }
    Core newC()
    {
        return new Core();
    }
    void showy() {
        // y=9; // 错误,马克-to-win:外层类不能直接访问内部类的属性。error,y not known here! System.out.println(y);
    }
}
public class Test {
    public static void main(String args[]) {
        ShellMark_to_win shell = new ShellMark_to_win();
        shell.visitCore();
        ShellMark_to_win.Core sc=shell.new Core();//马克-to-win:内部类是存在于外部对象里的。
        sc.display();
        ShellMark_to_win.Core.CoreCore scc=sc.new CoreCore();
        scc.displayDis();

        ShellMark_to_win.Core sc1=shell.newC();
        sc1.display();
/* 不能像下面这样写, 因为Core 是内部类。   can not resolve Core class ,because Core class are defiened as ShellMark_to_win's inner class. */
        //Core core = new Core();
    }
}

更多请见:https://blog.csdn.net/qq_44639795/article/details/103110520

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mark_to_win

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值