Java中private和public,Java中的public,protected和private有什么区别?

本文探讨了Java中访问修饰符的重要性,强调使用最小权限原则以实现代码封装,保护系统免受不必要的影响。从public到private,依次介绍了各修饰符的访问范围,包括公共、受保护、包内默认和私有。通过城堡比喻,阐述了如何根据需求选择合适的访问级别,以平衡代码修改自由度和稳定性。建议从private开始,并在必要时增加访问权限。
摘要由CSDN通过智能技术生成

Why we care

When modifying Java access, the general rule is to use only the minimum level of access that is necessary. Why? The idea is to encapsulate each piece of your system as much as possible. This way, you protect yourself and others from refactors that might have a ripple effect on other parts of the system. So, if you want to allow others to use your BuzzingBees method, but want to be able to modify BeautifulButterflies at your leisure, you can lock access to the butterflies down. You should be able to optimize your own code without breaking other code as much as possible. So, generally you want to start with private and go from there.

So how do we decide? In order of least private to most private:

Public, the simplest of the modifiers, will make the object most widely accessible. Everything in the module can see something public. It is accessible in all classes. If I was the queen of a kingdom, my castle would be big enough for everyone to see. Anyone could come up to the front gates and request entry to the castle via the bridge. They could even walk through the surrounding garden. But they wouldn’t be able to access the main hall or any of the contents of the castle without permission because these are not public.

Protected allows less access than public, making the member visible by classes throughout the same package OR any subclasses. This is package scope and child scope--potentially the most confusing level of access. Give protected access if you want to do some internal things that should not be exposed publicly, but still intend for the class to be inherited and potentially overridden by subclasses. If you want to change access from private to protected purely for unit testing, try to document that its functionality is not meant to be overwritten. If a lord or lady who carried the same family crest as me on their flag were to come to the gates of my castle, they would be granted entry immediately. They could come and go as they please in and out of the main hall.

With no modifier (default), a member will be accessible within all classes that are in the same package. This is package scope. Crest-carrying members of my family can be inside the castle, but only higher-ranking members, like the mother of the queen, who have been granted special privileges, may access the library or speak to the guards of the dungeon.

The private modifier restricts member access to only that specific class. This is also called class scope. It is a good starting place. To understand why, note that changing access from private to protected will not be a breaking change, but going the opposite way could be. Try to start here and add access when necessary. In our analogy, the castle is not completely open to my relatives. I would never allow my mother access to the treasure room, for example.

So, remember to drink lots of water, wash your face before going to bed, and use the minimum level of access necessary in your java code.

Modifier

Class

Package

Subclasses

World

public

protected

no modifier

private

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值