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反射用法示例