java中接口interface和private私有内部类怎样一块配合着用?

3.接口interface和private内部类协同工作【新手可忽略不影响继续学习】
马克-to-win:由于是private内部类,外面无法访问甚至无法看到你编的源代码(如果在不同的包中),非常安全。外界只能调用接口中的方法。下例中访问不了Core,甚至你不知道有Core的存在。给你的就是外部的接口,供你使用。马克-to-win:我们一直没讲class 如何能private, 这里内部类时,就可以用private了。且内部类随便访问外部类的东西, 这就非常有力度了, 可以用到外部类所有的资源!

例2.3---

interface CoreI
{   
    void display();
}
class ShellMark_to_win {
    int shell_x = 100;
    static int n;
    // 下面内部类是private,只能外层类的方法才能访问到, 非常安全
    private class Core implements CoreI {
/* 下一句错误,马克-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
        public void display() {
            shell_x=shell_x+20;
            n=n+1;//马克-to-win:轻松访问外层类的静态变量
            System.out.println("n is "+n+" display: shell_x and y " + shell_x + " "+ShellMark_to_win.this.shell_x+ " " + y+ " "+this.y);
        }
    }
    Core newC()
    {
        return new Core();
    }
}
public class Test {
    public static void main(String args[]) {
        ShellMark_to_win shell = new ShellMark_to_win();
     //   ShellMark_to_win.Core sc=shell.new Core();//错误,马克-to-win: 因为Core是私有的
        CoreI sc1=shell.newC();
        sc1.display();
    }
}

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

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

mark_to_win

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

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

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

打赏作者

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

抵扣说明:

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

余额充值