Control of packages and access rights in Java

Source: Internet
Author: User
Tags mul

Multi-person developmentin Java, you can separate the classes in a large project, categorize them into files, and compile them together, so that the program code will be easier to maintain. Multi-person development problem: If multiple developers develop a project together, there is a case that the class name is the same. Then it will be very troublesome.
The same file will appear in the Overwrite case. concept of the packageThe package is a measure to avoid duplicate names when using multiple classes or interfaces, simply by adding the Packages keyword to the program. Package Definition Format: package name. Child package name;
Package Org.lxh.demo08.demo;p ublic class Hello{public String GetInfo () {return ' Hello world!!! ';}};
After the package is defined, the name of the class is actually: Package. Class Nameto package a program:
Package org.lxh.demo08;//define a packet class Demo{public String GetInfo () {return "Hello world!!!";}}; public class Packagedemo01{public static void Main (String args[]) {System.out.println (New Demo (). GetInfo ());}};
Program compilation: Javac-d. Packagedemo01.javathe so-called package is actually a folder, a *.class file to be saved in a folder. Since the package itself is a folder, in the Java compiler directive provides the specialized package compiles the command, adds the parameter at compile timePackage Compilation:javac-d. Packagedemo01.javaonce the *.CALSS has been generated, it can be accessed directly. Java org.lxh.demo08.PackageDemo01if it is simpler in myeclipse, create a new org.lxh.demo08 package directly under SRC, declare the package name directly within the package, and create a new Java file.
Import of packages:The Import command is required when a package class file needs to use a class file of another package. Import Statementthe above program, two classes are stored in the same package, so your code is not fundamentally different from before, but if several classes are stored in different packages, you must use the import statement when the class is imported.
Demo.java
Package org.lxh.demo08.a; class demo{//default permission is the public String getcontent () {return "LX XXX") accessed by the class that cannot be outsourced;
Importdemo01.java
Package org.lxh.demo08.b;//put in different packages import org.lxh.demo08.a.*;//Import the Demo class public class Importdemo01{public static in different packages void Main (String args[]) {System.out.println (New Demo (). GetInfo ());}};
If you use a DOS command, you should compile the Demo.java class before compiling Importdemo01.java, because the latter uses the former class to operate. A compile-time error was found because the demo class does not have the default permission, and the class for default permissions cannot be accessed by classes that are not in the same package. Plus public can be accessed by the outside class.Public class and class:if a class is declared as public class, the file name must be the same as the class name, and there can be only one public class in a class, and if you declare a class using class, the file name can be inconsistent with the class name. However, the generated class file name must be executed at the time of execution. In addition to these, the public class and class are limited in the access to the package, if a class is accessed only in this package and does not need to be outsourced, it is declared as class, and if a class needs to be outsourced, it must be declared as a public class. In general development, for a *.java file, you often define only one class: public class. This is done explicitly by using the package. The class name is imported in a way. If you want to now assume that you want to import many classes in a package, it would be cumbersome to write. You can import directly by using the "*" method.
Package org.lxh.demo08.b;//put in different packages import org.lxh.demo08.a.*;//Import the Demo class public class Importdemo01{public static in different packages void Main (String args[]) {System.out.println (New Demo (). GetInfo ());}};
Problem:what is the higher performance of the two import statements above? The first type: import org.lxh.demo08.a.*; The second type: Package org.lxh.demo08.b;in fact, the performance of both is the same, because if you use * to import, it is the JVM to help users determine the need to import the class, the unwanted classes will not be loaded in. However, in the case of the guide package should also pay attention to another problem, if you import different packages of the same name when the class can be ambiguous information.

Package org.lxh.demo08.d; import org.lxh.demo08.a.*;//There is a demo class import org.lxh.demo08.c.* in the packet;//There is a demo class public class in the package Importdemo02{public static void Main (String args[]) {Demo d = new demo (); System.out.println (D.getinfo ());};

Now define a class to import two packages, two packages have a demo class, at this time when declaring the demo object will be an error, because the JVM can not distinguish between what is called the class in the package. So if this happens, it's best to write the full "package. Class name". Modify the code as shown below.
Package org.lxh.demo08.d; import org.lxh.demo08.a.*;//There is a demo class import org.lxh.demo08.c.* in the packet;//There is a demo class public class in the package Importdemo02{public static void Main (String args[]) {Org.lxh.demo08.a.demo d = new Org.lxh.demo08.a.demo (); System.out.println (D.getinfo ());};


Java.util.Arrays.sort ()Of course, in these packages, there are also a large number of sub-packages. new Java Features--static importThe function of static import is provided after JDK1.5, what is static import? If the methods in a class are all static methods that use static declarations, you can import them directly using the import static method when importing them in the following ways: import static Package. class. *;
Package ORG.LXH.DEMO08.E;p Ublic class operate{//methods are all static type public static int Add (int i,int j) {//addition operation return i + j ;} public static int sub (int i,int j) {//subtraction operation return i-j;} public static int Mul (int i,int j) {//multiplication operation return I * j;} public static int div (int i,int j) {//Division operation return i/j;}};
If you import it in the same way as before, you must use the class when you call. Method () "
Package org.lxh.demo08.f, import static org.lxh.demo08.e.operate.*;//static import public class Staticimportdemo{public static void Main (String args[]) {System.out.println ("3 + 3 =" + Add (3,3));//Call static method System.out.println directly ("3-2 =" + sub (3,2)); Call the static method System.out.println directly ("3 * 3 =" + Mul (3,3));//Call the static method System.out.println directly ("3/3 =" + div (3,3));//Call static method}};

Static methods can be called directly after a static import.use of the jar command-Package the class
You can enter the jar directly:

main parameters in the jar command:"C": Create a new document. "V": generates verbose output information. "F": Specifies the file name of the archive.
Package Org.lxh.demo08.demo;p ublic class Hello{public String GetInfo () {return ' Hello world!!! ';}};
Package it to compile: javac-d. Hello.javaMake it into a jar package: JAR-CVF my.jar org
means that the folder org is the total folder packaged into My.jara jar Package if you want to use it, you must configure the Classpath pathset classpath=.; E:/xxxx/my.jarimporting the jar package directly in MyEclipse allows you to import the classes in the jar using import. in the actual Java development often put some practical tool class into the jar package to the user to use. Summary of the package:1, the package can be a lot of class file classification of good storage, so as to avoid the development of many people, the same class file name problem. 2, in the actual development, no package class is basically nonexistent, the complete class name "package. Class name". 3, if you import a different package of the same name class, you can directly through the full package. The class name avoids duplication. 4. After JDK1.5, the static import function is provided, and a static method of a class can be used directly. 5. If all classes in a package need to be delivered to the user. You want to make it into a jar package. access control permissions and naming conventions. There are four types of access control permissions in Java:Private access rights. default does not add any declared access rights. protected protected access rights. Public access rights.
Private: You can define methods or properties that define methods and properties that cannot be accessed by external classes (including subclasses). default: It can be accessed anywhere in the package. protected: Protection, non-subclasses in different packages cannot be accessed. Public: It is accessible to all, and is not subject to any restrictions.
Review Summary:when a class is produced, the private keyword is used in order to ensure that the contents of the class are not seen directly externally. However, if the two related classes are now going to be troublesome to access the properties, they can only be obtained and set using the Setter/getter method, so in order to reduce the hassle of private property access, the class inner class is used, but the inner class destroys the structure of the program. In order to make the contents of a class continue to be convenient to use, the concept of inheritance is used, but the private property in inheritance is also not able to see the quilt class, so at this time, in order to facilitate the operation of the subclass, the attributes can be protected encapsulated, so that the external cannot directly see (different packages). after inheritance, since there is a parent-child relationship, you can use up or down transformation operations to accomplish polymorphism. , but the direct inheritance between classes and classes in development is not common, but often inherits abstract classes or implements interfaces, which can be done when several operations need to be decoupled. since there is an inner class, you can define it as an anonymous inner class if the subclass of an interface or abstract class is used only once. A class that does not have a package in development is absolutely nonexistent. Inheritance--- polymorphism, encapsulation
Java Naming conventions:class: Capitalize the first letter of all words. such as: Testjava. method: The first letter is lowercase, followed by the first letter of each word, such as: GetInfo (). Properties: The first letter in lowercase, followed by the initial capitalization of each word, such as: Studentname. Package: The first letter of all words is lowercase. such as: Hhxy.lx.ppconstants: Capitalize the first letter of all words, such as: FLAG

Control of packages and access rights in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.