[零基础学JAVA]Java SE面向对象部分-21.包及访问权限
版权声明:自创著作,谢绝转发!否则将追查法律责任。上季内容回顾:
异常的捕获与管理
本季首要知识点:
包与访问权限
包的生成因素:在开发中一切的程序必定要提交到服务器上去。
张三:--gt;A.class--gt;服务器
李四:--gt;A.class-------
基本因素在于一个文件夹下不能有重名的文件,包其实即是一个文件夹,每个文件夹下可以有相同类文件。
包的使用
在程序中使用package进行包的定义。
文件夹需求用户自己手艺建立吗?
必定不需求,由于假如用户自己建立,则今后很费事
最好可以依据package定义的格式自动生成包(文件夹)
javac-d.Demo01.java
·-d:表明生成文件夹,文件夹的名称即是以package中定义的名称为准。
·.:表明在当前地点的文件夹下生成此包.类
如何去访问包中的程序呢?
之前的运行:java类称号
打包后的运行:java包.类称号
例如:如今定义两类(包.类称号)
·org.redking.demo01.Demo--gt;有一个打印“HelloWorld”的方法
·org.redking.demo02.Demo02--gt;访问org.redking.demo01.Demo类中的方法,打印输出
Demo.java
Demo02.java
编译之后生成了以下错误:
Demo02.java:3<imgsrc="static/image/smiley/default/shocked.gif"smilieid="6"border="0"alt=""/>rg.redking.demo01.Demo在org.redking.demo01中不是公共的;无法从外部软件包中对其进行访问
importorg.redking.demo01.Demo;
^
Demo02.java:6:找不到符号
符号:类Demo
方位:类org.redking.demo02.Demo02
newDemo().print();
^
2错误假如一个类想要被外包所访问,则此类一定要声明成publicclass,即:文件称号要与类称号共同。
我们再从头编译下哈~成功编译及履行哈~
假如想一次编译多个文件可以使用javac-d.
.java
问题:
你如今导入的只要一个类,是按以下格式写的:
importorg.redking.demo01.Demo;
那么假如我如今要导入多个类的时候,这样导入会不会太多了呢?
假如假定如今需求导入一个包的很多类,则可以使用“
”来表明。
import包.
--gt;导入
假如如今使用的是导入“
”,性能上会不会有问题?
使用导入“
”和分别导入各个类本质上没有任何的区别。由于java程序会自动找到所需求的类进行加载,不必的类,是不会加载的。
再看:
假如说如今有一个Demo03的类,里边需求导入org.redking.demoa1.A类,也需求导入org.redking.demoa2.A类时会如何?
A.java
A.java
生成了两个包里边都有A.class
Demo03.java
如今Demo03中导入了两个包中的A类,程序会如何履行呢?编译出错了哈~
当一个类中导入了两个同名但不一样包的类的时候,在访问时最好参加完整的包.类称号。
注意点:
在程序中一直不会有没有包的类,即:只要是类肯定要放在包里。
那么假如一切的类都放在包里了,那么如今假如需求把一套类交给别的客户的时候必定是不能把一切
.class文件散着给出去,往往会把这些的
.class文件压缩成一个包——jar包,把jar包给用户。
假如要把一个类打成
.jar包,则可以借助于jar命令。
Person.java
jar打包
呈现my.jar文件哈~~~
我们可以使用winrar软件打开my.jar文件
如今有了my.jar,我们把org.redking.test包的文件夹删掉哈~
如今my.jar如何使用呢?我们再写一个DemoPerson.java
如今我们编译DemoPerson.java
提示找不到Person类哈,可是Person类现已在my.jar包了哈~
假如是一个jar包,则有必要指定classpath。
setclasspath=E:\java\oo9\my.jar
需求指定两个classpath
setclasspath=.;E:\java\oo9\my.jar
这即是jar包的使用哈~~~
假如觉得程序需求的很多
.jar包十分费事,则可以直接在我的电脑--gt;高档--gt;classpath进行设置。
常见的包
提供了很多的类库,类库中是按照包的方式寄存的。
JDK1.5版本,所以此包会自动导入,JDK1.0的时候需求手艺导入。
GUI设计包:java.awt、javax.swing--gt;JAVA2
Applet程序是在页面上运行的小程序,是JAVA的最陈旧原形,可是如今基本上也现已不再使用了。
Applet--gt;垃圾--gt;程序是一个发展过程。EJB--gt;垃圾--gt;理论很有价值
如今我们编译此Applet程序
需求编写html代码才可以运行applet程序
访问控制
Person01.java
Demo04.java
在同一包下可以进行访问
这时我们可以编写一个Stu子类
匿名内部类:Spring中使用的十分多
-但它是在抽象类和接口的基础上发展的。
以上代码中现已明确的定义出了子类——X类
这就叫匿名内部类哈~~~
本季要点
##############################################################
ps:下季进行JAVASE使用哈~~~
##############################################
(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~~~
##############################################)
页:
[1]