J2SE basics: 3. Create and use a class namespace and access permission for an object
1: package.
A package is a class container used to save the namespace of a category.
Logically, classes are divided.
Resolves conflicts between classes with the same name.
2: package naming rules:
A: The package must be written in the first line of the source file.
B: All package names must be in lower case.
C: (recommended) package uses the company's domain name.
Www.demo.com
Com. demo. Project name. Module name
3: packages are logically divided by namespaces.
Physically, it is divided by the file system path.
Com. demo. one
After compiling the class, put the compiled class under the com/demo/one folder.
Run
Java com. demo. one. Class Name;
4: Functions of the javac-d option
-D: Specifies the location where class files are stored.
Javac-d./Test_03.java
How to run classes with package and classpath
C:/> java-classpath F: \ aa. bb. cc. dd. ee. Test_03
Package import)
Classes under the same package can call each other.
When classes under different packages need to be used under another package
The path of the class required to import the import keyword.
5. Access to a famous package with no package name:
The classes under the Untitled package can access the classes under the famous package.
Classes under the famous package cannot access the classes under the unknown package.
6. access permission
Private: the access permission in the same class. Other classes cannot be accessed and modified using private.
.
Default: All classes under the same package can be accessed. Different packages cannot be accessed by default
Default class.
Protected: All classes in the same package can be accessed. Different packages cannot be accessed,
Under the same package, the subclass of this class can access the variables and Methods Modified by protected.
Method.
package aa.bb.cc.dd.ee;public class Test_03{public static void main(String args[]){System.out.println("Test_03");}}
package com.testdog;public class Dog{private int dogid;protected String dogName;int getDogid(){return this.dogid;}void setDogid(int dogid){this.dogid = dogid;}protected void setDogName(String dogName){this.dogName = dogName;}}
package com.testprotected;import com.testdog.Dog;public class TestProtected extends Dog{public static void main(String args[]){TestProtected dog = new TestProtected();dog.setDogName("test");System.out.println(dog.dogName);}}