简体   繁体   English

类可以嵌套在类中多少次?

[英]How many times can classes be nested within a class?

I came across this questions on one of the online Java Tests. 我在其中一个在线Java测试中遇到了这些问题。 The options were 4,5,8 and any number of times. 选项分别为4,5,8和任意次数。

I have only used one inner class, but have never tried multiple ones. 我只使用了一个内部类,但从未尝试过多个内部类。 I was wondering if anyone knows the answer. 我想知道是否有人知道答案。

It's a completely irrelevant question and I hope they weren't using the results for anything important. 这是一个完全无关紧要的问题,我希望他们不会将结果用于任何重要的事情。 I guess the answer they were looking for was 'any number of times' but in practice there will be a limit in any given implementation of Java. 我猜他们正在寻找的答案是“任意次”,但实际上在任何给定的Java实现中都会有限制。 If it's not defined directly it will be determined by something like the maximum file size, or some other (possibly undocumented) internal limit. 如果没有直接定义,则将由最大文件大小或其他(可能未记录的)内部限制等内容确定。 The best way to find out is to try it. 找出答案的最佳方法是尝试一下。

Update: 30 works, but 300 gives this error: 更新:30个工作,但300给出此错误:

A.java:3: error while writing B0.B1.B2.B3.B4.B5.B6.B7.B8.B9.B10.B11.B12.B13.B14.
B15.B16.B17.B18.B19.B20.B21.B22.B23.B24.B25.B26.B27.B28.B29.B30.B31.B32.B33.B34.
B35.B36.B37.B38.B39.B40.B41.B42.B43.B44.B45.B46.B47.B48.B49.B50.B51.B52.B53.B54.
B55.B56.B57.B58.B59.B60.B61.B62.B63.B64.B65.B66.B67.B68.B69.B70.B71.B72.B73.B74.
B75.B76.B77.B78.B79.B80.B81.B82.B83.B84.B85.B86.B87.B88.B89.B90.B91.B92.B93.B94.
B95.B96.B97.B98.B99.B100.B101.B102.B103.B104.B105.B106.B107.B108.B109.B110.B111.
B112.B113.B114.B115.B116.B117.B118.B119.B120.B121.B122.B123.B124.B125.B126.B127.
B128.B129.B130.B131.B132.B133.B134.B135.B136.B137.B138.B139.B140.B141.B142.B143.
B144.B145.B146.B147.B148.B149.B150.B151.B152.B153.B154.B155.B156.B157.B158.B159.
B160.B161.B162.B163.B164.B165.B166.B167.B168.B169.B170.B171.B172.B173.B174.B175.
B176.B177.B178.B179.B180.B181.B182.B183.B184.B185.B186.B187.B188.B189.B190.B191.
B192.B193.B194.B195.B196.B197.B198.B199.B200.B201.B202.B203.B204.B205.B206.B207.
B208.B209.B210.B211.B212.B213.B214.B215.B216.B217.B218.B219.B220.B221.B222.B223.
B224.B225.B226.B227.B228.B229.B230.B231.B232.B233.B234.B235.B236.B237.B238.B239.
B240.B241.B242.B243.B244.B245.B246.B247.B248.B249.B250.B251.B252.B253.B254.B255.
B256.B257.B258.B259.B260.B261.B262.B263.B264.B265.B266.B267.B268.B269.B270.B271.
B272.B273.B274.B275.B276.B277.B278.B279.B280.B281.B282.B283.B284.B285.B286.B287.
B288.B289.B290.B291.B292.B293.B294.B295.B296.B297.B298.B299: B0$B1$B2$B3$B4$B5$B
6$B7$B8$B9$B10$B11$B12$B13$B14$B15$B16$B17$B18$B19$B20$B21$B22$B23$B24$B25$B26$B
27$B28$B29$B30$B31$B32$B33$B34$B35$B36$B37$B38$B39$B40$B41$B42$B43$B44$B45$B46$B
47$B48$B49$B50$B51$B52$B53$B54$B55$B56$B57$B58$B59$B60$B61$B62$B63$B64$B65$B66$B
67$B68$B69$B70$B71$B72$B73$B74$B75$B76$B77$B78$B79$B80$B81$B82$B83$B84$B85$B86$B
87$B88$B89$B90$B91$B92$B93$B94$B95$B96$B97$B98$B99$B100$B101$B102$B103$B104$B105
$B106$B107$B108$B109$B110$B111$B112$B113$B114$B115$B116$B117$B118$B119$B120$B121
$B122$B123$B124$B125$B126$B127$B128$B129$B130$B131$B132$B133$B134$B135$B136$B137
$B138$B139$B140$B141$B142$B143$B144$B145$B146$B147$B148$B149$B150$B151$B152$B153
$B154$B155$B156$B157$B158$B159$B160$B161$B162$B163$B164$B165$B166$B167$B168$B169
$B170$B171$B172$B173$B174$B175$B176$B177$B178$B179$B180$B181$B182$B183$B184$B185
$B186$B187$B188$B189$B190$B191$B192$B193$B194$B195$B196$B197$B198$B199$B200$B201
$B202$B203$B204$B205$B206$B207$B208$B209$B210$B211$B212$B213$B214$B215$B216$B217
$B218$B219$B220$B221$B222$B223$B224$B225$B226$B227$B228$B229$B230$B231$B232$B233
$B234$B235$B236$B237$B238$B239$B240$B241$B242$B243$B244$B245$B246$B247$B248$B249
$B250$B251$B252$B253$B254$B255$B256$B257$B258$B259$B260$B261$B262$B263$B264$B265
$B266$B267$B268$B269$B270$B271$B272$B273$B274$B275$B276$B277$B278$B279$B280$B281
$B282$B283$B284$B285$B286$B287$B288$B289$B290$B291$B292$B293$B294$B295$B296$B297
$B298$B299.class (The filename, directory name, or volume label syntax is incorr
ect)

Code used to generate source (written in Python): 用于生成源代码(用Python编写):

n = input()
print "class A{public static void main(String[] a){}}\n"
print ''.join("class B%d{" % x for x in range(n)) + '}' * n

Disappointing. 令人失望。 I was actually hoping it would be a lot more. 我实际上希望它会更多。 I wonder if it could get much further on another system which allows longer filenames. 我想知道是否可以在另一个允许更长文件名的系统上取得更多进展。

Hmmm. 嗯。 I know that the online Java test you refer to is a lousy one. 我知道你提到的在线Java测试是一个糟糕的测试。 Does that count? 这算数了吗?

(This is the kind of limit that is irrelevant in practical experience. A similarly ridiculously question would be, "What is the maximum length of a function in bytes?") (这是一种与实际经验无关的限制。同样荒谬的问题是,“函数的最大长度是多少,以字节为单位?”)

I tried on my own: The answer is any number of times. 我自己尝试了:答案是任何次数。 The following is my test class, I had no compilation errors. 以下是我的测试类,我没有编译错误。

public class Test {


    public Test ()
    {

    }
    public static void main (String args[])
    {
    new Test ();
    }

    class Test2
    {
        class Test3
        {
            class Test4
            {
                class Test5{
                    class Test6{
                        class Test7{
                            class Test8{
                                class Test9
                                {

                                }
                            }
                        }
                    }
                }
            }

        }

    }


}

The true answer is none of the above. 真正的答案是以上都没有。 Javac will of course allow arbitrary nesting, but the underlying file system and/or OS have restrictions. Javac当然会允许任意嵌套,但底层文件系统和/或操作系统有限制。 We found recently that during startup the JVM running a Glassfish Java EE application server instance will hold open files to each .class during loading/JIT compilation. 我们最近发现,在启动期间,运行Glassfish Java EE应用程序服务器实例的JVM将在加载/ JIT编译期间为每个.class保存打开的文件。 If you're running CentOS Linux say, the default limit on open files/process is 1024, so if you have a lot of classes being loaded at once and don't set the ulimit -n to a higher value, the JVM will hit "too many open files". 如果您正在运行CentOS Linux,那么打开文件/进程的默认限制是1024,所以如果您一次加载了很多类并且没有将ulimit -n设置为更高的值,那么JVM将会命中“太多的打开文件”。 And @Mark Byers' example shows that the .class file names get very long and can hit the file system name length limit, if there is one. 而@Mark Byers的示例显示.class文件名变得非常长并且可以达到文件系统名称长度限制(如果有的话)。

As a follow-up to Mark's trial on windows - a bit silly but fun nonetheless - I did a trial on AIX 5.3. 作为Mark在Windows上进行试验的后续行动 - 虽然有点愚蠢但有趣 - 我在AIX 5.3上进行了试验。

91 subclasses deep is ok, number 92 results in the following error : 91个子类深度正常,数字92导致以下错误

-rw-r--r--   1 root     system        12813 Nov 24 15:15 A.class
Main class=class A

count=90 class=A$B$C$D$E$F$G$H$I$J$K$L$M$N$O$P$Q$R$S$T$U$V$W$X$Y$Z$AA$AB$AC$AD$AE$AF$AG$AH$AI$AJ$AK$AL$AM$AN$AO$AP$AQ$AR$AS$AT$AU$AV$AW$AX$AY$AZ$BA$BB$BC$BD$BE$BF$BG$BH$BI$BJ$BK$BL$BM$BN$BO$BP$BQ$BR$BS$BT$BU$BV$BW$BX$BY$BZ$CA$CB$CC$CD$CE$CF$CG$CH$CI$CJ$CK$CL$CM

-rw-r--r--   1 root     system        13081 Nov 24 15:16 A.class
Main class=class A

count=91 class=A$B$C$D$E$F$G$H$I$J$K$L$M$N$O$P$Q$R$S$T$U$V$W$X$Y$Z$AA$AB$AC$AD$AE$AF$AG$AH$AI$AJ$AK$AL$AM$AN$AO$AP$AQ$AR$AS$AT$AU$AV$AW$AX$AY$AZ$BA$BB$BC$BD$BE$BF$BG$BH$BI$BJ$BK$BL$BM$BN$BO$BP$BQ$BR$BS$BT$BU$BV$BW$BX$BY$BZ$CA$CB$CC$CD$CE$CF$CG$CH$CI$CJ$CK$CL$CM$CN

A.java:93: error while writing A.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.W.X.Y.Z.AA.AB.AC.AD.AE.AF.AG.AH.AI.AJ.AK.AL.AM.AN.AO.AP.AQ.AR.AS.AT.AU.AV.AW.AX.AY.AZ.BA.BB.BC.BD.BE.BF.BG.BH.BI.BJ.BK.BL.BM.BN.BO.BP.BQ.BR.BS.BT.BU.BV.BW.BX.BY.BZ.CA.CB.CC.CD.CE.CF.CG.CH.CI.CJ.CK.CL.CM.CN.CO: A$B$C$D$E$F$G$H$I$J$K$L$M$N$O$P$Q$R$S$T$U$V$W$X$Y$Z$AA$AB$AC$AD$AE$AF$AG$AH$AI$AJ$AK$AL$AM$AN$AO$AP$AQ$AR$AS$AT$AU$AV$AW$AX$AY$AZ$BA$BB$BC$BD$BE$BF$BG$BH$BI$BJ$BK$BL$BM$BN$BO$BP$BQ$BR$BS$BT$BU$BV$BW$BX$BY$BZ$CA$CB$CC$CD$CE$CF$CG$CH$CI$CJ$CK$CL$CM$CN$CO.class (A file or path name is too long.)
static class CO {
       ^
1 error

The generated Java source looks like: 生成的Java源代码如下所示:

class A {
  static class B {
    void run() { 
      System.out.println("count=" + 2 + " class=" + B.class.getName() + "\n"); 
    }
  }

  public static void main(String[] a){
    System.out.println("Main class=" + A.class + "\n");
    (new A.B()).run();
  }
}

是的,你可以在Java中无限地嵌套一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将其中包含嵌套类的类的对象传递给活动? - How can I pass an Object of a class that has nested classes within it to an Activity? 当我有许多 SpringBootTest 类时,如何有效地使用嵌套配置类来注入依赖项 - How can I efficiently use nested configuration classes to inject dependencies when I have many SpringBootTest classes Kryonet - 如何在课堂上注册课程? - Kryonet — How can I register classes within my class? 如何创建在所有大型类中都具有嵌套的类之前仅运行一次的扩展 - How to create Extension which runs beforeAll only once for whole big class with many nested classes inside 我们可以在java / android中的一个类中声明多少个接口 - How many interface we can declare within a class in java/android 静态嵌套类可以多次实例化吗? - Can a Static Nested Class be Instantiated Multiple Times? 您如何计算嵌套的for循环迭代多少次? - How do you count how many times a nested for loop iterates? 在散列映射中可以进行多次重新散列 - How many times can rehashing occur in a hashmap 令牌可以在正则表达式中重复多少次 - how many times a token can be repeated in regex 垃圾收集器可以调用多少次? - How many times can the Garbage Collector be called?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM