zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    String,StringBuffer,StringBuilder简单对比
    Java基本数据类型
    EasyMock框架的使用详解
    Python3.6在win7中无法正常运行的问题
    zabbix3.4源码安装步骤
    hadoop_2.6.5集群安装
    Cassandra2.2.10安装过程
    JDK1.8安装
    zookeeper3.4.6安装
    python3.6的安装及cx_oracle安装
  • 原文地址:https://www.cnblogs.com/kenantongxue/p/13985292.html
Copyright © 2011-2022 走看看