1 class School { 2 String name; 3 public class Student { 4 String name; 5 int age; 6 public Student(String schoolName,String studentName,int newAge) { 7 School.this.name=schoolName; 8 this.name=studentName; 9 age=newAge; 10 } 11 public void output() { 12 System.out.println("学校:"+School.this.name); 13 System.out.println("姓名:"+this.name); 14 System.out.println("年龄:"+this.age); 15 } 16 } 17 public void output() { 18 Student stu=new Student("金融学院","张三",24); 19 stu.output(); 20 } 21 } 22 public class Inner { 23 public static void main(String[] args) { 24 System.out.println("--通过外部类成员访问内部类成员--"); 25 School a=new School(); 26 a.output(); 27 System.out.println("--直接访问内部成员--"); 28 School.Student b=a.new Student("金融学院","李四",23); 29 b.output(); 30 } 31 }
新知识点来了,内部类!