Thinking in Java(6)——Access Control

Package

提供管理名字空间的机制。
一个java源代码文件为一个编译单元(compilation unit or translation unit),只能有一个public class,如果有别的class,必须hidden from the public。

Code organization
  • Compile a java file -> Get output file for each class (with “.class” extension.)
  • A working prgram is a bunch of .class files compressed into a java ARchive file. The java interpreter is responsible for finding loading, and interpreting these files.
  • A library is a group of these files.
  • package and import keywords allow you to divide up the single global namespace so you won’t have clashing names.
Creating unique package names

By convension, the first part of the package name is the reversed Internet domain name of the creator.

Java access specifiers

Package access
  • Use no access specifers, default acess.
  • public to other classes in the same package, private to all classes outside.
public: interface access

The default package: if two compilation are in the same directory and havent specified their packages, they are granted package access by default.

private access: you cant touch that

No one can access that member except the class that contains it.

protected access: inheritance access
  • Grant access to derived classes
  • Give package access

Interface and implementation

Seperate the interface and implementation

Class access

  • Only one public class per compilation unit
  • The name of the public class must exactly match the name of the file.
  • If a compilcation unit has no public class, you can name the file whatever you like.
  • class cannot be private or protected
  • If a class has private constructor, we can use static method to create new object.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值