• 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

    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();
    }

    参考:https://www.cnblogs.com/libinhong/p/10990602.html

  • 相关阅读:
    Django框架之视图层
    Django框架之模型层 多表操作
    Django框架之模型层 单表操作
    Django框架之模板层
    Django框架之路由层
    Django框架及ORM的基本使用
    Django框架安装
    python web的三大主流框架
    web基础之手动实现简易web服务框架
    C# 语法特性
  • 原文地址:https://www.cnblogs.com/kenantongxue/p/13985292.html
Copyright © 2020-2023  润新知