About the bag
Concept of package:
It can be used as a folder to understand that it is a management type to control access rights
Introduce the concept (full class name = package name + class name)
effect:
Avoid duplicate names
(Import keyword)
package day3;
/*
关键字import 导入外部包的类
*/
// import java.sql.Date; 只能用一次 不能使用import导入
import java.util.Date;
/*
包就是文件夹
作用:管理类
避免类重名 全类名:包名+类名
*/
public class Demo1 {
//默认为import导入的类名
Date date = new Date();
//只能使用全类名导入
java.sql.Date da = new java.sql.Date(10000);
}
Manage classes according to different functions
Introduce the concept (full class name = package name + class name)
Toolkit, access rights package...
Package naming rules
In the package name, you can use the. Number to distinguish the package level;
The package name is all lowercase
The first level refers to the type of the project, such as com (commercial type), org (non-profit type), gov (government type), etc.
The second level refers to the name of the company developed or operated by the project, such as: oracle, sun, huawei, etc.
The third level refers to the name of the project, such as: bcms, oa, erp, cms, etc.
The fourth level refers to the name of the project module, such as bean, action, exception, etc.
Can also be classified below (no requirement)
Access modifier
Arrange from largest to smallest
public Public modified classes, methods, attributes (can be accessed by any class)
protected protected permission modification method, attribute (must be accessed by a subclass in this class)
Default decoration class, method, attribute (can be accessed in different classes in the same package)
private private modification method, attribute (accessible only in this class)
package day3;
/*
public 公共的 修饰类 属性 方法
protected 受保护权限的 修饰属性 方法
默认的 修饰类 属性 方法
private 私有的 修饰 属性 方法
*/
public class Demo2 {
public int pnum;
protected int pronum;
int num;
private int prvnum;
public void Text(){
//在同一个类中,不管是什么访问符,都可以被访问
System.out.println(pnum);
System.out.println(pnum);
System.out.println(pnum);
System.out.println(pnum);
}
}
package day3;
public class Demo3 {
public static void main(String[] args) {
Demo2 demo2=new Demo2();
demo2.num=010;//在不同类中可以访问默认的 (同包不同类)
demo2.pnum=5;//在不同类中可以访问公共的 (同包不同类)
demo2.pronum=2;//在不同类中可以访问受保护的 (同包不同类)
}
}
package text;
import day3.Demo2;
public class Demo4 extends Demo2 {
public static void main(String[] args) {
Demo2 demo2=new Demo2();
demo2.pnum=56;//在不同包不同类中也可以访问公共属性
Demo4 demo4=new Demo4();
demo4.pronum=64;//在不同包不同类中可以通过继承来访问
}
}