在java中我很困惑何时公开一个类

gal*_*lly 0 java

我正在进入java并遇到了这个例子

interface Nose{
    public int iMethod();
}

abstract class Picasso implements Nose{
    public int iMethod(){
        return 7;
    }
}

class Clowns extends Picasso{}

class Acts extends Picasso{
    public int iMethod(){
        return 5;
    }
}
Run Code Online (Sandbox Code Playgroud)

因为没有任何内容被声明为public,这是否意味着这些类都不能从另一个文件中调用?我有另一个档案

public class Of76 extends Clowns{
    public static void main(String[] args) {
        Nose [] i = new Nose[3];
        i[0] = new Acts();
        i[1] = new Clowns();
        i[2] = new Of76();
        for(int x = 0; x < 3; x++) {
            System.out.println(i[x].iMethod()+" "+i[x].getClass());
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

因此,在这个示例中,类Of76可以从另一个没有任何公共类的文件中创建类.我很困惑为什么第一个文件可以拥有所有这些类以及为什么它们不在单独的类中.我读到默认情况下非公开的类是私有的,只能在同一个类中调用.那么Nose文件中的所有内容都只能在该Nose文件中调用?

Dan*_*lan 6

当你排除public并且没有其他任何东西时,protected或者private这被称为"包受保护".这应该解释给你的不同之处:在Java中,默认,公共,受保护和私有之间的区别