Java中有什么类似内部类的东西吗?

2022-08-31 23:59:08

在 C# 中,可以将类标记为只能从同一包中访问它。Java中有什么类似的东西吗?internal


答案 1

可以通过从类的声明中省略安全修饰符(公共、私有)来创建包私有类。

package com.sample;

class MyPackagePrivateClass
{
    ...
}

答案 2

删除访问修饰符类似于 C# 中的内部修饰符。

C#

public class A
{
    public static int X;
    internal static int Y;
    private static int Z;
}
internal class B
{
    public static int X;
    internal static int Y;
    private static int Z;
    public class C
    {
        public static int X;
        internal static int Y;
        private static int Z;
    }
    private class D
    {
        public static int X;
        internal static int Y;
        private static int Z;
    }
}

爪哇岛

public class A
{
    public static int X;
    static int Y;
    private static int Z;
}
class B
{
    public static int X;
    static int Y;
    private static int Z;
    public class C
    {
        public static int X;
        static int Y;
        private static int Z;
    }
    private class D
    {
        public static int X;
        static int Y;
        private static int Z;
    }
}

资料来源:http://www.javacamp.org/javavscsharp/internal.html