成员普通内部类
public class Person {
public int age;
public Heart getHeart(){
int age = new Heart().age;
return new Heart();
}
public void eat(){
System.out.println("eat now");
}
public class Heart{
int age = 18;
public void GetOutMethod(){
eat();
}
public void HeartAge(){
System.out.println(age);
}
public void GetSameHeartAge(){
System.out.println(Person.this.age);
}
}
public static void main(String[] args) {
Heart heart = new Person().new Heart();
Person person = new Person();
Heart heart1 = person.new Heart();
Person person1 = new Person();
Heart heart2 = person1.getHeart();
}
}
静态内部类
public class People {
public int age;
public static int true_age = 18;
public static class Eye {
public int getAge(){
return new People().age;
}
public static void SayHello(){
System.out.println("hello" + true_age);
}
}
public static void main(String[] args) {
Eye eye = new People.Eye();
Eye eye1 = new Eye();
eye1.getAge();
Eye.SayHello();
eye.getAge();
}
}
方法内部类 不推荐使用
匿名内部类
- 将类的定义和创建放在一起完成 通常用来简化抽象类和接口实例化的操作