Java(32):内部类

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

上一篇:Java单体应用 - 架构模式 - 03.设计模式-01.工厂模式


下一篇:Java设计模式-工厂模式