Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bdc156b

Browse files
committed
in-java-whats-the-difference-between-public-default-protected-and-private
1 parent d6369fc commit bdc156b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ stackoverflow-Java-top-qa
1313
* [将InputStream转换为String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/read-convert-an-inputstream-to-a-string.md)
1414
* [将数组转换为List](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/create-arraylist-arraylistt-from-array-t.md)
1515
* [如何遍历map对象](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/iterate-through-a-hashmap.md)
16+
* [public,protected,private,不加修饰符。有什么区别呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/in-java-whats-the-difference-between-public-default-protected-and-private.md)
1617

1718
> 代码规范
1819
@@ -37,7 +38,6 @@ stackoverflow-Java-top-qa
3738
- [Generating random integers in a range with Java](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java)
3839
- [Why is printing "B" dramatically slower than printing "#"?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing)
3940
- [What is a serialVersionUID and why should I use it?](http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it)
40-
- [In Java, what's the difference between public, default, protected, and private?](http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private)
4141
- [Converting String to int in Java?](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java)
4242
- [Is there a unique Android device ID?](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id)
4343
- [How to test a class that has private methods, fields or inner classes](http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
##Java修饰符:public,protected,private,不加修饰符。有什么区别呢?
2+
3+
4+
如下表所示,Y表示能访问(可见性),N表示不能访问,例如第一行的第3个Y,表示类的变量/方法如果是用public修饰,它的子类能访问这个变量/方法
5+
| 修饰符 | 类内部| 同个包(package)| 子类 | 其他范围 |
6+
| -------- | :--: | :--: | :--: | :--: |
7+
| public | Y | Y | Y | Y |
8+
| protected | Y | Y | Y| N |
9+
| 无修饰符 | Y| Y | N or Y(见说明)| N |
10+
| private | Y | N | N | N |
11+
12+
说明:
13+
需要特别说明"无修饰符"这个情况,子类能否访问父类中无修饰符的变量/方法,取决于子类的位置。如果子类和父类在同一个包中,那么子类可以访问父类中的无修饰符的变量/方法,否则不行。
14+
15+
译注:本来觉得很简单一个问题,没想记录的,但看到答案,才发现自己以前错了。我以前一直以为无修饰符和private是一样的,如果没给变量加修饰符,java就默认为private。
16+
17+
stackoverflow链接:
18+
http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /