[CS61B-Sp18] Packages and Access Control

Packages and Access Control

Packages

Creating

  1. Put the package name at the top of every file in this package
  2. Store the file in a folder that has the appropriate folder name

In IntelliJ:

  1. File → New Package

  2. Choose package name (i.e. “ug.joshh.animal”)

Adding (new) Java Files to a Package, in IntelliJ:

  1. Right-click package name

  2. Select New → Java Class

  3. Name your class, and IntelliJ will automatically put it in the correct folder

Adding (old) Java Files to a Package, in IntelliJ:

  1. Add “package [packagename]” to the top of the file.

  2. Drag the .java file into the corresponding folder.

Default packages

Bad:

  • code from the default package cannot be imported,
  • possible to accidentally create classes with the same name under the default package

JAR files

Creating a JAR File (IntelliJ):

  1. Go to File → Project Structure → Artifacts → JAR → “From modules with dependencies”

  2. Click OK a couple of times

  3. Click Build → Build Artifacts (this will create a JAR file in a folder called “Artifacts”)

  4. Distribute this JAR file

Build Systems

Rather than importing a list of libraries, we can simply put the files into the appropriate place, and use “Build Systems”.

Access Control

Access is based only on static type.

ModifierClassPackageSubclassWorld
publicYYYY
protectedYYYN
(package protected)YYNN
privateYNNN
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值