package zzz; import zzz.Circle.Draw; public class innerClass { public static void main(String[] args) { Circle circle = new Circle(10); // 成员内部类创建方式必须依靠外部类对象创建 // 方式1: Circle.Draw draw = circle.new Draw(); System.out.println(draw); System.out.println(circle.new Draw()); // 方式2: Circle.Draw draw2 = circle.getDraw(); System.out.println(draw2); } } // 外部类 class Circle { private Draw draw = null; private double radius = 0; public static int count = 1; public Circle(double radius) { this.radius = radius; } public Draw getDraw() { if (draw == null) { draw = new Draw(); } return draw; } // 成员内部类 class Draw { public Draw() { } public void drawShape() { System.out.println("draw shape."); System.out.println(radius); System.out.println(count); } } }
更多详细参考:https://www.cnblogs.com/dolphin0520/p/3811445.html
public class InnerclassDemo { public static void main(String[] args) { Person person = new Person(); Person.Heart heart = person.new Heart(); heart.jump(); person.setLive(false); heart.jump(); } } class Person { private boolean live = true; public void setLive(boolean live) { this.live = live; } public boolean isLive() { return live; } class Heart { public void jump() { if (live) { System.out.println("心脏在跳动..."); }else { System.out.println("死亡了..."); } } } }
public class InnerclassDemo2 { public static void main(String[] args) { FlyAble flyAble = new FlyAble() { @Override public void fly() { System.out.println("起飞!"); } }; flyAble.fly(); } } abstract class FlyAble { public abstract void fly(); }