(Copyright statement: self-created works, please do not forward! Otherwise, legal responsibility will be pursued.
A recap of last season's content:
Exception capture and management
Top knowledge points this season:
Packages and Access Rights
Package generation factor: All programs in development must be submitted to the server.
Zhang San:--gt;A.class--gt;Server
Li Si:--gt;A.class-------
The basic factor is that a folder cannot have files with the same name. A package is actually a folder, and each folder can have the same class of files.
Use of packages
Use package in the program to define the package.
Does the folder need to be created manually by the user?
It is definitely not required, because if the user builds it himself, it will be very troublesome in the future
It is best to automatically generate packages (folders) according to the format defined by package
javac-d.Demo01.java
·-d: Indicates the generated folder, and the name of the folder is based on the name defined in the package.
·.: Indicates that this package is generated under the folder of the current location. Class
How to access the programs in the package?
previous run: java class name
Packaged operation: java package. class name
For example: now define two classes (package. class name)
org.redking.demo01.Demo--gt; has a method that prints "HelloWorld"
org.redking.demo02.Demo02--gt; access the methods in the org.redking.demo01.Demo class and print the output
Demo.java
Demo02.java
After compiling the following error was generated:
Demo02.java:3<imgsrc="static/image/smiley/default/shocked.gif"smilieid="6"border="0"alt=""/>rg.redking.demo01.Demo at org.redking.demo01 is not public in; it cannot be accessed from external packages
importorg.redking.demo01.Demo;
^
Demo02.java:6: Symbol not found
Symbol: class Demo
Orientation: class org.redking.demo02.Demo02
newDemo().print();
^
2 Error If a class wants to be accessed by outsourcing, this class must be declared as publicclass, that is: the file name must be the same as the class name.
Let's compile it from scratch again~ Successfully compiled and executed~
If you want to compile multiple files at once you can use javac-d.
.java
question:
All you need to import now is one class, written in the following format:
importorg.redking.demo01.Demo;
So if I want to import multiple classes now, will this import be too much?
Suppose you now need to import many classes of a package, you can use "
" to show.
import package.
--gt;import
If you are currently using the import "
", will there be a problem with performance?
use import"
"There is essentially no difference between importing each class separately. Since the java program will automatically find the required classes and load them, unnecessary classes will not be loaded.
Look again:
Suppose there is a class of Demo03, which needs to import the org.redking.demoa1.A class, and also need to import the org.redking.demoa2.A class?
A.java
A.java
Two packages are generated with A.class in them
Demo03.java
Now that class A in two packages has been imported into Demo03, how will the program execute? There is an error in the compilation~
When two classes with the same name but different packages are imported into a class, it is better to enter the complete package.class name when visiting.
be careful:
There will never be a class without a package in the program, that is, as long as it is a class, it must be placed in a package.
Then if all the classes are placed in the package, then if you need to give a set of classes to other customers, you must not be able to put all the classes
.class files are scattered, and these are often
The .class file is compressed into a package - the jar package, and the jar package is given to the user.
If you want to make a class into
.jar package, you can use the jar command.
Person.java
jar packaging
Present my.jar file ha~~~
We can use winrar software to open my.jar file
Now that we have my.jar, let's delete the folder of the org.redking.test package~
How to use my.jar now? Let's write another DemoPerson.java
Now we compile DemoPerson.java
It prompts that the Person class cannot be found, but the Person class is now packaged in my.jar~
If it is a jar package, it is necessary to specify the classpath.
setclasspath=E:\java\oo9\my.jar
Need to specify two classpaths
setclasspath=.;E:\java\oo9\my.jar
This is the use of jar packages~~~
If you feel that the program needs a lot
The .jar package is very troublesome, you can set it directly in my computer--gt; high-end--gt; classpath.
common package
A lot of class libraries are provided, and the class libraries are registered in the way of packages.
JDK1.5 version, so this package will be automatically imported, when JDK1.0 needs to be imported manually.
GUI design package: java.awt, javax.swing--gt;JAVA2
Applet program is a small program running on the page, it is the oldest prototype of JAVA, but it is basically no longer used now.
Applet--gt; Garbage--gt; A program is a developmental process. EJB -- gt; rubbish -- gt; theory is valuable
Now we compile this applet program
You need to write html code to run the applet program
Access control
Person01.java
Demo04.java
can be accessed under the same package
At this point we can write a Stu subclass
Anonymous inner classes: used a lot in Spring
- but it develops on the basis of abstract classes and interfaces.
The above code has now clearly defined a subclass - X class
This is called an anonymous inner class.
Highlights of the season
################################################## ############
ps: I will use JAVASE next season~~~
##############################################)