Chapter Five, 2-package packages and permission modifiers

Source: Internet
Author: User
Tags modifier modifiers naming convention

One, Package packages

To better organize classes, Java provides a package mechanism for distinguishing the namespace of a class name.

The role of the package:

1, the function of similar or related classes or interfaces organized in the same package, convenient for the search and use of the class.

2, like the folder, the package also uses the tree directory storage. The class names in the same package are different, and the class names in different packages can be the same, and when you call two classes of the same class name in different packages, you should add the package name to differentiate it. Therefore, packages can avoid name collisions.

3. The package also restricts access, and classes that have access to the package can access the classes in a package.

This mechanism for using packages in Java is designed to prevent naming conflicts, access control, and to provide search and location classes, interfaces, enumerations, and annotations.

Ii. How to create a package in eclipse

Locate the Java project for which you want to create the package---Right-click on new---package---to name the packages (chapter II of the naming convention)---"Finish

A package named Automibile is created here:

We then create a BMW class in the Introduction7 package (the first row in each class indicates the package name that the class belongs to):

Third, permission modifier

Function: Used to control the visible range of modified variables, methods, classes.

The public access level is highest, and the starting time is protected, default, Private.

1. Move the cars and BMW classes created earlier (previous chapters) into the Automibile package

2. In the new Automibile package, create a class named Accessmodifierdome, in which a new cars object, new will probably error after completion, because in the previous section demonstrating inheritance inheritance, a parameter was added to the constructor method. Therefore, a default parameterless constructor must be written in the cars class (if this problem does not occur):

3, default: New A Cars object, call the variable speed in the Cars class, the variable permission modifier is the default, because in the same package, it can be called successfully. (You can move the class to its other package and then call the variable, you will find the program error, this occurs because the default permissions of the variable can only be accessed in the same package, if the cross-package access, the program will be error)

4. Private: Re-declare a shaping variable in the cars class, with the permissions private

 PackageAutomibile; Public classCars {intSpeed ; private int privatespeed;  PublicCars () { This(0); }    //Create a constructor method     PublicCars (intstartspeed) { speed=Startspeed; }        //To create a method named Increasespeed     Public voidincreasespeed () { speed++; System.out.println ("The acceleration of the cars class ... "); }        //To create a method named Idcreasespeed     Public voididecreasespeed () { speed--; System.out.println ("The deceleration of the cars class ... "); }}

Then we go to the Accessmodifierdome class to call this variable, show that the call is unsuccessful, the program error, because the private modified variables can only be accessed in the same class,

If you need to provide access, you can use the get and set methods to add the following code to the Cars class:

 PackageAutomibile;ImportJava.util.Set; Public classCars {intSpeed ; Private intPrivatespeed;  PublicCars () { This(0); }    Public void Setprivatespeed (int privatespeed) {this.privatespeed = Privatespeed;    } public int Getprivatespeed () {return privatespeed; }    //Create a constructor method     PublicCars (intstartspeed) { speed=Startspeed; }        //To create a method named Increasespeed     Public voidincreasespeed () { speed++; System.out.println ("The acceleration of the cars class ... "); }        //To create a method named Idcreasespeed     Public voididecreasespeed () { speed--; System.out.println ("The deceleration of the cars class ... "); }}

Called in the Accessmodifierdome class:

 Package Automibile;  Public class accessmodifierdome {    publicstaticvoid  main (string[] args) {         New Cars ();         = ten;        C1.setprivatespeed (   - );        System.out.println (C1.getprivatespeed ());}    }

Print Result: 20

5. Public permissions: As long as the package and this class are imported, they can be accessed in any other class.

6. Protected protected: Define a variable of that type in cars, change the Decreasespeed method permission to private permission,

We first call the Accessmodifierdome class in the same package to see if it can be called successfully,

We then open the Inheritancedemo class in the package named Introduction7, and then call the variables and methods in the cars class to see if the call was successful.

Next we call in the BMW class that inherits the cars class (cars and BMW are in the same package), and see whether the call succeeds through instantiation and direct invocation.

Then we were in the demo class that also inherited cars (the demo belongs to the package named Introduction7), and in the same two ways to see if the call was successful,

Summary: Protected permission variables or only the following methods can be successfully invoked

1, same package of similar

2. Different types of the same package

3. When different classes are in the same package, the caller and non-caller have an inheritance relationship, they can only access directly and cannot access after instantiating the object.

Related Article

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.