zoukankan      html  css  js  c++  java
  • 类内部实例化自身可行吗?

    答案是不能。

    事实证明,在类内部一直实例化自身会造成栈溢出,测试代码如下。

     1 public class test1 {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         test1.A aa = new test1().new A();
     9         test1.A b = aa.getA();
    10         System.out.println("ok");
    11     }
    12 
    13     class A{
    14  
    15         private A a = new A();
    16         
    17         public A getA(){
    18            
    19             return a;
    20         }
    21     }
    22 }

    结果:

     1 exception in thread "main" java.lang.StackOverflowError
     2     at test1$A.<init>(test1.java:13)
     3     at test1$A.<init>(test1.java:15)
     4     at test1$A.<init>(test1.java:15)
     5     at test1$A.<init>(test1.java:15)
     6     at test1$A.<init>(test1.java:15)
     7     at test1$A.<init>(test1.java:15)
     8     at test1$A.<init>(test1.java:15)
     9     at test1$A.<init>(test1.java:15)
    10     at test1$A.<init>(test1.java:15)

    但是如下的代码为何能够良好运行?

     1 public class test1 {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         test1.A aa = new test1().new A();
     9         test1.A b = aa.getA();
    10         System.out.println("ok");
    11     }
    12 
    13     class A{
    14  
    15         private A a;
    16         
    17         public A getA(){
    18             a = new A();
    19             return a;
    20         }
    21     }
    22 }
    这是因为,声明类,可以认为是在类内放一张空白纸,实例化是按照图纸造了一座房子,类内声明自己,只是放了一张A类型的图纸,并没有造房子,不会引起无限调用。
    在函数中实例化类,必须调用函数才会执行,因此也不会引起无限调用,所以不会出现错误。
    由此,还可以实现单例模式。
    单例模式分为懒汉式和饿汉式
    懒汉式:
     1 public class test1 {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         test1.A aa = new test1().new A();
     9         test1.A b = aa.getA();
    10         System.out.println("ok");
    11     }
    12 
    13     class A{
    14  
    15         private A a = null;
    16         
    17         public A getA(){
    18             if(a == null)
    19                 a = new A();
    20             return a;
    21         }
    22     }
    23 }

    饿汉式:

     1 public class test1 {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         test1.A aa = new test1().new A();
     9         test1.A b = aa.getA();
    10         System.out.println("ok");
    11     }
    12 
    13     class A{
    14  
    15         private A a = new A();
    16         
    17         public A getA(){
    18             return a;
    19         }
    20     }
    21 }

    懒汉式容易线成不安全,饿汉式加载占内存

  • 相关阅读:
    RuntimeError: cryptography is required for sha256_password or caching_sha2_p
    Django-C003-视图
    MySQL 实时监控日志
    ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061) : 第一次设置MySQL也适用
    Django-C002-深入模型,到底有多深
    ubuntu16.04下安装&配置anaconda+tensorflow新手教程
    人脸算法系列:MTCNN人脸检测详解
    YOLO系列:YOLOv1,YOLOv2,YOLOv3,YOLOv4,YOLOv5简介
    python __getitem__()方法理解
    启动scala的方法
  • 原文地址:https://www.cnblogs.com/feichangnice/p/9117962.html
Copyright © 2011-2022 走看看