java66-局部类内方法访问

定义一个类

package com.zx;
public class NotebookComputer {
    private int num;    
    private CPU cpu;
    public String fromIntoBit(){    
        class CPU2{// 局部内部类
            public String countBit(int num){
                return Integer.toBinaryString(num);
            }
        }
        return (new CPU2().countBit(num));
    }
    /***
     * 普通内部类
     */
private final class CPU{
        
        }
    public NotebookComputer(int num){
        this.num = num;
        if(cpu == null)
            cpu = new CPU();
    }
    
}
定义测试类

package com.zx;
 
public class TestInnerClass {
    public static void main(String[] args) {
//        // 创建一个外部类
//        NotebookComputer com = new NotebookComputer();
//        // 声明一个内部类
//        NotebookComputer.CPU cpu;
//        
//        // 通过外部类的实例创建一个内部类对象
//        cpu = com.new CPU();
//        
//        String bitStr = cpu.countBit(11);
//        System.out.println(bitStr);
        NotebookComputer com = new NotebookComputer(9);
        
        System.out.println(com.fromIntoBit());
        
//        System.out.println(com.fromIntoBit());
//        
//        /* 内部静态类的创建*/
//        NotebookComputer.VideoCard vc = new NotebookComputer.VideoCard();
//        
//        vc.showMessage("我是内部静态类");
        
    }
}
运行结果


 

posted @ 2022-06-22 22:55  前端导师歌谣  阅读(35)  评论(0)    收藏  举报