java反射用法示例
配套 (Packages)
Packages in Java is simply a mechanism to encapsulate (i.e. to put in a short and concise form) a group of classes,interfaces,enumerations, sub packages, etc. In real world, application is developed in such a manner so that we can easily maintain each module. To create package is simply use package keyword with name of the package at first statement in the program.
Java包只是一种封装(即,以简明扼要的形式)一组类,接口,枚举,子包等的机制。在现实世界中,应用程序的开发方式使我们可以轻松地维护每个模块。 要创建包,只需在程序的第一条语句中使用带有包名称的package关键字。
There are two type of Packages that are found in java,
java中有两种类型的Packages,
User defined packages
用户定义包
In Built packages
内置包装
1)用户定义的套餐 (1) USER DEFINED PACKAGES)
The Packages that are created by the user to differentiate between the classes and the interfaces that are made in their projects are user defined packages.
用户创建的用于区分类和在其项目中创建的接口的包是用户定义的包。
2)内置包装 (2) IN-BUILT PACKAGES)
The Packages that are the part of java API’s and includes variousclasses, interfaces, sub packages that are already defined in it are in-built packages. These packages are also known as the Predefined packages.
包是Java API的一部分,包括各种类,接口,已在其中定义的子包是内置包。 这些软件包也称为预定义软件包。
There are some packages that exists in java, they are:
Java中存在一些软件包,它们是:
java.lang: uses to bundles the fundamental classes.
java.lang :用于捆绑基本类。
java.io: classes for input , output functions are bundled in this package.
java.io :用于输入,输出功能的类捆绑在此包中。
java.util: classes which are implemented in data structure for date and time operations are bundled here.
java.util :这里捆绑了在数据结构中用于日期和时间操作的类。
java.applet: bundles classes for making applets .
java.applet :捆绑用于制作applet的类。
java.net: bundles the classes for supporting network operations.
java.net :捆绑用于支持网络操作的类。
These all are in-built packages that are commonly used.
这些都是常用的内置软件包。
Java包的优点 (MERITS of packages in java)
By the use of packages in java, it becomes easy to search and locate any class, annotation, enumeration etc.
通过使用Java中的包,可以轻松地搜索和找到任何类,注释,枚举等。
Naming conflict can be prevented that are occurred in between the different classes by the use of java packages.
通过使用Java包,可以防止在不同类之间发生命名冲突。
Java packages renders protection.
Java软件包提供了保护。
Most of programming tasks are done by the API’s classes and Packages, which minimize the number of lines that are written within the piece of code.
大多数编程任务都是由API的类和包完成的,它们可以最大程度地减少代码段中编写的行数。
Reduction in execution time i.e. execution time is less.
减少执行时间,即执行时间更少。
Uses less memory space.
使用更少的内存空间。
Improved performance.
改进的性能。
Steps for creating a user defined package:
创建用户定义包的步骤:
Package program’s first statement should be the package statement.
打包程序的第一个语句应为package语句。
Class modifier must we public so that the class and methods can be used outside the program.
我们必须公开Class修饰符,以便可以在程序外部使用类和方法。
Only one public class or only one public interface are used in package program while any number of normal classes are used in it.
程序包程序中仅使用一个公共类或仅一个公共接口,而在其中使用了任意数量的普通类。
It should contain any main class not the main () in it.
它应该包含任何主类,而不是main()。
Constructor modifier must be Public.
构造函数修饰符必须为Public。
Method modifier of class or interface must be public.
类或接口的方法修饰符必须是公共的。
The package program should be save either with public class name or a public interface name.
程序包应使用公共类名或公共接口名保存。
Syntax:
句法:
//Sum.java
//save package with 'public' classname
//first statement is package
package OurPackage
//class modifier must public
public class Sum {
//constructor modifier must public.
Public Sum() {
System.out.println("Sum class constructor");
}
//method modifier must public.
Public void show() {
System.out.println("Sum class method");
}
}
Read more: Packages in Java
: Java包
翻译自: https://www.includehelp.com/java/packages-in-java.aspx
java反射用法示例