在Java中使用可见性修饰符 [英] The use of visibility modifiers in Java

查看:285
本文介绍了在Java中使用可见性修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Orange{

    Orange(){
    }

}

在类的前面和构造函数前面使用修饰符(在本例中为package-private)之间的区别是什么?我认为构造函数前面的修饰符意味着允许实例化类 Orange 的实例。但是这个类前面的修饰符呢?

What is the difference between the usage of the modifier - in this case, package-private - in front of the class and in front of the constructor? I think the modifier in front of the constructor means it is allowed to instantiate an instance of the class Orange. But what about the modifier in front of the class?

推荐答案

首先,有3个访问修饰符创建了4个访问级别。

To start with there are 4 access levels created by 3 access modifiers.


  1. 公众 - 随处可访问

  2. 受保护 - 可在同一个套餐和儿童中使用

  3. 默认 - 只能在同一个包中访问

  4. private - 只能在同一个类中访问。

  1. public - accessible everywhere
  2. protected - accessible in the same package and in the children
  3. default - accessible only in the same package
  4. private - accessible only in the same class.

你是正确的 - 构造函数级别的修饰符与类的实例化直接相关。

You are correct about - Modifiers at the level of constructors are directly related to the instantiation of the class.

修饰符在Class级别决定Class的可访问性。

这篇关于在Java中使用可见性修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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