Java 中的 public、protected、package-private 和 private 之间有什么区别? [英] What is the difference between public, protected, package-private and private in Java?
本文介绍了Java 中的 public、protected、package-private 和 private 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Java 中,是否有明确规定何时使用每个访问修饰符,即默认(包私有)、public
、protected
和 private
,同时制作class
和interface
并处理继承?
In Java, are there clear rules on when to use each of access modifiers, namely the default (package private), public
, protected
and private
, while making class
and interface
and dealing with inheritance?
推荐答案
官方教程可能对你有用.
班级 | 包装 | 子类 (相同的包) | 子类 (diff pkg) | 世界 | |
---|---|---|---|---|---|
public | + | + | + | + | + |
受保护 | + | + | + | + | |
无修饰符 | + | + | + | ||
私有 | + |
+:可访问
空白:不可访问
+ : accessible
blank : not accessible
这篇关于Java 中的 public、protected、package-private 和 private 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文