1 package cn.inner; 2 3 public class TestInner { 4 private String name = "huhu"; 5 private int age = 15; 6 public static String master = "王先生"; 7 8 public TestInner() { 9 new dog().sleep();//01外部类调用内部类的属性和方法 10 } 11 12 public void eat(String foodName) { 13 System.out.println(name + "吃" + foodName);//02 14 } 15 16 public void yell() { 17 System.out.println(name + "在叫喊!!!"); 18 } 19 20 class dog { 21 private String name = "狼犬"; 22 23 public void sleep() { 24 eat("肉!!!");//02 内部类调用外部类的属性和方法 25 System.out.println(name + "在睡覺!!!"+", "+name+"的名字叫"+TestInner.this.name);//03内部类调用外部类的属性 26 } 27 } 28 29 public static void main(String[] args) { 30 //04 main 方法里创建内部类的构造方法 31 TestInner t1=new TestInner(); 32 dog d=t1.new dog(); 33 d.sleep(); 34 } 35 }