Java中的默认访问修饰符是什么? [英] What is the default access modifier in Java?

查看:501
本文介绍了Java中的默认访问修饰符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我没有明确说明方法或实例变量的默认访问修饰符是什么?

What is the default access modifier for a method or an instance variable if I do not state it explicitly?

例如:

package flight.booking;

public class FlightLog
{
    private SpecificFlight flight;

    FlightLog(SpecificFlight flight)
    {
        this.flight = flight;
    }
}

此构造函数的访问修饰符是受保护的还是包的?可以在同一个包中的其他类( flight.booking )调用此构造函数吗?

Is the access modifier of this constructor protected or package? Can other classes in the same package, which is flight.booking, call this constructor?

推荐答案

来自Java 文档


如果一个类没有修饰符(默认的,也称为包私有),它只在它自己的包中可见(包被命名为相关课程 - 您将在后面的课程中了解它们。)

If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

成员级别,您也可以使用public修饰符或 no modifier (package-private),就像使用顶级类一样,并且具有相同的含义。

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning.

你可以在这里阅读全文(我最近写的):

Full story you can read here (Which I wrote recently):

http://codeinventions.blogspot.com / 2014/09 /default-access-modifier-in-java-or-no.html

这篇关于Java中的默认访问修饰符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆