定义一个类 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("我是内部静态类"); } }
